0

我在使用 ajax 回调保存 DataTable 的状态时遇到问题,其中状态取自数据库而不是 localStorage。当我做console.log一次stateLoadCallback得到正确的值时。但是当我做console.log两次stateSaveCallbackconsole.log。下面是代码:

var initDataTable = function (selector) {
    $.when(App.availableViewsXhr).done(function () {
        $.ajax({
            url: "api/v1.0/views/" + App.activeView + "/metadata",
            type: "GET"
        })
                .done(function (data) {
                    if (App.dataTable) {
                        App.dataTable.destroy();
                        $(selector).html('');
                    }
                    appendHeaderAndFooter(selector, data.columns);
                    var dom = "<'row'<'col-sm-7'AEOBF><'col-sm-5'f>>";
                    dom += "<'row'<'#notice.col-sm-12'>>";
                    dom += "<'row'<'col-sm-12'tr>>";
                    dom += "<'row' <'col-sm-12'lip>>";
                    App.dataTable = $(selector).DataTable({
                        "lengthMenu": [[25, 50, 100], ['25', '50', '100']],
                        "dom": dom,
                        "buttons": [
                            {
                                className: 'btn-sm',
                                extend: 'colvis',
                                columns: getHideableColumnNumbers(data.columns)
                            }
                        ],
                        "stateSave": true,
                        "colReorder": {
                            "realtime": false,
                            "fixedColumnsLeft": data.config.colReorder.fixedColumnsLeft
                        },
                        "order": [[getFirstOrderableColumnNumber(data.columns), "desc"]],
                        "columns": getColumns(data.columns, data.config),
                        "serverSide": true,
                        "ajax": {
                            "url": 'api/v1.0/views/' + App.activeView,
                            "type": "POST",
                            "complete": function () {
                                App.dataTable.columns.adjust();
                            }
                        },
                        "scrollX": true,
                        stateSaveCallback: function (settings, data) {
                            console.log('save');
                        },
                        stateLoadCallback: function (settings) {
                            console.log('load');
                        }
                    });
                });
    });
};

这样他们就不会被不必要地调用两次 ajax 保存回调。我不明白为什么会这样。

4

0 回答 0