编程排序功能在 Tabulator 中不起作用

Programmatic sort functionality not working in Tabulator

我对 Tabulator 及其编程排序功能有疑问。更具体地说,setSort() 函数在被 tabEndNewRow 方法调用时未按预期工作。

以下是我的Tabulatortable设置代码:

    var table = new Tabulator(divId, {
        height: "100%",
        data: [],
        layout: "fitDataFill",
        tabEndNewRow: function (row) {
            table.setSort([
                { column: "DateComp", dir: "asc" },
                { column: "TimeStart", dir: "asc" }
            ]);
            return { DateComp: currentDate, TimeStart: "6:00 AM", TimeFinish: "6:00 AM" };
        },
        columns: [
            { title: "Date Worked", field: "DateComp", responsive: 0, hozAlign: "center", sorter: "date", editor: dateEditor },
            { title: "Start Time", field: "TimeStart", responsive: 0, hozAlign: "center", sorter: "time", editor: timeEditor },
            { title: "Finish Time", field: "TimeFinish", responsive: 0, hozAlign: "center", sorter: "time", editor: timeEditor }
        ]
    });

每次使用 tabEndNewRow 方法插入新行时,我都会尝试重新排序 Tabulator table。非常感谢任何帮助。

您遇到的问题是因为您正在对 table 进行排序然后返回新行,所以新行总是在排序后应用。

您需要添加一个 setTimeout 来延迟排序,直到添加行之后:

tabEndNewRow: function (row) {
    
    //delay sorting until after the new row is added
    setTimeout(() => {
        table.setSort([
            { column: "DateComp", dir: "asc" },
            { column: "TimeStart", dir: "asc" }
        ]);
    }, 100);

    return { DateComp: currentDate, TimeStart: "6:00 AM", TimeFinish: "6:00 AM" };
},