Id Name Link Inserted Img Action
-
-
One 6r0xliyalt 6. 2. 2015 img-1 Blahblah
2.00 ozl4gy9udu 6. 2. 2015 img-2 Blahblah
3.00 9i20xsrese 6. 2. 2015 img-3 Blahblah
4.00 yk95ak88ra 6. 2. 2015 img-4 Blahblah
Five 2loxs05egs 6. 2. 2015 img-5 Blahblah
6.00 wzrdlu2o0v 6. 2. 2015 img-6 Blahblah
7.00 401nd5xv80 6. 2. 2015 img-7 Blahblah
8.00 qeba0t2ovv 6. 2. 2015 img-8 Blahblah
9.00 el25id2emd 6. 2. 2015 img-9 Blahblah
Ten vmkppicf1z 6. 2. 2015 img-10 Blahblah
( Items: 1 - 10 from 200 )

Show me the code of this datagrid

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

	$grid->setSortable();

	$exampleRowRepository = $this->entityManager->getRepository(ExampleRow::class);
	$grid->setDataSource(
		$exampleRowRepository->createQueryBuilder('er')
			/*->addSelect('a')
			->leftJoin('er.ages', 'a')*/
	);

	/**
	 * Columns
	 */
	$grid->addColumnNumber('id', 'Id')
		->setSortable()
		->setFormat(2)
		->setAlign('left')
		->setReplacement([1 => 'One', 5 => 'Five', 10 => 'Ten']);

	/*$grid->addColumnText('surname', 'Surname', 'age.surname')
		->setSortable('a.surname');*/

	$grid->addColumnText('name', 'Name')
		->setSortable();

	$grid->addColumnLink('link', 'Link', 'this#demo', 'name', ['id', 'surname' => 'name']);

	$grid->addColumnDateTime('inserted', 'Inserted');

	$grid->addColumnText('img', 'Img')
		->setTemplate(__DIR__ . '/templates/Datagrid/grid.img1.latte');

	/**
	 * Filters
	 */
	$grid->addFilterText('name', 'Search', ['name', 'id', 'inserted']);

	$grid->addFilterDateRange('inserted', 'Inserted:');

	$grid->addFilterDateRange('from_to', 'Inserted:');
	$grid->addFilterRange('id', 'Id:');

	/**
	 * ACtions
	 */
	$grid->addAction('blah', 'Blahblah', 'blah!')
		->setClass('btn btn-xs btn-primary ajax');

	$grid->addAction('this', '')
		->setIcon('repeat')
		->setClass('btn btn-xs btn-success');

	$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');
}

Datasources

There are these supported datasources so far:

You can set data source like this:

$grid->setDataSource(this->ndb->table('user')); // NDBT
$grid->setDataSource(this->dibi->select('*')->from('user')); // Dibi
$grid->setDataSource([['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Joe']]); // Array
$grid->setDataSource($exampleRepository->createQueryBuilder('er')); // Doctrine query builder

The primary key column is by default id. You can change that:

$grid->setPrimaryKey('email');

Once you have set a data source, you can add columns to the datagrid.

ORM - Relations

When you are using Doctrine as a datasource (this datagrid can work also with LeanMapper\Entity instances), you can easily access another related entites for rendering in column. Let's say you have an entity User and each instance can have a property $name and $grandma. $grandma is also an instance of User class. Displaying people and their grandmas is very simple then - just use this dot notation:

$grid->addColumnText('name', 'Name', 'name');
$grid->addColumnText('grandma_name', 'Grandma', 'grandma.name');

ApiDataSource

There is also datasource, that takes data from remote api. It is experimental, you can extend it and overwrite whatever you want.

Basic usage:

$grid->setDataSource(
	new Ublaboo\DataGrid\DataSource\ApiDataSource('http://my.remote.api')
);

The idea is simply to forward filtering/sorting/limit/... to remote api. Feel free to leave me a comment if you want to add/improve something.

NextrasDataSource

There is one specific behaviour when using Netras ORM. When custom filter conditions are used, user has to work not with given Collection instance, but with Collection::getQueryBuilder(). That snippet of code will not work correctly, because DbalCollection calls clone on each of it's methods:

$grid->getFilter('name')
	->setCondition(function ($collection, $value) {
		$collection->limitBy(1);
	});

User should use collection's QueryBuilder instead:

$grid->getFilter('name')
	->setCondition(function ($collection, $value) {
		$collection->getQueryBuilder()->andWhere(['name' => $value]);
	});

NetteDatabaseTableDataSource

There is a special feature for NetteDatabaseTableDataSource and referenced/related columns. When you want to reach related column from another table, you can do that using this syntax:

$grid->addColumnText('name', 'Name', ':related_table.name');

For referenced table column, just remove the colon:

$grid->addColumnText('name', 'Name', 'referenced_table.name');

In case you want to specify the "through-column", use following syntax:

$grid->addColumnText('name', 'Name', ':related_table.name:through_column_id');
$grid->addColumnText('name', 'Name', 'referenced_table.name:through_column_id');
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