我想要做的是過濾一個表格,只顯示包含給定值的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