inteve/datagrid

DataGrid component for Nette.

Fund package maintenance!
Other

v1.2.0 2024-01-05 11:35 UTC

This package is auto-updated.

Last update: 2024-11-05 13:19:56 UTC


README

Build Status Downloads this Month Latest Stable Version License

DataGrid component for Nette.

Donate

Installation

Download a latest package or use Composer:

composer require inteve/datagrid

Inteve\Datagrid requires PHP 7.2.0 or later.

Usage

In presenter:

class MyPresenter extends Nette\Application\UI\Presenter
{
	protected function createComponentGrid()
	{
		$datasource = new Inteve\DataGrid\DataSources\LeanMapperQuery($this->repository->queryAll(), $this->mapper);
		$grid = new Inteve\DataGrid\DataGrid($datasource);
		$grid->setTemplateFile(__DIR__ . '/@grid.latte'); // optional
		$grid->setItemsOnPage(20, TRUE); // optional

		$grid->addTextColumn('title', 'Title')
			->setCustomRender(function (Entity\Post $post) {
				$label = Html::el();
				$label->addText($post->title);
				return $label;
			})
			->setSortable();

		$grid->addLinkColumn('url', 'URL');

		$grid->addDateColumn('date', 'Date')
			->setSortable();

		$grid->addNumberColumn('views', 'Views')
			->setSortable()
			->setDecimals(1)
			->setValueProvider(function (Entity\Post $post) {
				return max(1, $post->views);
			});

		$grid->addAction('edit', 'Upravit', $this->lazyLink('edit'));

		$grid->addAction('delete', 'Smazat', $this->lazyLink('delete!'));

		$grid->addTextFilter('title', 'Title');

		$grid->addTextFilter('url', 'URL');

		$grid->setDefaultSort(array(
			'date' => 'DESC',
			'title' => 'ASC',
		));

		return $grid;
	}
}

In template:

{control grid}

License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/