未通过stateLoadCallback从已保存状态恢复列可见性

时间:2015-12-05 18:15:02

标签: datatables

我添加了“列可见性”按钮,以选择显示或隐藏某些列。我将状态保存在数据库中,通过单击按钮调用stateSaveCallback函数。

我找不到关于以这种方式检索数据的文档,所以我只是链接到页面并传递变量以从数据库中获取数据,然后使用stateLoadCallback加载它。

现在所有这一切都正常,除了列可见性没有恢复。它是在返回的JSON数据中。

这是我的完整代码:

$(document).ready(function() {

    $.extend( jQuery.fn.dataTableExt.oSort, {
        "date-uk-pre": function (a){
            return parseInt(moment(a, "DD/MM/YYYY").format("X"), 10);
        },
        "date-uk-asc": function (a, b) {
            return a - b;
        },
        "date-uk-desc": function (a, b) {
            return b - a;
        }
    });



    var edit_date_col_num = $('th:contains("Edit Date")').index();
    var entry_date_col_num = $('th:contains("Entry Date")').index();

    var table = $('.mainTable').DataTable( {

        pageLength: 50,
        colReorder: true,
        stateSave: true,

        columnDefs: [
            { "type": "date-uk", targets: [ edit_date_col_num, entry_date_col_num ] }
        ],

        dom: 'Blfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'print',
            {
                extend: 'colvis',
                collectionLayout: 'fixed four-column',
                postfixButtons: [ 'colvisRestore' ]
            }
        ],



        <?php
        $id = $this->input->get('id');
        $action = $this->input->get('action');
        if(isset($action) && $action == 'load' && isset($id) && $id != '') :
        ?>
        "stateLoadCallback": function (settings) {
            var o;

            // Send an Ajax request to the server to get the data. Note that
            // this is a synchronous request since the data is expected back from the
            // function
            $.ajax( {
                "url": EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=state_save&action=load&id=<?php echo $id;?>",
                "async": false,
                "dataType": "json",
                "success": function (response) {
                    response = JSON.parse(response);
                    o = response;
                }
            });

            return o;
        },
        <?php
        endif;
        ?>

        initComplete: function (settings) {
            this.api().columns().every( function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );

            // Need to re-apply the selection to the select dropdowns
            var cols = settings.aoPreSearchCols;
            for (var i = 0; i < cols.length; i++)
            {
                var value = cols[i].sSearch;
                if (value.length > 0)
                {
                    value = value.replace("^", "").replace("$","");
                    console.log(value);
                    $("tfoot select").eq(i).val(value);
                }
            }

        },

    } );


    // Save a datatables state by clicking the save button
    $( ".save_state" ).click(function(e) {
        e.preventDefault();

        table.destroy();
        $('.mainTable').DataTable( {

            colReorder: true,
            stateSave: true,

            "stateSaveCallback": function (settings, data) {

                var save_name = $('.save_name').val();

                // Send an Ajax request to the server with the state object
                $.ajax( {
                    "url": EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=state_save&action=save&save_name="+save_name,
                    "data": data,
                    "dataType": "json",
                    "type": "POST",
                    "success": function (response)
                    {
                        //console.log(response);
                    }
                } );

            },


        });


        //table.state.save();
        window.location.replace(EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=applications");
    });


    $( ".clear_state" ).click(function(e) {
        e.preventDefault();
        table.state.clear();
        window.location.replace(EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=applications");
    });


} );

这是保存的JSON,在开头有几个可见的false(加载后可见):

{"time":"1449338856556","start":"0","length":"50","order":[["0","asc"]],"search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"},"columns":[{"visible":"false","search":{"search":"","smart":"false","regex":"true","caseInsensitive":"true"}},{"visible":"false","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"false","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"false","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"false","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":"false","caseInsensitive":"true"}}],"ColReorder":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70"]}

由于

2 个答案:

答案 0 :(得分:1)

在我的情况下,datatables根据“stateDuration”和“time”属性拒绝旧数据。

解决方案:忽略状态持续时间

   "stateSave": true,
   "stateDuration": -1,

以上案例: “可见”:“false”可能应该是“可见的”: false

答案 1 :(得分:0)

经过一段时间的调试,我自己在这里对我有用..

这个问题是JSON中的所有值都是字符串,并且它们需要具有正确的数据表插件数据类型。

在“stateSaveCallback”ajax请求中保存你的状态我对json字符串执行了以下操作,然后它正确保存了所有值,然后加载了状态。

"stateSaveCallback": function (settings, data) {

    var save_name = $('.save_name').val();

    // Send an Ajax request to the server with the state object
    $.ajax( {
        "url": EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=state_save&action=save&save_name="+save_name,
        //"data": data,
        "data": JSON.stringify(data), // change to this..
        "dataType": "json",
        "type": "POST",
        "success": function (response)
        {
            //console.log(response);
        }
    } );

},