【问题标题】:JQuery tablesorter plugin - update sorting after modified rowsJQuery tablesorter 插件 - 修改行后更新排序
【发布时间】:2017-01-05 15:39:00
【问题描述】:

我使用 tablesorter 2.0,并使用 ajax 更新单元格的值。通话后,我需要再次订购行,但 $('#thisTable').trigger('update') 对我没有帮助。

我处理单元格内的标记,但这不是问题。

 $('#thisTable').tablesorter({
   textExtraction: function(node) {
     return node.getElementsByTagName('input')[0].value; 
   }
 });

任何帮助将不胜感激。

-- 克里

【问题讨论】:

    标签: jquery sorting tablesorter


    【解决方案1】:

    您可以在表格排序器docs 中找到答案。你必须触发另一个事件sorton

    【讨论】:

    • 是的,我读过,但我不知道,更新前对哪一列进行了排序... sorton 需要排序列作为参数。
    • 实际排序列存储在:$('#thisTable').get(0).config.sortList。如果未定义,则可以添加一些默认排序。
    • 谢谢!就是这样,我需要的! :)
    【解决方案2】:

    这是我的代码

    //append some content to the tbody
    $('table').trigger('update');    
    var $sort = $('table').get(0).config.sortList;
    $("table").trigger("sorton",[$sort]); 
    

    在我向表格主体添加一些行之后调用上述内容。我可以看到 $sort 值,但触发器函数没有对新添加的行进行排序。

    【讨论】:

    • 我启用了表格排序器的调试模式。 update 触发器更新表缓存,但是 sorton 触发器正在对先前缓存的数据进行排序。所以新数据没有得到排序。为了让它们同步,我把 $("table").trigger("sorton",[$sort]);在 setTimoutblock 中,延迟为 50 毫秒。但这只是一种解决方法,但我猜这不是正确的解决方法。有人可以建议正确的解决方法吗?
    【解决方案3】:

    我对源代码做了一些小改动。我在更新事件处理程序中添加了一个参数来请求排序。

    $("#MyTable").trigger('update') 将照常工作。

    $("#MyTable").trigger('update', true)更新后会要求排序。

    $this.bind("update", function (e, sort) {
       var me = this;
       setTimeout(function () {
           // rebuild parsers.
           me.config.parsers = buildParserCache(
           me, $headers);
           // rebuild the cache map
           cache = buildCache(me);
           // ADDED
           if (sort) $(me).trigger('sorton', [me.config.sortList]);
       }, 1);
    });
    

    【讨论】:

    • 这应该是 tablesorter 源可接受的功能。你有没有尝试在 github 上贡献这个?不确定 sorton 的触发。可能有更直接的方法?
    【解决方案4】:

    关于“更新”事件的实现, 它在 1ms 超时后执行更新。 这个函数应该在表格排序器中重写,使用回调。

    $this.bind("update", function () {
      var me = this;
      setTimeout(function () {
        // rebuild parsers.
        me.config.parsers = buildParserCache(
        me, $headers);
        // rebuild the cache map
        cache = buildCache(me);
    }, 1);
    

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多