Table of contents

# 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);
1
2
3
4
5
6

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

# 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)');
1
2
3
4
5

# (Not) Using templates in CSV export

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

# Export columns

When you're exporting the data, you can have different columns in export and in the datagrid. Or differently rendered. So there is another method Contributte\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 Contributte\Datagrid\Column\ColumnText($grid, 'name', 'name', 'Name');
$column_even = (new Contributte\Datagrid\Column\ColumnText($grid, 'name', 'even', 'Even ID (yes/no)'))
	->setRenderer(function(array $item): string {
		return $item['id'] % 2 ? 'No' : 'Yes';
	});

$grid->addExportCsv('Csv export', 'examples_all.csv')
	->setTitle('Csv export')
	->setColumns([
		$column_name,
		$column_even
	]);
1
2
3
4
5
6
7
8
9
10
11
12

# 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', ',');
1
2
3
4
5
6
7
8
9
10
11