The grid bundle adds several new features on top of the base grid library.
The output of the boolean column type is localized (in the PrezentGrid
translation domain).
Furthermore, column header labels and action labels are localized as well. By default this uses the main messages
translation
domain, but you can set a custom domain using the label_translation_domain
option.
You can generate URLs from routes for columns and actions. Two new options have been added to the base element type:
The route name to generate an URL from. When the route contains values in braces, they are interpreted as property paths into your row and will be replaced. You can also suply a callback which will recieve the row as it's only parameter.
An array of route parameters. Parameter values which contain braces are interpreted as property paths into your row and will be replaced. You can also suply a callback which will recieve the row as it's only parameter:
Example:
$builder->addAction('edit', [
'route' => 'your_edit_route',
'route_parameters' => [
'id' => '{id}',
'slug' => function ($item) {
return slugify($item->getName());
}
]
]);
Columns can be made sortable by enabling the sortable
option. This turns the column header into a link. Sorting parameters are extracted
from the current request to alternate between ascending and descending sorting.
Note that you need to extract the sorting parameters from the request yourself and apply it to whatever query you are using to fetch the data.
Various new options are added to the base column type to configure the sorting behaviour:
Set to true
to enable the column to be sortable.
Which field should be sorted on. The value of this option is the value of the HTTP request parameter when sorting on this column. defaults to teh column name.
Which route to generate the sorting URL with. defaults to the current route.
Parameters for the sorting route.
The name of the request attribute that specifies which field you should sort on. Defaults to 'sort_by'
.
The name of the request attribute that specifies which direction you should sort in (ASC or DESC). Defaults to 'sort_order'
.
Example usage:
<?php
$builder->addColumn('name', StringType::class, [
'sortable' => true,
'sort_field' => 'name.raw',
]);