繁体   English   中英

Tabulator - ajax,如何恢复表头持久化

[英]Tabulator - ajax, how to restore table header persistence

我正在尝试从本地存储恢复表头顺序,但它不适用于来自 ajax 响应的头。

var table = new Tabulator("#example-table", {
        placeholder: "No Data Available", //display message to user on empty table
        movableColumns: true, //enable user movable columns
        persistence: {
            columns: true,
        },
        ajaxURL: "/ajax/showall", //ajax URL
        persistenceWriterFunc: function (id, type, data) {
            //id - tables persistence id
            //type - type of data being persisted ("sort", "filter", "group", "page" or "columns")
            //data - array or object of data                

            if (Array.isArray(data) && data.length) {
                // array does not exist, is not an array, or is empty
                // ⇒ do not attempt to process array
                console.log('do not save empty array');
                localStorage.setItem(id + "-" + type, JSON.stringify(data));
            };
        },
        persistenceReaderFunc: function (id, type) {
            //id - tables persistence id
            //type - type of data being persisted ("sort", "filter", "group", "page" or "columns")                
            return data ? JSON.parse(data) : false;
        },

        ajaxResponse: function (url, params, response) {
            this.setColumns(response.header);
            return response.data;
        }
    });

有没有办法做到这一点? 我试图使用tableBuilding: function()等,但没有任何效果。 我不想发送 ajax 查询只是为了获取表头。

无需手动执行任何操作,您启用持久性模块,只要您启用了持久列选项,它就会自动为您完成所有这些工作

var table = new Tabulator("#example-table", {
    persistence:{
        columns: true, //persist columns
    }
});

有关更多信息,请参阅持久性文档

每当您的 ajax 响应设置列时,它们都会自动更新本地存储选项。

如果您尝试替换内置存储选项,您只需要使用persistenceWriterFuncpersistenceReaderFunc选项,在这里您似乎不需要这样做

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM