简体   繁体   English

在IE8中处理Select2中多个选项的选择-性能问题

[英]Handling the selection of multiple options in Select2 in IE8 - performance issues

I have added a Select all / deselect all wrapper round a Select2 multi select control. 我在Select2多选择控件中添加了全选/取消全选包装器。

It works by looping through the options, pushing the values into an array then passing the array to the selct2 val as follows: 它通过遍历选项,将值推入数组然后按如下所示将数组传递到selct2 val来工作:

mySelect2.select2("val", mySelectedValuesArray);

This works fine in Chrome and in cases where there are not so many options to be selected. 在Chrome中以及在没有太多选项可供选择的情况下,这种方法可以正常工作。 But in IE8 where they might be 100+ options the browser freezes as it attempts to render the selected values and I get multiple Stop Running this Script? 但是在IE8(可能有100多个选项)中,浏览器在尝试呈现选定的值时会冻结,并且出现多个“停止运行此脚本”? alerts. 警报。 I have had similar problems with IE8 when using expandable text boxes where the browser freezes whenever it has to increase the height of the textbox and assume its a quirk of the IE rendering engine. 使用可扩展文本框时,我在IE8中也遇到了类似的问题,其中,每当浏览器不得不增加文本框的高度并假定其具有IE渲染引擎的怪异功能时,浏览器就会冻结。 Anyway, in this case it renders the page unusable whenever you select all with anything more than 30 or 40 options. 无论如何,在这种情况下,只要您选择了超过30或40个选项的所有页面,它就会使页面无法使用。

I have tried creating the markup for the selected options container manually so as to just add it in one go, but, aside form then having to manually wire up the click events on each one to be able top remove them, Im finding when the selects change event fires it, select2 ends up removing the options anyway and i cant find a way round this. 我尝试过手动为选定的选项容器创建标记,以便一次性添加它,但是,除了表格,然后必须手动将每个事件的单击事件连接起来,才能从顶部将它们删除,我发现选择的时间更改事件将其触发,select2最终还是删除了选项,我无法找到解决方法。

Any ideas? 有任何想法吗?

As an update here is my code 作为更新,这是我的代码

$(".filterIconContainer .filtericon").on("click",function () {
    var $this = $(this);
    var $associatedSelect = $("#" + $this.attr("data-associated-select"));
    if ($associatedSelect.length == 0) {
        $associatedSelect = $("#filterContainer div[data-tabid='" + $("#filterTabs li.active").attr("id") + "'] select");
    }

    if ($this.attr("data-action") == "select") {
        var selected = [];
        $associatedSelect.find("option").each(function (i, e) {
           selected.push($(e).attr("value"));
        });

       setTimeout(function() {
           $associatedSelect.select2("val", selected); // Browser throws stop running this script alert during select2 processing this line
           $associatedSelect.change(); // call the change event to force any post change action
       },5);
    }
    else {
       $associatedSelect.select2('val', '');
       $associatedSelect.change(); // call the change event to force any post change action
    }
});

In the end I fixed this by making a change to the select2.js file 最后,我通过更改select2.js文件来解决此问题

$(data).each(function () {
    var i = this;
    setTimeout(function () {
        self.addSelectedChoice(i);
    }, 0);
});

Wrapping the call to addSelectedChoice in a setTimeout allows IE to render the change without throwing a slow running script error. 在setTimeout中包装对addSelectedChoice的调用可以使IE呈现更改,而不会引发运行缓慢的脚本错误。

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

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