Skip to content

Commit

Permalink
Add a sorting on before write example.
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
ajshort committed Feb 13, 2014
1 parent 5eec0f7 commit 796bd0a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
```

0 comments on commit 796bd0a

Please sign in to comment.