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

Show me the code of this datagrid

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

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

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

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

	$grid->addColumnText('parent', 'Parent Name', ':ublaboo_example.name:parent_example_id')
		->setSortable();

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

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

	/**
	 * Exports
	 */
	$grid->addExportCallback('Dump to ajax rq', function($data_source, $grid) {
		echo('All fetched data were passed to export callback. Size of data: ');
		echo sizeof($data_source);
		die;
	})->setAjax();

	$grid->addExportCsvFiltered('Csv export (filtered)', 'examples.csv')
		->setTitle('Csv export (filtered)');

	$column_name = new Ublaboo\DataGrid\Column\ColumnText($grid, 'name', 'name', 'Name');
	$column_even = (new Ublaboo\DataGrid\Column\ColumnText($grid, 'even', 'even', 'Even ID (yes/no)'))
		->setRenderer(function($item) { return $item['id'] % 2 ? 'No' : 'Yes'; });

	$grid->addExportCsv('Csv export', 'examples_all.csv')
		->setTitle('Csv export')
		->setColumns([
			$column_name,
			$column_even
		]);

	return $grid;
}

Export

ExportCallback

DataGrid allows you to export the data via $grid->addExportCallback(). The parameters are:

/**
 * $text = Button text
 * $callback = your export callback
 * $filtered = $should datagrid pass a filtered data to your callback, or all?
 */
$grid->addExportCallback($text, $callback, $filtered = FALSE);

You can tell whether to use ajax or not (->setAjax()). Or a button title: ->setTitle('Title').

CSV export

There is already a CSV export implemented (filtered and not filtered):

/**
 * Or $grid->addExportCsvFiltered();
 */
$grid->addExportCsv('Csv export (filtered)', 'examples.csv')
	->setTitle('Csv export (filtered)');

(Not) Using templates in CSV export

ExportCsv ignores column template, because i don't like the idea Latte (tempalting engine for HTML) exporting data for CSV format. Using custom renderer sounds better to me in that case.

Export columns

When you exporting the data, you can have different columns in export and in the datagrid. Or differently rendered. So there is another method Ublaboo\DataGrid\Export\Export::setColumns(). You can create instances of another columns and pass them in array to this method. These will be rendered in export:

$column_name = new Ublaboo\DataGrid\Column\ColumnText($grid, 'name', 'name', 'Name');
$column_even = (new Ublaboo\DataGrid\Column\ColumnText($grid, 'name', 'even', 'Even ID (yes/no)'))
	->setRenderer(function($item) { return $item['id'] % 2 ? 'No' : 'Yes'; });

$grid->addExportCsv('Csv export', 'examples_all.csv')
	->setTitle('Csv export')
	->setColumns([
		$column_name,
		$column_even
	]);

Export encoding, delimiter

By default, DataGrid exports data in utf-8 with semicolon delimiter ;. This can be changed:

/**
 * Defaults:
 * $output_encoding = 'utf-8'
 * $delimiter       = ';'
 */
$grid->addExportCsvFiltered('Csv export (filtered)', 'examples.csv');

/**
 * Changed
 */
$grid->addExportCsv( 'Csv export', 'examples_all.csv', 'windows-1250', ',');
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