jQuery 的插件 TableSorter 似乎无法处理将其附加到空表的情况。有解决这个问题的巧妙方法吗?
在我的应用程序中,用户可以过滤和搜索数据,最终他或她会提出一个不返回任何值的搜索条件。在这些情况下,最好“分离”TableSorter 或以某种方式修复它的代码,以便它可以处理空表。
我目前正在使用这样的插件:
$("#transactionsTable")
.tablesorter({ widthFixed: true, widgets: ['zebra'] })
.tablesorterPager({ container: $("#pager"), positionFixed: false });
这很有效,直到表格为空。然后我得到以下错误:
Line: 3
Error: '0.length' is null or not an object
有什么想法吗?是否可以更改脚本,以便仅在表格有行时才将表格排序器添加到表格中?
最佳答案
我认为你可以自己做。
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 });
}
如果你的表有一个 tbody 标签,那就更容易了:
if ($("#transactionsTable").find("tbody").find("tr").size() > 0)
这种方式可能不是最专业的方式,但在这种情况下应该是可行的。
关于javascript - jQuery + 表格排序器 : Error when table is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857962/