DatatablesBundle
DatatablesBundle copied to clipboard
initial_search does not work together with state_save = true
Hi,
setting "initial_search" for a column filter doesn't work if "state_save" is set to "true", filter inputs remain empty. Setting "state_save" to "false" fixes this, but then of course all other settings are lost.
Managed to get a workaround for this by adding a function for state_load_params:
added this to buildDatatable()
$this->callbacks->setStateLoadParams(
[
'template' => 'datatable/_data_callback.js.twig',
'vars' => [
'column' => $searchColumn,
'search' => $searchTerm,
],
]
);
and this as the new template-file "_data_callback.js.twig"
function stateLoaded(settings, data) {
var column = '{{ vars['column'] }}';
var search = '{{ vars['search'] }}';
if (column && search) {
data.columns[column].search.search = search;
}
}
This sets the search-data for the given column to the search-term. Not pretty but fixes the broken "initial_search" when using "state_save = true".