Jump to:

23001 Posts in 11702 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Custom sort of DataObjects

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

Page: 1
Go to End
Author Topic: 644 Views
  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Custom sort of DataObjects Link to this post

    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

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Custom sort of DataObjects Link to this post

    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

  • DesignerX.com.au
    Avatar
    Community Member
    107 Posts

    Re: Custom sort of DataObjects Link to this post

    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")
    );
    //....
    }

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