Skip to content

Latest commit

 

History

History
78 lines (59 loc) · 2.35 KB

export.md

File metadata and controls

78 lines (59 loc) · 2.35 KB

Table of contents

Exports

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')). 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)');

(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
	]);

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', ',');