jquery 的可选插件非常慢

发布于 2024-10-06 05:24:33 字数 33 浏览 2 评论 0原文

当在巨大的列表上使用时它会很慢,等等。如何使它更快?

it's slow when used on huge lists, etc. how make it fast?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

Saygoodbye 2024-10-13 05:24:33

jQuery UI selectable 采用 DOM 结构的所有元素,将元素数量限制为顶部的元素数量。添加过滤器:

$("#items").selectable({filter: 'li'}); 

http://forum.jquery.com/topic/major -可选择的性能问题

jQuery UI selectable takes all the elements of the DOM structure, limit the numbers of elements to those that are in the top. Add a filter:

$("#items").selectable({filter: 'li'}); 

http://forum.jquery.com/topic/major-performance-problems-with-selectable

带刺的爱情 2024-10-13 05:24:33

如果您有一个巨大列表,您将需要禁用昂贵的autoRefresh 选项,如下所示:

$(".mySelector").selectable({ autoRefresh: false });

当您需要时(例如停止),您可以刷新自己,如下所示:

$(".mySelector").selectable("refresh");

If you have a huge list, you'll want to disable the expensive autoRefresh option like this:

$(".mySelector").selectable({ autoRefresh: false });

When you want (say on stop) you can refresh yourself, like this:

$(".mySelector").selectable("refresh");
疯到世界奔溃 2024-10-13 05:24:33

我发现 jquery.selectable 在旧版浏览器(如 IE7 和 8)中非常慢,因为它必须在每个项目上调用 .offset() 方法。我在表格中的单元格上使用它,因此我能够通过制作修改版本,将 .offset() 调用的数量减少到每行一次和每列一次(而不是每个单元格一次调用)该插件具有修改后的刷新功能。这使得大型表的性能可以接受。 cellPositions 数组保存每列的水平位置。

      this.refresh = function() {
            var cellPositions = [];
            var firstRow = true;
            selecteeRows = $("tr", self.element[0]);
            selecteeRows.each(function() {
                var row = $(this);
                // adding any filters here seems to slow down IE a lot
                // self.options.filter is not used!!
                var selecteeCells = $(row).find("td"); 
                if (firstRow && selecteeCells.length > 0) {
                    for (var i = 0; i < selecteeCells.length; i++) {
                        cellPositions[i] = $(selecteeCells[i]).offset().left;
                    }
                    cellPositions[selecteeCells.length] = cellPositions[selecteeCells.length - 1] + $(selecteeCells).outerWidth(true);
                    firstRow = false;
                }
                if (selecteeCells.length > 0) {
                    var top = $(selecteeCells).first().offset().top;
                    var bottom = top + $(selecteeCells).first().outerHeight();
                    var i = 0;
                    selecteeCells.each(function() {
                        var $this = $(this);
                        first = false;
                        $.data(this, "selectable-item", {
                            element: this,
                            $element: $this,
                            left: cellPositions[i],
                            top: top,
                            right: cellPositions[i + 1],
                            bottom: bottom,
                            startselected: false,
                            selected: $this.hasClass('ui-selected'),
                            selecting: $this.hasClass('ui-selecting'),
                            unselecting: $this.hasClass('ui-unselecting')
                        });
                        i++;
                    });
                }
            });

        };

编辑:这是 github 中代码的链接:https://github .com/dfjackson/jquery.ui.selectableTable

I found that jquery.selectable is very slow in older browsers (like IE7 and 8) because it has to call the .offset() method on each item. I was using it on the cells in a table, so I was able to reduce the number of .offset() calls to one for each row and one for each column (instead of one call for every cell) by making a modified version of the plugin with a modified refresh function. This made the performance acceptable for large tables. The cellPositions array holds the horizontal position for each column.

      this.refresh = function() {
            var cellPositions = [];
            var firstRow = true;
            selecteeRows = $("tr", self.element[0]);
            selecteeRows.each(function() {
                var row = $(this);
                // adding any filters here seems to slow down IE a lot
                // self.options.filter is not used!!
                var selecteeCells = $(row).find("td"); 
                if (firstRow && selecteeCells.length > 0) {
                    for (var i = 0; i < selecteeCells.length; i++) {
                        cellPositions[i] = $(selecteeCells[i]).offset().left;
                    }
                    cellPositions[selecteeCells.length] = cellPositions[selecteeCells.length - 1] + $(selecteeCells).outerWidth(true);
                    firstRow = false;
                }
                if (selecteeCells.length > 0) {
                    var top = $(selecteeCells).first().offset().top;
                    var bottom = top + $(selecteeCells).first().outerHeight();
                    var i = 0;
                    selecteeCells.each(function() {
                        var $this = $(this);
                        first = false;
                        $.data(this, "selectable-item", {
                            element: this,
                            $element: $this,
                            left: cellPositions[i],
                            top: top,
                            right: cellPositions[i + 1],
                            bottom: bottom,
                            startselected: false,
                            selected: $this.hasClass('ui-selected'),
                            selecting: $this.hasClass('ui-selecting'),
                            unselecting: $this.hasClass('ui-unselecting')
                        });
                        i++;
                    });
                }
            });

        };

Edit: Here's a link to the code in github: https://github.com/dfjackson/jquery.ui.selectableTable

獨角戲 2024-10-13 05:24:33

与其他 jquery ui 方法不同,选择器甚至适用于嵌套元素。仅选择直接祖先使用:

jQuery('#content').selectable({
filter: '>*',
});

Unlike other jquery ui methods, selector is applied even on nested elements. to select only direct ancestors use:

jQuery('#content').selectable({
filter: '>*',
});
深巷少女 2024-10-13 05:24:33

我知道这已经晚了几年,但我一直在努力在 50x100 的桌子上获得可选择的感觉。

我发现,如果我在插入表格内容之前在表格的容器 div 上创建可选择项(使用 filter:'td'),它的运行速度会非常快。在 Firefox 中,它的实例化时间约为 1 毫秒(相比之下,在预先存在的内容上创建它时需要大约 100 毫秒)。

I know this is a couple of years too late, but I've been trying to get selectable feeling snappy on a 50x100 table.

I have found that if I create the selectable on the table's container div (with filter:'td') prior to inserting the table content it runs super fast. In firefox it was instantiating in about 1ms (compared to about 100 when creating it on pre-existing content).

流殇 2024-10-13 05:24:33

对于一个非常大的列表也有同样的问题,最终处理悬停事件并为每一行调用 .selectable() 。这为我解决了这个问题。

Had the same problem with a very large list and ended up handling the hover event and calling .selectable() there for each row. That solved the issue for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文