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.

Customising the CMS /

Sorting in the list


Reply


6 Posts   948 Views

Avatar
Victor

Community Member, 128 Posts

28 January 2010 at 8:51am

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

Avatar
zenmonkey

Community Member, 528 Posts

29 January 2010 at 7:14am

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

Avatar
Victor

Community Member, 128 Posts

29 January 2010 at 11:38pm

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

Avatar
zenmonkey

Community Member, 528 Posts

30 January 2010 at 2:59am

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

Avatar
Victor

Community Member, 128 Posts

30 January 2010 at 3:57am

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

Victor

Avatar
zenmonkey

Community Member, 528 Posts

30 January 2010 at 4:14am

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/