I think I found a bug in the stateSave

I think I found a bug in the stateSave

mmcnair80mmcnair80 Posts: 83Questions: 21Answers: 7

I have a lot of users that use the report website that I created. There are around 30 reports. Most of them use DataTables to format and filter the tables. I have stateSave set to true. The problem that I'm having is that if more than one user is using the same report the filters that one of them uses gets saved for all the users.
Here is how I setup my DataTables:

    $(document).ready(function ()
    {
        // Setup - add a text input to each footer cell
        $('#DataTable tfoot th').each(function ()           //creates the search bar as the footer
        {
            var title = $(this).text();
            $(this).html('<input type="text" placeholder="Search ' + title + '" />');
        });

        var table = $('#DataTable').DataTable({
            "lengthMenu": [[25, 50, 75, 100, 150, -1], [25, 50, 75, 100, 150, 'All']],
            "dom": '<"top"Bifpl<"clear">>rt<"bottom"ip<"clear">>',
            "buttons": [{
                extend: 'collection',
                text: 'Export',
                buttons: ['export', { extend: 'csv',
                    text: 'Export All To CSV',              //Export all to CSV file
                    action: function (e, dt, node, config)
                    {
                        window.location.href = './ServerSide.php?ExportToCSV=Yes';
                    }
                }, 'csv', 'pdf', { extend: 'excel',
                    text: 'Export Current Page',            //Export to Excel only the current page and highlight the first row as headers
                    exportOptions: {
                        modifier: {
                            page: 'current'
                        }
                    },
                    customize: function (xlsx)
                    {
                        var sheet = xlsx.xl.worksheets['sheet1.xml'];
                        $('row:first c', sheet).attr('s', '7');
                    }
                }]
            }
            ],
            "fixedHeader": {                                //Keeps the header and footer visiable at all times
                header: true,
                footer: true
            },
            "select": true,                                 //sets the ability to select rows
            "processing": true,                             //shows the "Processing" when working
            "serverSide": true,                             //sends data to the server for processing
            "ajax": {                                       //where the data is sent and processed
                "url": "./ServerSide.php",
                "type": "POST"
            },
            stateSave: true,                                //Saves the current state of the page
            columnDefs: [{ visible: false, targets: 0}],    //Hides the first column the ID column
            initComplete: function ()                       //sets the search
            {
                var api = this.api();

                // Apply the search
                api.columns().every(function ()
                {
                    var that = this;

                    $('input', this.footer()).on('keyup change', function (e)
                    {
                        if (that.search() !== this.value & e.keyCode == 13) //you have to hit enter for the search to start
                        {
                            that
                              .search(this.value)
                              .draw();
                        }
                    });
                });
            }
        });
    });

    $.fn.dataTable.ext.buttons.export =
    {
        className: 'buttons-alert',                         //Adds the "Export all to Excel" button
        id: 'ExportButton',
        text: "Export All To Excel",
        action: function (e, dt, node, config)
        {
            window.location.href = './ServerSide.php?ExportToExcel=Yes';
        }
    };

I am pretty sure that if I was set stateSave to false (or just not include it) then it would not keep one persons filters. I've not been able to test this and get definative results.

Is there something that I am not doing correctly to cause this or have I stumbled upon a bug?

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    DataTables' state saving uses localStorage (open your browser's inspector and type localStorage into the console and you'll be able to inspect it).

    Unless your users are all sharing the same computer and browser, that shouldn't be possible with the default state saving. Is it possibly that something is overriding it (I don't see that in your code above).

    Can you link to a test case showing the issue?

    Allan

This discussion has been closed.