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

14 Posts   2378 Views


10 October 2012 at 4:05am (Last edited: 10 October 2012 4:05am), Community Member, 44 Posts

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');


11 October 2012 at 2:23pm Community Member, 19 Posts

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


11 October 2012 at 2:53pm Forum Moderator, 474 Posts

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


11 October 2012 at 3:47pm Community Member, 19 Posts

Thankyou! Works a treat. :)


25 April 2013 at 11:29pm Community Member, 168 Posts

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..


26 April 2013 at 12:53pm Community Member, 19 Posts

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 :)


27 April 2013 at 12:07am (Last edited: 27 April 2013 12:12am), Community Member, 168 Posts

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() {
   public function MyHMIBMembers() {
      $data = DataObject::get("HMIBMember")->sort('Discipline ASC');
      return $data;


<% loop MyHMIBMembers %>
<li class="hmibmembers">
             <% if MemberPicture %><div id="memberpic">$MemberPicture.SetHeight(160)</div><% end_if %>
             <% if Website %><a href="$Website.URL" target="_blank">Website</a><% end_if %>
       <% 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?


27 April 2013 at 8:25pm Community Member, 19 Posts

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