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');
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# Position of new item row

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)
1
2

# Limitation when using array datasource

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();
};
1
2
3
4