简体   繁体   English

在oi-select中加载大数组会在angularjs中花费太多时间

[英]loading large array in oi-select takes too much of time in angularjs

I am using oi-select library, i have customized it according to my project need and i have written directive for it. 我正在使用oi-select库,我已根据我的项目需要对其进行了定制,并且我已经为它编写了指令。 The problem is its taking too much of time say 10secs to load around 3k array data. 问题是它花了太多时间说10秒加载大约3k阵列数据。 I want to minimize the time for it. 我想尽量缩短它的时间。 Here I have created plunker for this. 在这里,我为此创造了plunker

I have directive which loads all factory data and provides it to oi-select, in my code here is html 我有指令加载所有工厂数据并提供给oi-select,在我的代码中这里是html

<small><b>select {{MPhardwaresListDropDown.length}}</b></small>
                        <div style="padding-top: 7px">
                            <div title="" class="selected-multiple-items">
                                    {{MPselectedHardwares.length}} selected
                            </div>
                            <grid-multi-select id="hardwareId" clean="clean" optionlist="MPhardwaresListDropDown" colval="name"></grid-multi-select>
                        </div>

HTML code in directive looks like 指令中的HTML代码如下

<div>
        <div ng-repeat="optionVal in tempOptionList | orderBy : ['-originalcheck','+label']" prevent-close>
            <div ng-if="optionVal.label">
                <label class="checkbox" ng-if="!typeFilterOptions || (optionVal.label.toString().toLowerCase().indexOf(typeFilterOptions.toLowerCase()) > -1)">
                    <input type="checkbox" name="checkbox1" ng-checked="optionVal.check" ng-model="optionVal.check"/>

                    <span class="checkbox__input"></span>
                    <span class="checkbox__label" style="color:#A9A9A9;">{{optionVal.label}}</span>
                </label>
            </div>
            <div ng-if="!optionVal.label">
                <label class="checkbox" ng-if="!typeFilterOptions || (optionVal.val.toString().toLowerCase().indexOf(typeFilterOptions.toLowerCase()) > -1)">
                    <input type="checkbox" name="checkbox1" ng-checked="optionVal.check" ng-model="optionVal.check" ng-change="checking(typeFilterOptions)"/>

                    <span class="checkbox__input"></span>
                    <span class="checkbox__label" style="color:#A9A9A9;">{{optionVal.val}}</span>
                </label>
            </div>



        </div>

angular code is too big to mention in this question please refer plunker, but this is how it loops 角度代码太大了,在这个问题中请参考plunker,但这就是它如何循环

scope.selection = scope.selectionorg;
                scope.checkedall=scope.checkedallorg;
                scope.OptionList=parentScope[scope.parentListName].sort();
                scope.tempOptionList=[];
                var i=0;
                for(i=0;i<scope.OptionList.length;i++) {
                    scope.tempOptionList[i] = {val: scope.OptionList[i], check: false, originalcheck: false};
                }
                if(scope.optionListSelectedList.length>0) {
                    angular.forEach(scope.optionListSelectedList, function(obj){
                        angular.forEach(scope.tempOptionList, function(obj1){
                            if(obj===obj1.val){
                                obj1.check=true;
                                obj1.originalcheck=true;
                            }
                        });
                    });
                }
                else{
                    scope.checkedall=false;
                }
            };

I want something like which will load partial data on scroll it loads more data, any help will be appreciated. 我想要一些类似的东西将加载部分数据滚动它加载更多的数据,任何帮助将不胜感激。 Thank you so much. 非常感谢。

EDIT Now i have edited my plunker with limitTo in ng-repeat, for that i have written new directive which will trigger addmoreitems function when scroll will reach bottom. 编辑 现在我已经使用limitTo在ng-repeat中编辑了我的plunker,因为我已经编写了新指令,当滚动到达底部时将触发addmoreitems函数。 updatedPlunker updatedPlunker

Now problem is when i am typing and searching something its searching in only available records with respect to limitTo its not searching in all data, say now the limitTo is 50 then search is happening only in 50 records not in 3k records. 现在的问题是,当我打字并搜索其搜索内容时,只搜索有关限制的可用记录,而不是搜索所有数据,现在说limitTo为50,那么搜索只发生在不是3k记录的50条记录中。

Correct way of doing this kind of requirement is to do with pagination , since you are loading data from the server side, you should make your api to support pagination. 正确执行此类要求的方法是使用pagination ,因为从服务器端加载数据,您应该使api支持分页。

It should accept three parameters such as number of items , start , limit and you should initially get the number of items and repeat it until you get the whole result set. 它应该接受三个参数,例如number of itemsstartlimit ,你应该首先得到项目的数量并重复它,直到你得到整个结果集。

There should be another request to get the total number of items. 应该有另一个请求来获取项目total number By doing this you can retrieve all the elements at once loaded in the client side. 通过这样做,您可以检索客户端一次加载的所有元素。 Until that you should have a loading indicator, or you could load this data when the login process/application starts. 在此之前,您应该有一个加载指示器,或者您可以在登录进程/应用程序启动时加载此数据。

limitTo will not be able to help with the search , because you are limiting the results. limitTo将无法帮助您进行搜索,因为您限制了搜索结果。

问题不在于数组,浏览器可以轻松处理,问题在于你渲染了所有3k DOM元素,即使对于实际的机器也是如此繁重,因为每个dom元素中都有绑定{{}}他们正在通过AngularJs看,我得到了同样的问题,解决了使用从AngularJS材质虚拟重复 ,这样做是不使被NG重复产生的整个3K DOM元素,而不是它只是呈现的那些这是可见的,如果你不想使用Angular Material,我也找到了另一个库,这似乎也是一样的: Angular VS-Repeat

You may try the limitTo filter in ng-repeat in angularjs which takes the additional argument to start the iteration. 您可以尝试使用angularjs中的ng-repeat中的limitTo过滤器,该过程采用附加参数来启动迭代。

https://docs.angularjs.org/api/ng/filter/limitTo https://docs.angularjs.org/api/ng/filter/limitTo

On scroll, you can then change that argument based on the number of items pending or left for rendering or the number of items already rendered. 在滚动时,您可以根据待渲染或待渲染的项目数或已渲染的项目数更改该参数。 This should help you in approach of selective loading of data on scroll. 这应该可以帮助您选择加载滚动数据。

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

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