Search code examples
javascriptphpsilverstripemodeladmin

Show hide DataObject columns within ModelAdmin with javascript


Users have many different preferences for the columns that are shown for the same DataObject in the same ModelAdmin and would like to control them for their own sessions.

Given the following dataobject...

class MyDataObject extends DataObject {
    static $db = array(
        'Name'      => 'Varchar',
        'Date'      => 'SS_DateTime',
        'Number'    => 'Decimal(8,2)',
    );
    public static $summary_fields = array(
        'Name',
        'Date',
        'Number'
    );
}

...and the following ModelAdmin...

class MyModelAdmin extends ModelAdmin {
    static $mangaged_models = array(
    'MyDataObject',
    );  
    static $url_segment = 'mymodeladmin';
    static $menu_title = 'MyModelAdmin';
    static $menu_priority = 9;
}

...is there a straightforward javascript only solution (that I imagine would store the columns chosen in a cookie) or something more programmatic (i.e. php) that would control the columns on a per user basis - allowing each user to select the columns they want and it remembers it from session to session?

Any help is much appreciated.


Solution

  • Like mentioned in the comments, you could have a look at this module, which should provide you with the functionality you'll need.

    https://github.com/smindel/silverstripe-GridFieldAddOns/blob/master/docs/en/GridFieldUserColumns.md

    From the docs:

    Just add the following code to the end of your mysite/_config.php

    Member::add_extension('GridFieldUserColumnsExtension');
    Object::useCustomClass('GridFieldConfig_RecordEditor', 'GridFieldConfig_ExtendedRecordEditor');
    Object::useCustomClass('GridFieldConfig_RelationEditor', 'GridFieldConfig_ExtendedRelationEditor');
    

    Screenshot of the column selector