简体   繁体   English

在javascript中隐藏和显示表格的所有行的最快方法是什么?

[英]What's the fastest way of hiding and showing all the rows of a table in javascript?

I am building a filter for querying our html table (10k+ rows). 我正在建立一个用于查询我们的html表(超过10k行)的过滤器。 My initial thought was to first hide all the rows, and then unhide the rows that match the specific query. 我最初的想法是先隐藏所有行,然后取消隐藏与特定查询匹配的行。 If the filter is removed, then show all rows. 如果删除了过滤器,则显示所有行。

Is this a more optimal way of writing the hide/show all rows functions? 这是编写隐藏/显示所有行功能的最佳方法吗?

// optimize this!
this.hideAllRows = function() {
    nodes = document.getElementById('table_body').children
    for(var i=0, i_max=nodes.length; i<i_max; i++) {
        nodes[i].style.display="none"
    }
}

// optimize this!
this.showAllRows = function() {
    nodes = document.getElementById('table_body').children
    for(var i=0, i_max=nodes.length; i<i_max; i++) {
        nodes[i].style.display=""
    }
}

Thanks! 谢谢!

One solution would be to implement a pagination or "infinite" scroll feature. 一种解决方案是实现分页或“无限”滚动功能。 This would remove the need to render 10k dom elements simultaneously. 这将消除同时渲染10k dom元素的需要。 You could render them in batches, as the user scrolls, or chunk them into pages. 您可以在用户滚动时分批渲染它们,也可以将它们分块成页面。

Alternatively, you can try pulling the table out of the dom, hiding rows, then reinserting it. 或者,您可以尝试将表从dom中拉出,隐藏行,然后重新插入。 This will prevent unneccesary reflows/paints/etc. 这将防止不必要的回流/油漆/等。

As a rule of thumb using a pure javascript for loop is faster than using jQuery .each() but your basic selection using the .getElementById() and .children property is already well optimized. 根据经验,使用纯JavaScript for循环比使用jQuery .each()更快,但是使用.getElementById().children属性的基本选择已经得到了优化。

However iterating through 10k+ elements in the browser is always going to be slow. 但是,在浏览器中遍历10k +元素总是很慢。 Showing and hiding elements is best suited to record sets in the 100s not the 1000s. 显示和隐藏元素最适合记录100而不是1000的记录集。

Why not make an AJAX request that returns data (presumably from a database) already formated as <tr>...some <td>s here....</tr><tr>...some <td>s here....</tr> ? 为什么不发出AJAX请求以返回已经格式化为<tr>...some <td>s here....</tr><tr>...some <td>s here....</tr>数据(可能是来自数据库) <tr>...some <td>s here....</tr><tr>...some <td>s here....</tr>吗?

That way you can let your database do all the heavy lifting when it comes to filtering, they are optimized to do this. 这样一来,您就可以让数据库在过滤方面承担所有繁重的工作,它们已经过优化,可以做到这一点。 It keeps your code is simple, your bandwidth is reduced, and your DOM manipulation is kept to a minimum. 它使您的代码简单,带宽减少,并且DOM操作降至最低。

Whenever you want to apply a filter, you can make a $.ajax request. 每当您要应用过滤器时,都可以发出$.ajax请求。

function filter(criteria){

    $.ajax({
        url : myDataUrl,
        type : "POST",
        data : {criteria : criteria}
    })
    .success(function (data){
        $("#table-body").html(data);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM