jQuery + TableSorter:表为空时出错

Mik*_*nen 7 javascript jquery tablesorter exception

jQuery的插件TableSorter似乎没有处理它附加到空表的情况.这有什么好的方法吗?

在我的应用程序中,用户可以过滤和搜索数据,最终他或她将提出不返回任何值的搜索条件.在这些情况下,最好"分离"TableSorter或以某种方式修复它的代码,以便它与空表一起使用.

我目前正在使用这样的插件:

    $("#transactionsTable")
    .tablesorter({ widthFixed: true, widgets: ['zebra'] })
    .tablesorterPager({ container: $("#pager"), positionFixed: false });
Run Code Online (Sandbox Code Playgroud)

这很有效,直到表为空.然后我收到以下错误:

Line: 3
Error: '0.length' is null or not an object
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?是否可以更改脚本,以便只有表格行才有表格才能添加到表格中?

rep*_*ter 10

我想你可以为自己做到这一点.

    if ($("#transactionsTable").find("tr").size() > 1)
    {
          //> 1 for the case your table got a headline row
          $("#transactionsTable")
          .tablesorter({ widthFixed: true, widgets: ['zebra'] })
          .tablesorterPager({ container: $("#pager"), positionFixed: false });
    }
Run Code Online (Sandbox Code Playgroud)

如果你的表有一个tbody标签,那就更容易了:

if ($("#transactionsTable").find("tbody").find("tr").size() > 0)
Run Code Online (Sandbox Code Playgroud)

这种方式可能不是最专业的,但它应该在这种情况下工作.

  • 顺便说一句,我找到了一个处理空表的选项.http://tablesorter.com/docs/example-empty-table.html链接演示了它. (2认同)