Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Sorting in the list

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

Page: 1
Go to End
Author Topic: 936 Views
  • Victor
    Avatar
    Community Member
    128 Posts

    Sorting in the list Link to this post

    We have a page People.php with children of the type Person.php displaying names, positions etc (so Person.php has fields Name and Position). By design they are sorted by Name.

    How to make "headers" Name and Position clickable so clicking on "Position" sorting would be on Position, and only then by Name and vv?

    I know how to do it in php/sql, but in frames of SilverStripe?

    Thank you. Victor

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Sorting in the list Link to this post

    If you use the DataObjectManger module to manage the Person DataObject it does it out of the box

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Sorting in the list Link to this post

    DataObjectManager does it for editor of CMS, I am talking about viewer

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Sorting in the list Link to this post

    There are a lot of table sorting plugin available for jQuery if you need to handle it in the front then. If you want to do it in the front end with PHP the best way is to create a function that either checks for a URL variable or uses the action/id method and returns the dataobject. You can then have the table heading either call this function or reload the page with the correct URL variable.

    I do something like this on Products that have multiple colors

    function showColor(){
        if(isset($_GET['color'])){
           $colorSelect = $_GET['color'];
           return DataObject::get("Color", "StripedColorName='".$colorSelect."' AND ColorProductID = ".$this->ID);
          }
          else {
             return false;
             
          }
    }


    The colo links on the page just reload the page with ?color=pink tacked on the end. Then in my template I just do a check against showColor and if its there I use a showColor control to render the data. In you case it would even be easier since it the same data just in a different order.

    Silverstripe will needs to let you call the Sort() function form the template as it does for GroupBy().

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Sorting in the list Link to this post

    Could you recommend a sorting plugin (js) and how to install it and to hook to the page?

    Victor

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Sorting in the list Link to this post

    Just download jquery from http://jQuery.com (or call it directly from google in Page.php) and the plugin http://tablesorter.com/ Call both js files from your template or page class and follow the instruction on the tablesorter plugin, its pretty easy just one line of code to implement it.

    If you want to know how to call javascript from your page class just look at the following tutorial on ssbits
    http://www.ssbits.com/using-jquery-for-form-validation/

    936 Views
Page: 1
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.