column.visible does not appear work with sateSave

column.visible does not appear work with sateSave

pwdpwd Posts: 4Questions: 1Answers: 0

I have set the option stateSave to true and for some (one in example) set the columns.visible to false. unless I comment out
the stateSave option the column is still visible. I am using the buttons extension to allow the user to control which columns are
seen. The data is loaded via ajax one time.

If I allow responsive: true everything works (at least it looks like it does) but if I allow the stateSave the "base" column still
shows up (it can be turned off with the 'colvis' button but the colvisRestore puts the base back on).

This is the top of my datatables.js file:
Note DataTables 1.10.12

/*
 * This combined file was created by the DataTables downloader builder:
 *   https://datatables.net/download
 *
 * To rebuild or modify this file with the latest versions of the included
 * software please visit:
 *   https://datatables.net/download/#dt/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.12/af-2.1.2/b-1.2.2/b-colvis
-1.2.2/b-html5-1.2.2/b-print-1.2.2/cr-1.3.2/fc-3.2.2/fh-3.1.2/kt-2.1.3/r-2.1.0/rr-1.1.2/sc-1.4.2/se-1.2.
0
 *
 * Included libraries:
 *   JSZip 2.5.0, pdfmake 0.1.18, DataTables 1.10.12, AutoFill 2.1.2, Buttons 1.2.2, Column visibility 1
.2.2, HTML5 export 1.2.2, Print view 1.2.2, ColReorder 1.3.2, FixedColumns 3.2.2, FixedHeader 3.1.2, Key
Table 2.1.3, Responsive 2.1.0, RowReorder 1.1.2, Scroller 1.4.2, Select 1.2.0
 */

/*!


My example JS code

$(document).ready(function() {
    var table = $('#item-select').DataTable( {
        ajax: '/mbme/ajax/select',
        colReorder: true,
        fixedColumns: true,
        //responsive: true,
        // columns not visible interfears with save state.
        //stateSave: true,
        "order": [[1, 'asc']],
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
        keys: true,
        dom: 'lBfrtip',
        buttons: [
            {
                extend: 'colvis',
                text: 'Columns',
                postfixButtons: [ 
                    {
                        text: 'Restore Default',
                        extend: 'colvisRestore' 
                        }
                    ]
                },
            {
                extend: 'copyHtml5',
                exportOptions: {
                    columns: ':visible'
                    }
                },
            {
                extend: 'csvHtml5',
                exportOptions: {
                    columns: ':visible'
                    }
                },
            // for now PDF is too slow
            //{
            //  extend: 'pdfHtml5',
            //  exportOptions: {
            //      columns: ':visible'
            //      }
            //  },
            {
                extend: 'print',
                exportOptions: {
                    columns: ':visible'
                    }
                }
            ],
        columns: [
            { 
                "visible": true,
                data: "itemid" 
                },
            { 
                "visible": true,
                data: "name" 
                },
            { 
                "visible": true,
                data: "description" 
                },
            { 
                "visible": true,
                data: "type" 
                },
            { 
                "visible": true,
                data: "sell" 
                },
            { 
                "visible": true,
                data: "price" 
                },
            { 
                "visible": false,
                data: "base" 
                },
            ]
        });
    });

Answers

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    Did you enable the state saving before you hide the column? If so, it will have state saved the fact that it was visible!

    Try your page in an incognito window.

    Allan

This discussion has been closed.