我想要做的是过滤一个表格,只显示包含给定值的tbody与输入到文本框中的值,并显示斑马条纹图案中的过滤行。什么是在javascript中找到tbody行的最佳方式?正则表达式还是...?
斑马条纹很快,过滤通常很快,除了在很多tbodys的桌子上的第一个过滤器(比如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
将返回此示例数据的第一和第三行,但我认为你已经想通了...
帮助?
即使交换,也无法避免呈现问题。通过innerHTML以字符串形式完成的大表格仍需要一段时间。 – Robusto 2010-09-19 04:24:09
我喜欢这个技巧,我得试试这个...... – jcolebrand 2010-09-19 10:54:04
@Robusto我更新了我的答案,在大桌子上显示了不同的表现。 – gradbot 2010-09-19 17:21:29