I am using stateSave: true in my DataTable setup. How to prevent search term from saving.
I am using stateSave: true in my DataTable setup. How to prevent search term from saving.
I am using a pretty vanilla datatable setup and it the config I have stateSave: true
. It works to save the state like column visibility, column order, and search term between page loads. But I would like for the user to have the option of NOT saving the search term between visits, but still have the other state items like column visibility still work. Given this:
$('#my_table').dataTable( {
stateSave: true,
stateDuration: 0,
dom: '<"toolbar">Bfrtip',
colReorder: true,
buttons: [
{ extend: 'colvis', position: 'dropdown', columns: ':not(.noVis)',
className: "btn btn-xs btn-brand dropdown-toggle", dataToggle: "dropdown", ariaExpanded: "false"},
{ extend: 'copy', className: 'btn btn-xs btn-brand' },
{ extend: 'print', className: 'btn btn-xs btn-brand' },
{ extend: 'csv', className: 'btn btn-xs btn-brand' },
{ extend: 'excel', className: 'btn btn-xs btn-brand' }
],
inputType: 'search',
name: 'search',
"oLanguage": {
"sSearch": "Filter: "
},
search: {
search: '<%= raw(@filter) %>'
},
"order": [[ 1, "asc" ]],
"columnDefs": [
{"targets": [0], "orderDataType": "dom-checkbox"},
{"targets": [0], "className": "noVis"}
],
scrollY: 550,
scrollX: true,
scrollCollapse: true,
paging: false,
fixedHeader: {
header: true,
footer: true
}
} );
I've read all of the documentation for state.save and I can't figure out a way to add a checkbox that would cause the search term to be saved in the state, but if the box is not checked, it doesn't save the search term (but preserves the rest of the state).
This question has an accepted answers - jump to answer
Answers
There's a couple of ways of doing that with
stateLoadParams
andstateSaveParams
- you can either not save the search, or not load it back in!This example demonstrates the not-saving approach:
Colin
Thank you, that makes total sense. This is a Rails app and I actually have other links that go to this page that pass a search term in and I'm able to do that. So I could use this for "empty by default" and if someone wants to save a search term between visits I can add a checkbox that grabs the term and passes it to the server.