Name
Name2
Id
Status
6r0xliyalt
ozl4gy9udu
9i20xsrese
yk95ak88ra
2loxs05egs

Show me the code of this datagrid

public function createComponentCategoriesGrid($name)
{
	/**
	 * @var Ublaboo\DataGrid\DataGrid
	 */
	$grid = new DataGrid($this, $name);

	/**
	 * Dibi
	 */
	$join = $this->db->select('COUNT(id) AS count, parent_category_id')
		->from('ublaboo_category')
		->groupBy('parent_category_id');

	$fluent = $this->db
		->select('c.*, c_b.count as has_children')
		->from('ublaboo_category', 'c')
		->leftJoin($join, 'c_b')
			->on('c_b.parent_category_id = c.id')
		->where('c.parent_category_id IS NULL');

	$grid->setDataSource($fluent);
	$grid->setTreeView([$this, 'getChildren'], 'has_children');

	$grid->setSortable();

	$grid->addColumnText('name', 'Name');
	$grid->addColumnText('name2', 'Name2', 'name');
	$grid->addColumnText('id', 'Id')
		->setAlign('center');

	$grid->addColumnStatus('status', 'Status')
		->setAlign('center')
		->addOption(1, 'Online')
			->setClass('btn-success')
			->endOption()
		->addOption(2, 'Standby')
			->setClass('btn-primary')
			->endOption()
		->addOption(0, 'Offline')
			->setClass('btn-danger')
			->endOption()
		->onChange[] = [$this, 'categoriesStatusChange'];

	$grid->addAction('edit', 'Edit', 'edit!')
		->setIcon('pencil')
		->setClass('btn btn-xs btn-default ajax')
		->setTitle('Edit');

	$grid->addAction('delete', '', 'delete!')
		->setIcon('trash')
		->setTitle('Delete')
		->setClass('btn btn-xs btn-danger ajax')
		->setConfirm('Do you really want to delete example %s?', 'name');
}


public function categoriesStatusChange($id, $status)
{
	$this->categoryRepository->changeStatus($id, $status);

	$status_text = ['offline', 'online', 'standby'][$status];

	$this->flashMessage("Status of category [$id] was updated to [$status_text].", 'success');

	if ($this->isAjax()) {
		$this->redrawControl('flashes');
		$this['categoriesGrid']->setDataSource($this->categoryRepository->getAdminSelection('all'));
		$this['categoriesGrid']->redrawItem($id, 'c.id');
	} else {
		$this->redirect('this');
	}
}

Tree view

Api

There is a possibility to render the data as a tree:

/**
 * First parameter is a callback that will return particular children rows for given parent
 * Second is a column name (or callback), that indicates whether the row has some children or not
 */
$grid->setTreeView(callable $getChildrenCallback, string|callable $has_children_column);

Example table structure

Click the link "Show me the code" above. This will show you the code of this datagrid example. And here is a database table structure:

Idparent_category_idnamestatus
1NULL6r0xliyalt0
2NULLozl4gy9udu0
3NULL9i20xsrese1
4NULLyk95ak88ra2
5NULL2loxs05egs0
61wzrdlu2o0v1
71401nd5xv801
81qeba0t2ovv0
91el25id2emd0
101vmkppicf1z1
112yj6icacjk00
123rjqpytdq630
133wkossm2fud0
1434gbik9rxbp0
152jl9ke3q9s72

Redrawing one row (ajax)

Different situation occurs when you need to redraw just one row. DataGrid does not know about all the items (it has originally only top level rows). So before you call DataGrid::redrawItem(), you have to set datagrid datasource where the item will be visible:

public function handleSetCategoryStatus($id, $status)
{
	$this->categoryRepository->changeStatus($id, $status);

	$this->flashMessage("Status of category [$id] was updated to [$status].", 'success');

	$join = $this->db->select('COUNT(id) AS count, parent_category_id')
		->from('ublaboo_category')
		->groupBy('parent_category_id');

	$fluent = $this->db
		->select('c.*, c_b.count as has_children')
		->from('ublaboo_category', 'c')
		->leftJoin($join, 'c_b')
			->on('c_b.parent_category_id = c.id');

	if ($this->isAjax()) {
		$this->redrawControl('flashes');

		$this['categoriesGrid']->setDataSource($fluent);
		$this['categoriesGrid']->redrawItem($id, 'c.id');
	} else {
		$this->redirect('this');
	}
}
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tím souhlasíte. V pořádku Další informace