Add a sorting on before write example.

Closes #44
This commit is contained in:
Andrew Short 2014-02-14 09:34:13 +11:00
parent 5eec0f70b4
commit 796bd0ad68
1 changed files with 18 additions and 0 deletions

View File

@ -79,3 +79,21 @@ $grid->getConfig()->addComponent(new GridFieldOrderableRows());
// Specifying the sort field.
$grid->getConfig()->addComponent(new GridFieldOrderableRows('SortField'));
```
By default, when you create a new item, it is created with a sort order of "0" - that is, it is added
to the start of the list. The sort order is only set for the first time when the user reorders the items.
If you wish to append newly created items to the end of the list, use an `onBeforeWrite` hook like:
```php
class Item extends DataObject {
private static $db = array('Sort' => 'Int');
protected function onBeforeWrite() {
if (!$this->Sort) {
$this->Sort = Item::get()->max('Sort') + 1;
}
parent::onBeforeWrite();
}
}
```