Skip to content

Instantly share code, notes, and snippets.

@sminnee
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/fe1d8f27da25226214d7 to your computer and use it in GitHub Desktop.
Save sminnee/fe1d8f27da25226214d7 to your computer and use it in GitHub Desktop.
2nd GridField example
<?php
/**
* Add an OptionSetField of sort options
*/
class GridFieldSortOptionSet implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider {
/**
* Returns the optionset in the header
*/
public function getHTMLFragments($gridField) {
$buttons ="Sort by:";
$buttons .= GridField_FormAction::create(
$gridField, 'SetSort', 'Custom order', 'SetSort', array('SortColumn' => ''))->Field();
$buttons .= GridField_FormAction::create(
$gridField, 'SetSort', 'Start date', 'SetSort', array('SortColumn' => 'StartDate'))->Field();
$buttons .= GridField_FormAction::create(
$gridField, 'SetSort', 'Name', 'SetSort', array('SortColumn' => 'Name'))->Field();
return array(
'toolbar-header-right' => $buttons,
);
}
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
if($column = $gridField->State->GridFieldSortOptionSet->SortColumn) {
$gridField->getConfig()->removeComponentsByType('GridFieldSortableRows');
return $dataList->sort($column);
} else {
return $dataList;
}
}
public function getActions($gridField) {
return array('SetSort');
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
$gridField->State->GridFieldSortOptionSet->SortColumn = $arguments['SortColumn'];
}
}

First, add GridFieldOptionSet.php to your project, and get it working on the Job list in the client detail view.

You will need to work out yourself how to add the component to the GridField.

Challenges

  1. Add a configuration method, setComponentsToDisableOnSort(), that can be given a list of components to disable instead of being hard-coded to GridFieldSortableRows.

  2. Switch from the undefinedoffset module to the grid sorter provided in silverstripe-australia/gridfieldextensions. Use (1) to preserve the current functionality.

  3. Update the module to list a number of buttons for each sortable column, instead of hard-coding. The following methods might help:

    $gridField->getColumns(); 
    $gridField->getColumnMetadata($columnField);
    $gridField->getList();
    $dataList->canSortBy($columnField));
    
  4. Make the button sort on reverse order after the second click.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment