Skip to content

Commit 796bd0a

Browse files
committed
Add a sorting on before write example.
Closes #44
1 parent 5eec0f7 commit 796bd0a

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

docs/en/index.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,21 @@ $grid->getConfig()->addComponent(new GridFieldOrderableRows());
7979
// Specifying the sort field.
8080
$grid->getConfig()->addComponent(new GridFieldOrderableRows('SortField'));
8181
```
82+
83+
By default, when you create a new item, it is created with a sort order of "0" - that is, it is added
84+
to the start of the list. The sort order is only set for the first time when the user reorders the items.
85+
If you wish to append newly created items to the end of the list, use an `onBeforeWrite` hook like:
86+
87+
```php
88+
class Item extends DataObject {
89+
private static $db = array('Sort' => 'Int');
90+
91+
protected function onBeforeWrite() {
92+
if (!$this->Sort) {
93+
$this->Sort = Item::get()->max('Sort') + 1;
94+
}
95+
96+
parent::onBeforeWrite();
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)