用jquery过滤表行 [英] Filter table rows with jquery

查看:164
本文介绍了用jquery过滤表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据列值过滤表格行。插件排除,我想弄清楚如何使这个东西工作。

解决方案

你的问题是相当含糊的,但一般的想法可能是这样的:

$ $ $ $ $ $ $ $td)filter(function(){
返回$(this).text()。indexOf(whatever)!== -1;
})。parent()。remove();

这是一个工作示例。它首先选择所有的表格单元格,然后根据一些文本对它们进行过滤,并删除剩余行的父级(它应该是 tr )。
$ b

如果你不关心每个列,你可以选择 tr 元素,并去掉。这仍然可以工作,因为 text 将会返回所选 tr 的所有子项的文本。



更新基于评论

以上操作将完全从DOM中删除匹配的表格行。如果你想隐藏它们,你可以用 hide 替换 remove 。如果你想让这些行再次可见,你可以简单地做一些事情:

  $(tr)。show ); 

选择所有 tr 元素并显示他们(那些已经可见的将不会受到影响,所以只是以前隐藏的将再次可见)。

I would like to know how to filter table rows based on a column value. Plugins excluded, I would like to figure it out how to make this thing to work.

解决方案

Your question is quite vague, but the general idea would be something like this:

$("td").filter(function() {
    return $(this).text().indexOf("whatever") !== -1;
}).parent().remove();

Here's a working example. It first selects all table cells, then filters them based on some text, and removes the parent (which should be the tr) of any remaining rows.

If you don't care about individual columns, you can select the tr elements and get rid of the call to parent. That will still work because text will return the text of all the children of the selected tr.

Update based on comments

The above will remove the matching table rows from the DOM completely. If you want to just hide them, you can replace remove with hide. If you then wanted to make the rows visible again you could simply do something like:

$("tr").show();

Which selects all the tr elements and shows them (ones that are already visible will not be affected, so just the ones previously hidden will become visible again).

这篇关于用jquery过滤表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆