如何在动态表更新后刷新整个表排序缓存

How can I refresh the entire table sorting cache after dynamic table update?

本文关键字:排序 缓存 刷新 更新 动态      更新时间:2024-04-19

我正在使用JoeQuery的Stupid TablejQuery插件对一个简单的表进行排序。我不知道如何刷新动态表上的整个排序缓存。

updateSortVal函数允许更新单个小区。例如:

$age_td.updateSortVal(23);

但是,当整个theadtbody被替换时,我如何刷新整个表?

这是我的代码:

$.ajax({
    ...
    success: function(data) {
        $("#myTable thead, #myTable tbody").empty();
        if (data.data.length > 0) {
            var $thead_tr;
            var $tbody_tr;
            for (var r = 0; r < data.data.length; r++) {
                if (r == 0) $thead_tr = $("<tr>").appendTo("#myTable thead");
                    $tbody_tr = $("<tr>").appendTo("#myTable tbody");
                    for (var c in data.data[r]) {
                        if (r == 0) $("<th>").html(c).appendTo($thead_tr);
                        $("<td>").html(data.data[r][c]).appendTo($tbody_tr);
                        // I've tried adding `.updateSortVal(data.data[r][c]);`
                        // to the above as well, but the cache for the `th`
                        // needs to be re-cached as well, so let's do it all at once
                    }
                }
                // I'd like to refresh the entire stupidtable cache here
                $("#myTable").stupidtable().show();
            }
        }
    }    
});

也许你应该基本上删除你的表,而不是清空它,然后重新创建它以重置合适的(例如width replaceWith())

$("#myTable").replaceWith('<table id="myTable"><thead></thead><tbody></tbody></table>');

而不是

$("#myTable thead, #myTable tbody").empty();

编辑

由于我不确定你所说的缓存是什么,所以我做了这个小提琴。如果你重复你的问题,你能检查一下并向我解释吗?

Fiddle演示