使用 jQuery 更高效地根据条件查找每个表行中的第 n 个单元格?
我们有一个表格,可以进行高级搜索。我想让表格突出显示他们搜索的每个术语的表格中的文本。例如:如果他们在“主题”字段中搜索特定单词,则应仅在“主题”列中突出显示该术语,即使该单词可能出现在另一列中。
我正在使用 .highlight()
方法的突出显示插件 - 但我最担心的是选择正确表格单元格的有效方法。 我的内容有作品,但有数百行时速度很慢。我觉得有一种更好的方法可以在没有 .each()
循环的情况下做到这一点。
//Select the table
var $table = $("#myTable");
//Examples: The users' search terms
var sFrom = "example";
var sTo = "example";
var sSubject = "example";
//Make sure there is at least 1 term to search for
if(sFrom !== "" || sTo !== "" || sSubject !== ""){
//Find the index of each column based on a class set on the table header
//(the number of columns could change from page to page)
var $headers = $table.find("thead tr").children();
var iFrom = $headers.filter(".js-from").index();
var iTo = $headers.filter(".js-to").index();
var iSubject = $headers.filter(".js-subject").index();
//----------------------------------------------
//This is the critical part!
//----------------------------------------------
//Loop through each table row and select each
$table.find("tbody tr").each(function (i, row) {
var $thisRowCells = $(row).children();
if (sFrom !== "") $thisRowCells.eq(iFrom).highlight(sFrom);
if (sTo !== "") $thisRowCells.eq(iTo).highlight(sTo);
if (sSubject !== "") $thisRowCells.eq(iSubject).highlight(sSubject);
});
//----------------------------------------------
}
编辑: 下面是上述代码的 JSFiddle 可供尝试: http://jsfiddle.net/ZLTdf/1/
We have a table, that allows for an advanced search. I'd like to get the table to highlight the text in the table of each term them searched for. For example: if they searched for a specific word in the "subject" field, it should only highlight that term in the "subject" column even though that word might appear in another column.
I am using a highlighting plugin for the .highlight()
method - but I'm mostly worried about an efficient way to select the proper table cells. What I have works, but it's slow with several hundred rows. I feel like there's a better way to do this without an .each()
loop.
//Select the table
var $table = $("#myTable");
//Examples: The users' search terms
var sFrom = "example";
var sTo = "example";
var sSubject = "example";
//Make sure there is at least 1 term to search for
if(sFrom !== "" || sTo !== "" || sSubject !== ""){
//Find the index of each column based on a class set on the table header
//(the number of columns could change from page to page)
var $headers = $table.find("thead tr").children();
var iFrom = $headers.filter(".js-from").index();
var iTo = $headers.filter(".js-to").index();
var iSubject = $headers.filter(".js-subject").index();
//----------------------------------------------
//This is the critical part!
//----------------------------------------------
//Loop through each table row and select each
$table.find("tbody tr").each(function (i, row) {
var $thisRowCells = $(row).children();
if (sFrom !== "") $thisRowCells.eq(iFrom).highlight(sFrom);
if (sTo !== "") $thisRowCells.eq(iTo).highlight(sTo);
if (sSubject !== "") $thisRowCells.eq(iSubject).highlight(sSubject);
});
//----------------------------------------------
}
EDIT:
Here's a JSFiddle of the above code to try out: http://jsfiddle.net/ZLTdf/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎对我有用,并且在计时时比
.each()
循环快大约 2 倍。我能够在大约 200 毫秒内突出显示 500 行的单独列中的 3 个唯一字符串,这是否可以改进?
This seems to work for me and be about 2x faster than the
.each()
loop when timed. I was able to highlight 3 unique strings in seperate columns for 500 rows in ~200msCan this be improved at all?
我认为没有任何方法可以避免循环,但您可以在执行循环之前缩小堆栈的大小。类似的东西:(
我在 http://jsfiddle.net/nicholasstephan/84DK9/)
可能会更好...
I don't think there's any way to avoid looping, but you can trim down the size of the stack before you do it. Something like:
(I've got a jsfiddle up at http://jsfiddle.net/nicholasstephan/84DK9/)
might work better...
IE8 未能通过 jsfiddle 测试。 http://jsfiddle.net/ZLTdf/
IE8 did not survive the jsfiddle test. http://jsfiddle.net/ZLTdf/