Table of contents
Since version 3.3.0
there is a feature "inline adding" available. Up above is a demo where you can try that out. Just hit the "plus" button, fill some inputs and save the container. Example implementation:
$grid->addInlineAdd()
->onControlAdd[] = function(Nette\Forms\Container $container) {
$container->addText('id', '')->setAttribute('readonly');
$container->addText('name', '');
$container->addText('inserted', '');
$container->addText('link', '');
};
$grid->getInlineAdd()->onSubmit[] = function(Nette\Utils\ArrayHash $values): void {
$v = '';
foreach($values as $key => $value) {
$v . ="$key: $value, ";
}
$v = trim($v,', ');
$this->flashMessage("Record with values [$v] was added! (not really)", 'success');
$this->redrawControl('flashes');
};
As you can see, new item row is rendered at the bottom of the table. You may change that and make datagrid render the new item row on the top:
$grid->addInlineAdd()
->setPositionTop(); // Or take it down again: ::setPositionTop(false)
When you use array datasource, there is one limitation. Simply redrawing the grid won't do. You will also have to set the datasource again to refresh the data.
$grid->getInlineAdd()->onSubmit[] = function(ArrayHash $values) use ($grid): void {
$grid->setDatasource($this->model->getDatasource());
$this->redrawControl();
};