loading large array in oi-select takes too much of time in angularjs
Asked Answered
M

3

8

I am using oi-select library, i have customized it according to my project need and i have written directive for it. The problem is its taking too much of time say 10secs to load around 3k array data. I want to minimize the time for it. Here I have created plunker for this.

I have directive which loads all factory data and provides it to oi-select, in my code here is 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

<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

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. 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.

Misconceive answered 20/1, 2018 at 11:30 Comment(0)
O
5

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.

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.

There should be another request to get the total number of items. 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.

Oakley answered 25/1, 2018 at 15:23 Comment(1)
can you please check my updated question, the search will happen only in firtst 50 if i use limitTo, what if i wanna search in 3k records, also this is in angular grid where i cant make seperate calls.Misconceive
P
2

The problem is not the array, the browser can easly handle that, the problem is that you're rendering all the 3k DOM elements, that's really heavy work even for an actual machine, also since there is bindings in each dom element {{}} they're being watching by AngularJs, I got the same problem and solved using Virtual Repeat from AngularJS Material, what this does is it doesn't render the whole 3k DOM elements generated by the ng-repeat, instead it just renders the ones that are visible, also I've found another library if you don't want to use Angular Material, this seems to work the same way: Angular VS-Repeat

Potts answered 20/1, 2018 at 15:30 Comment(1)
Thank u for your response, but can u pls edit my plunker code with your suggested library and give me link ?Misconceive
C
1

You may try the limitTo filter in ng-repeat in angularjs which takes the additional argument to start the iteration.

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.

Cadastre answered 24/1, 2018 at 11:58 Comment(4)
Yes I tried limitTo i will update my plunker very soon, the thing is I am not able to search after limitto, its searching only in 20records. I will update my plunker and give you wait. Thank you for responseMisconceive
here is updated plunker link plnkr.co/edit/ebakhTOCbTWR8ZOiDhe9?p=preview now the problem is when i type something its showing result in only available limitTo entries not in whole data.Misconceive
@Sudarshan The search will be a completely different scenario and more of a feature. If you need your page to be quick, you will need the limitTo filter. But for search, you will have to follow a different approach obviously.Cadastre
thats why i added plunker and asked this question sir... I am not able to get right approach.Misconceive

© 2022 - 2024 — McMap. All rights reserved.