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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Custom sort of DataObjects


Reply

3 Posts   751 Views

Avatar
theGermaniac

8 August 2012 at 9:26pm Community Member, 82 Posts

Hey there!

How can I custom sort DataObjects - means how to sort them independent from it's columns. This was possible with the DataObject Manager Module in SS 2.x and it was great! :-)

Thanks. Mario

Avatar
Matty Balaam

9 August 2012 at 3:53am Community Member, 69 Posts

I think it's going to be introduced to the core at a later date, but at the moment you need to install this module:

https://github.com/UndefinedOffset/SortableGridField

Avatar
DesignerX.com.au

9 August 2012 at 4:42am (Last edited: 10 August 2012 7:43pm), Community Member, 107 Posts

Hi:
That module works great .. It took me sometime to get it working so here few things that may save you alot of time :
Example for a basic Gallery module = Gallery has_many GalleryImages
** GalleryImage.php
- Make sure you add field for SortID
Code to use :
// to change the default sorting to the new SortID
public static $default_sort = 'SortID Asc';
// To change the Default Value if SortID to LastSortID+1 , basic auto sort
function onBeforeWrite() {
parent::onBeforeWrite();
if (!$this->SortID) {
return $this->SortID = $this->getNextSortID();
}
}

public function getNextSortID() {
return $this->SGallery()->Images()->Last()->SortID + 1;
}

** Gallery.php
function getCMSFields() {
$fields = parent::getCMSFields();
//......
$gridFieldConfig = GridFieldConfig_RelationEditor::create();
$gridFieldConfig->addComponents(
new GridFieldSortableRows("SortID")
);
//....
}