Jump to:

3432 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sort GridField by Column

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2206 Views
  • svandragt
    Avatar
    Community Member
    44 Posts

    Sort GridField by Column Link to this post

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

  • RuthAdele
    Avatar
    Community Member
    19 Posts

    Re: Sort GridField by Column Link to this post

    I would also like to know this...

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Sort GridField by Column Link to this post

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

  • RuthAdele
    Avatar
    Community Member
    19 Posts

    Re: Sort GridField by Column Link to this post

    Thankyou! Works a treat.

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Sort GridField by Column Link to this post

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

  • RuthAdele
    Avatar
    Community Member
    19 Posts

    Re: Sort GridField by Column Link to this post

    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

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Sort GridField by Column Link to this post

    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?

  • RuthAdele
    Avatar
    Community Member
    19 Posts

    Re: Sort GridField by Column Link to this post

    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

    2206 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.