Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Sort GridField by Column


Go to End
Reply


14 Posts   2912 Views

Avatar
svandragt

Community Member, 44 Posts

10 October 2012 at 4:05am

Edited: 10/10/2012 4:05am

How do I set the field by which the gridfield is sorted?

I tried the following but it oesn't work even though it's recognised as setting the fieldsorting in GridFieldSortableHeader.

      $gf_config = GridFieldConfig::create()->addComponents(
         new GridFieldToolbarHeader(),
         new GridFieldSortableHeader(),
         new GridFieldAddNewButton('toolbar-header-right'),
         new GridFieldDataColumns(),
         new GridFieldEditButton(),
         new GridFieldDeleteAction(),
         new GridFieldDetailForm()
      );
    $gridfield = new GridField("Steps", "Steps:", $this->Steps(), $gf_config);
$sortable = $gridfield->getConfig()->getComponentByType('GridFieldSortableHeader');
$sortable->setFieldSorting('Title');

Avatar
RuthAdele

Community Member, 19 Posts

11 October 2012 at 2:23pm

I would also like to know this... :)

Avatar
simon_w

Forum Moderator, 474 Posts

11 October 2012 at 2:53pm

Sort the DataList you're passing in, so use $this->Steps()->sort('Title').

Avatar
RuthAdele

Community Member, 19 Posts

11 October 2012 at 3:47pm

Thankyou! Works a treat. :)

Avatar
pinkp

Community Member, 168 Posts

25 April 2013 at 11:29pm

How do you get it to keep this sort order from the CMS when looped in the template? (im not using drag n drop) I just want the order it appears in the CMS to translate to the front end..

Avatar
RuthAdele

Community Member, 19 Posts

26 April 2013 at 12:53pm

If you're not adding a custom Sort Order, you should simply be able to loop over the DataObject in the template, i.e.
<% loop Objects %>
$Title, $Blah
<% end_loop %>

But if you are displaying with the sort order, you need to write a function in your page controller:
public function OrderedObjects() {
$dataList = DataList::create("Object");
$dataList->sort("SortOrder ASC");

return $dataList : $dataList ? false;
}
and then loop it the same in the template, but with <% loop OrderedObjects %>

Hope that helps :)

Avatar
pinkp

Community Member, 168 Posts

27 April 2013 at 12:07am

Edited: 27/04/2013 12:12am

Thanks so much for your reply RuthAdele (dont get many ha)

In the end I used:

class HMIBMemberPage_Controller extends Page_Controller {

   public static $allowed_actions = array (
   );

   public function init() {
      parent::init();
   }
   public function MyHMIBMembers() {
      $data = DataObject::get("HMIBMember")->sort('Discipline ASC');
      return $data;
   }

}


............template.ss

<% loop MyHMIBMembers %>
<li class="hmibmembers">
             <% if MemberPicture %><div id="memberpic">$MemberPicture.SetHeight(160)</div><% end_if %>
             <h2>$Name</h2>
             <% if Website %><a href="$Website.URL" target="_blank">Website</a><% end_if %>
             <h3>$Discipline</h3>
             <p>$Description</p>
          </li>
       <% end_loop %>

I couldn't find an entire example telling me exactly where to put it, I need more practise but when people post code they assume you know and I didn't.

What does this do?

$dataList ? false;

Now all I need to do is add a filter function so my results can be filtered by Discipline, any ideas how to achieve this on the front end via the user?

Avatar
RuthAdele

Community Member, 19 Posts

27 April 2013 at 8:25pm

No problem :) I never get replies either, but I was still subscribed to this thread, so I got a notification email about it :)

So the line:

return $dataList : $dataList ? false;

is shorthand code for:

if ($dataList) {
return $dataList;
}
else {
return false;
}

That way the function just returns false if there are no results from the DataList::create query.

On your filter question, do you mean that you want the user to be able to filter the results based on Discipline? Depending on how many items there are, you could do this with a form, or you could just use css/jquery to show/hide each disclipline section.
I would use the first is there were (or will be) lots of results, and I would use the second if there was only going to be a small number.
Let me know how you want to do it and I'll see if I can help :)

Go to Top