State save is not working when draw a column in datatable

State save is not working when draw a column in datatable

vishal jaiswalvishal jaiswal Posts: 1Questions: 1Answers: 0

$(document).ready(function () {

    //var url=location.href;
    //localStorage.setItem("url",url);

    var StoredUrl = 'http://localhost/brandtrack/admin/activitymaster/detail';
    var previusurl =  document.referrer;
    var preUrl = previusurl.substring(0, previusurl.indexOf('?'));
    //alert(clean_uri);


    var tb =  $('.dataTables-activity').DataTable({
        stateSave: true,
        responsive: true,
        dom: "<'html5buttons'B>lfrt<'row'<'col-sm-5'i><'col-sm-7'p>>",
        buttons: [

            {extend: 'csv', title: 'Activity_Report',exportOptions: {
                    columns: [ 1,2,3,4,5 ]
                }},
            {extend: 'excel', title: 'Activity_Report',exportOptions: {
                    columns: [ 1,2,3,4,5 ]
                }},
            {extend: 'pdf', title: 'Activity_Report',exportOptions: {
                    columns: [ 1,2,3,4,5 ]
                }},


            {
                extend: 'print',
                customize: function (win) {
                    $(win.document.body).addClass('white-bg');
                    $(win.document.body).css('font-size', '10px');

                    $(win.document.body).find('table')
                        .addClass('compact')
                        .css('font-size', 'inherit');
                }
            }
        ]
    });


    //console.log('ok');
    if(StoredUrl != preUrl){
        tb.state.clear();
        tb.destroy();
        var tb =  $('.dataTables-activity').DataTable({
            /* stateSave: true,*/
            responsive: true,
            dom: "<'html5buttons'B>lfrt<'row'<'col-sm-5'i><'col-sm-7'p>>",
            buttons: [

                {extend: 'csv', title: 'Activity_Report',exportOptions: {
                        columns: [ 1,2,3,4,5 ]
                    }},
                {extend: 'excel', title: 'Activity_Report',exportOptions: {
                        columns: [ 1,2,3,4,5 ]
                    }},
                {extend: 'pdf', title: 'Activity_Report',exportOptions: {
                        columns: [ 1,2,3,4,5 ]
                    }},
                {
                    extend: 'print',
                    customize: function (win) {
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');

                        $(win.document.body).find('table')
                            .addClass('compact')
                            .css('font-size', 'inherit');
                    }
                }
            ]
        });
        //console.log('not ok');
    }

    tb.on( 'order.dt search.dt', function () {
        tb.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } );

});

Answers

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    I don't fully understand the problem, sorry, can you give more information on what you want to see, but what is happening instead?

    One thing to note, you are destroying the table and clearing the state before recreating it without stateSave - so in that second table, you wouldn't expect to see any state saved.

    Cheers,

    Colin

This discussion has been closed.