Id Name Inserted Inserted 2 Status Status2
-
-
1 6r0xliyalt 6. 2. 2015 6. 2. 2015 Online Online
2 ozl4gy9udu 6. 2. 2015 6. 2. 2015 Standby Standby
3 9i20xsrese 6. 2. 2015 6. 2. 2015 Offline Offline
4 yk95ak88ra 6. 2. 2015 6. 2. 2015 Offline Offline
5 2loxs05egs 6. 2. 2015 6. 2. 2015 Online Online
6 wzrdlu2o0v 6. 2. 2015 6. 2. 2015 Offline Offline
7 401nd5xv80 6. 2. 2015 6. 2. 2015 Online Online
8 qeba0t2ovv 6. 2. 2015 6. 2. 2015 Offline Offline
9 el25id2emd 6. 2. 2015 6. 2. 2015 Online Online
10 vmkppicf1z 6. 2. 2015 6. 2. 2015 Offline Offline
( Items: 1 - 10 from 200 )

Show me the code of this datagrid

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

	$grid->setDataSource($this->ndb->table('ublaboo_example'));

	/**
	 * Columns
	 */
	$grid->addColumnNumber('id2', 'Id', 'id')
		->setSortable()
		->setFilterRange();

	$grid->addColumnText('name', 'Name')
		->setSortable()
		->setFilterText(['name', 'id', 'inserted']);

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

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

	$grid->addColumnText('status', 'Status')
		->setReplacement([
			0 => 'Offline',
			1 => 'Online',
			2 => 'Standby'
		]
	)->setFilterSelect([
		'' => 'All',
		0 => 'Offline',
		1 => 'Online',
		2 => 'Standby'
	]);

	$grid->addColumnText('status2', 'Status2', 'status')
		->setReplacement([0 => 'Offline', 1 => 'Online', 2 => 'Standby']
	)->setFilterMultiSelect([0 => 'Offline', 1 => 'Online', 2 => 'Standby']);

	return $grid;
}

Filter

::addColumn()->setFilter()

You can either create filters separately: $grid->addFilterText(...) or directly when defining a column: $grid->addColumnText(...)->setFilterText(...).

Api

There are several filter classes and they all have some common behaviour and properties. Let's start with parameters, I will take a FiterText as an example.

/**
 * $key, $name, $columns
 */
$grid->addFilterText('name', 'Name');

/**
 * Equivalent
 */
$grid->addFilterText('name', 'Name', 'name');

/**
 * Same functionality - search in column 'name'
 */
$grid->addFilterText('x_foo', 'Name', 'name');

FilterText is a little bit different than other filters. It can search in multiple columns. Here's how you do that:

$grid->addFilterText('name', 'Search', ['name', 'surname', 'company', 'address']);

Placeholder

$grid->addFilterText('all', 'Search:', ['name', 'id'])
	->setPlaceholder('Search...');

Custom where condition

$grid->addFilterText('custom', 'Custom search:', 'name')
	->setCondition(function($fluent, $value) {
		/**
		 * The data source is here DibiFluent
		 * No matter what data source you are using,
		 * prepared data source will be passed as the first parameter of your callback function
		 */
		$fluent->where('id > ?', strlen($value));
	});

Templates:

Filters can also have their own templates:

$grid->addFilterText('name', 'Name:')
	->setTemplate(__DIR__ . '/templates/filter_name.latte');

There is how the default FilterText template looks like:

{**
 * @param Filter                         $filter
 * @param Nette\Forms\Controls\TextInput $input
 *}

<div class="row">
	{label $input class =>; 'col-sm-3 control-label' /}
	<div class="col-sm-9">
		{input $input, class =>; 'form-control input-sm', data-autosubmit => TRUE}
	</div>
</div>

Filter blocks

User can define filter template via {block} macro:

$grid->setTemplateFile(__DIR__ . '/my_grid.latte');
$grid->addFilterText('name', 'Name:');

And the my_grid.latte:

{block filter-name}
	{input $input}
{/block}

Arguments $filter (Filter class instance), $input (filter form input) and $outer (TRUE or FALSE) are passed to the block. You can inspire yourself with native filter templates, for example text filter template can be found int vendor/ublaboo/datagrid/src/templates/datagrid_filter_text.latte.

Filter type blocks

Macro {block filtertype-text} also defines template, but in that case case for all filters of type text. Same way you can define template for filter of any type (select, multi-select, date, etc).

Removing filter

You can remove filter from grid like so:

$grid->addFilterText('foo', 'Name', 'name');
$grid->removeFilter('foo');

FilterText

By default, when you type "foo bar", FilterText will split input words into n single phrases (... OR (<column> LIKE "%foo%") OR (<column> LIKE "%bar%")). That behaviour can be overridden:

$grid->addFilterText('name', 'Name')
	->setSplitWordsSearch(FALSE);

FilterSelect

FilterSelect has one more parameter - options:

$grid->addFilterSelect('status', 'Status:', ['' => 'All', 1 => 'On', 2 => 'Off']);

/**
 * Equivalent
 */
$grid->addFilterSelect('status', 'Status:', ['' => 'All', 1 => 'On', 2 => 'Off'], 'status');

Again, you can use custom condition callback, the same in all other filters.

FilterMultiSelect

Api of FilterMultiSelect is the same as of FilterSelect

$grid->addFilterMultiSelect('status', 'Status:', [1 => 'On', 2 => 'Off', 2 => 'Another option']);

Keep in mind that FilterMultiSelect uses bootstrap-select JS library. Read more on homepage.

FilterDate

$grid->addFilterDate('created', 'User registerd on');

This filter also has some special features. First, it shows datepicker. Second, You can set date format. Sadly, JavaScript has different date formatting modifiers, so you have to set them both at once:

/**
 * This is default formatting
 * $php_format, $js_format
 */
$grid->addFilterDate('created', 'User registerd on')
	->setFormat('j. n. Y', 'd. m. yyyy');

FilterRange

This filter renders two inputs: From and To. If you want to set inputs placeholders, you have to set both in an array.

$grid->addFilterRange('price_range', 'Price:', 'price');

FilterDateRange

FilterDateRange is similar:

$grid->addFilterDateRange('date_created', 'User registered:');

Default filter values

Datagrid filters can have contain default filter values. Once user changes the filter, default values are not longer applied to the filter. Example usage:

$grid->setDefaultFilter(['status' => 1, 'name' => 'Joe']);

Notice! Values of FilterRange, FilterDateRange and FilterMultiSelect must be of type array:

$grid->addFilterMultiSelect('status', 'Status:', [
	0 => 'Offline',
	1 => 'Online',
	2 => 'Standby'
]);

$grid->addFilterRange('age', 'Age');

$grid->setDefaultFilter(['status' => [1], 'age' => ['from' => 18]]);

Resetting filter to default values

By default, once you reset the filter, default fitler values are applied. If you don't want to apply them after resetting the filter, pass FALSE as a second parameter to DataGrid::setDefaultFilter():

$grid->setDefaultFilter('id' => 10, FALSE);

Filters rendering

Note that if you are rendering filter in datagrid table, you have to choose identical keys for column and filter:

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

/**
 * This filter won't show up, because it has different key name
 */
$grid->addFilterText('search', 'Name', 'name');

Outer filters rendering

You can set outer filters rendering:

$grid->setOuterFilterRendering(); // - that is TRUE. Or $grid->setOuterFilterRendering(FALSE);
Id Name Inserted Inserted 2 Status2 Status
1 6r0xliyalt 6. 2. 2015 6. 2. 2015 Online Online
2 ozl4gy9udu 6. 2. 2015 6. 2. 2015 Standby Standby
3 9i20xsrese 6. 2. 2015 6. 2. 2015 Offline Offline
4 yk95ak88ra 6. 2. 2015 6. 2. 2015 Offline Offline
5 2loxs05egs 6. 2. 2015 6. 2. 2015 Online Online
6 wzrdlu2o0v 6. 2. 2015 6. 2. 2015 Offline Offline
7 401nd5xv80 6. 2. 2015 6. 2. 2015 Online Online
8 qeba0t2ovv 6. 2. 2015 6. 2. 2015 Offline Offline
9 el25id2emd 6. 2. 2015 6. 2. 2015 Online Online
10 vmkppicf1z 6. 2. 2015 6. 2. 2015 Offline Offline
( Items: 1 - 10 from 200 )

Session - remeber state

Grid refreshes its state on several levels. One could be session. It is by defaut turned on, but can be disabled:

$grid->setRememberState(FALSE); // Or turned on again: $grid->setRememberState(TRUE);

Session - filters / filter values changed

When you set some filters and user do some filtering, values are stored in session. After that, when filters are changed (maybe some select options are removed, etc), datagrid would throw an exception, because it can not find particular filters / filter values that are still stored in session. You can supress those exception:

$grid->setStrictSessionFilterValues(FALSE);

Go ahead, try it! Do some filtering, maybe paginating on both datagrids on that page and then just delete the query path in your browser and hit enter! What happens? Nothing, previous states are loaded into both grids.

URL refreshing - history API

Second, grid refreshes URL via history API. So when you refresh, there is always current url. That can be also disabled:

$grid->setRefreshUrl(FALSE); // Or enabled again: $grid->setRefreshUrl(TRUE);

Auto submit

DataGrid filter is submitted automatically after keypress (there is of course a little delay). If you want to desable that feature and use customizable submit button insted, use this code:

$grid->setAutoSubmit(FALSE);
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