DatatablesBundle icon indicating copy to clipboard operation
DatatablesBundle copied to clipboard

initial_search does not work together with state_save = true

Open knallcharge opened this issue 4 years ago • 1 comments

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.

knallcharge avatar Jul 23 '20 07:07 knallcharge

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".

knallcharge avatar Jul 23 '20 08:07 knallcharge