Column search filters not reloading search query when state save is true

Column search filters not reloading search query when state save is true

2312hannah2312hannah Posts: 1Questions: 1Answers: 0


I've set up a datatable that adds search filters to the column headers and it's working great, but when I introduced stateSave I noticed that if I filtered the table and then went off to another page and returned, the data remained filtered but the column search filters were empty so it looked as though the table just wasn't loading any data.

This is the code I am using to generate the table:

$(document).ready(function () {
// Setup - add a text input to each footer cell
$('#usersTable thead tr')
.appendTo('#usersTable thead');

    var table = $('#usersTable').DataTable({
        lengthChange: false,
        info: false,
        stateSave: true,
        orderCellsTop: true,
        fixedHeader: true,
        columnDefs: [
                    targets: [6],
                    sortable: false,
        initComplete: function () {
            var api = this.api();

            // For each column
                .each(function (colIdx) {
                    if ([6].includes(colIdx)) {
                            var cell = $('.filtersCpdEventsWhole th').eq(
                        } else {

                    // Set the header cell to contain the input element
                    var cell = $('.filters th').eq(
                    var title = $(cell).text();
                    $(cell).html('<input type="text" />');
                    // On every keypress in this input
                        $('.filters th').eq($(api.column(colIdx).header()).index())
                        .off('keyup change')
                        .on('change', function (e) {
                            // Get the search value
                            $(this).attr('title', $(this).val());
                            var regexr = '({search})'; //$(this).parents('th').find('select').val();
                            var cursorPosition = this.selectionStart;
                            // Search the column for that value
                                    this.value != ''
                                        ? regexr.replace('{search}', '(((' + this.value + ')))')
                                        : '',
                                    this.value != '',
                                    this.value == ''
                        .on('keyup', function (e) {

                                .setSelectionRange(cursorPosition, cursorPosition);


  • kthorngrenkthorngren Posts: 16,783Questions: 25Answers: 3,972

    There are lots of threads with this question. See if Allan's example in this thread helps.


Sign In or Register to comment.