SilverStripe - Set Default Sort for Summary Grid in Admin Model
Asked Answered
B

1

5

I have an admin model tab (called "Appointments") for a SilverStripe that stored form submissions. Everything is working fine and the data is displaying inside the summary grid. However, I need to find a way to change the default sorting of the data. Right now, entries are listed oldest to newest but I need to make it so the newest is always first.

Looking at the mysql table, I see there is a Created column and LastEdited column by default. I'd like to somehow make use of the Created column so I can set a custom sort to override what the default is, but I am not sure how to do this. I've never tried to override the default sorting method for an admin model's sumimmary grid.

Here is the code for the Appointment class::

<?php
class Appointment extends DataObject {

    private static $db = array(
        'Name' => 'varchar',
        'Email' => 'varchar',
        'Phone' => 'varchar',
        'Message' => 'HTMLText',
    );

    private static $summary_fields = array(
        'Name',
        'Email',
        'Phone',
        'Message',
    );

    private static $field_labels = array(
        'Name' => 'Name',
        'Email' => 'Email',
        'Phone' => 'Phone',
        'Message' => 'Message',
    );

}
Bethought answered 6/1, 2016 at 21:20 Comment(0)
B
8

In SilverStripe we can set the default sort by setting the class $default_sort variable:

class Appointment extends DataObject {

    // ...

    private static $default_sort = 'Created DESC';
}

An alternative way to do this is to set the value through a yml file:

Appointment:
  default_sort: 'Created DESC'
Bellbottoms answered 6/1, 2016 at 22:31 Comment(1)
By setting in config you can also overwrite the $default_sort of other module's code without touching it.Baneful

© 2022 - 2024 — McMap. All rights reserved.