【发布时间】:2010-09-20 01:46:02
【问题描述】:
我想要做的是过滤一个表格,仅显示包含给定值的 tbody 与输入到文本框中的值,并以斑马条纹图案显示过滤后的行。
斑马条纹很快,过滤通常很快,除了在有很多 tbody 的桌子上的第一个过滤器(比如 2000 tbody?...我没有测量第一次可见的减速,并且没有' t 通过数字测试速度,但在 Firefox 和 Chrome 中速度很慢)
首先是JS:
//filter results based on query
function filter(selector, query) {
var regex = new RegExp( query, "i" ); // I did this from memory, may be incorrect, but I know the thing works, the problem is the next part, cos on 5 rows it's fast
$(selector).each(function() {
( regex.test( $(this).text() ) ) < 0) ? $(this).hide().removeClass('visible') : $(this).show().addClass('visible');
});
}
// then after this I recall the zebra function, which is fast.
然后是样本数据:
<table>
<thead>
<tr>
<th>value to find 1</th>
<th>value to find 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>12345</td>
<td>67890</td>
</tr>
<tr>
<td>empty for now, while testing</td>
<td>may contain other info later</td>
</tr>
</tbody>
<tbody>
<tr>
<td>23456</td>
<td>78901</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
<tbody>
<tr>
<td>45678</td>
<td>90123</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
... /ad nauseum, for 2000 rows +
<tfoot>
</tfoot>
</table>
因此,例如,尝试匹配值 123 将返回此示例数据的第一行和第三行,但我认为您已经弄清楚了...
帮忙?
【问题讨论】:
标签: javascript jquery regex