0

I search through large data using AngularJS only when I type >3 chars in input.

var app = angular.module('test_table', []);
app.controller('main_control',function($scope, $http){
    $scope.inputChange = function(){
        if($scope.search.length > 3){
            $http.get("http://localhost:7001/load").success(function(data){
                $scope.loaded=data; 
            });
        }
        if($scope.search.length < 4){
            $http.get("http://localhost:7001/load").success(function(data){
                $scope.loaded=""; 
            });
        }
    }       
}); 

HTML code:

<input type="search" class="inputsearchform" ng-model="search" ng-change="inputChange()"/>
...
<tr class="rowR" ng-repeat="data in loaded | filter:{song_name: search}">

I have problem. When I type more than 3 chars and then delete everything using default button in input, the whole data is loaded to the page, and it cause a big delay. How to deal with it?

2 Answers 2

3

You could add the following:

if($scope.search.length == 0){
      $scope.loaded=""; 
}

or create a function triggered by the delete button:

$scope.delete = function(){ 
      $scope.loaded=""; 
}

PS: You shouldn't call a service if you are going to assign an empty variable:

        if($scope.search.length < 4){
            $http.get("http://localhost:7001/load").success(function(data){
                $scope.loaded=""; 
            });
        }

should really be:

        if($scope.search.length < 4){
                $scope.loaded=""; 
        }

PS 2: if($scope.search.length < 4) doesn't really make sense either because if there are 3 chars - this if statement would be satisfied as eluded to by @aw04

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a reason not to just remove the http call on < 4 rather than create another condition? I can't see the point in it
Yes, my mistake. Thanks for help!
1

In order to avoid the delay, you will have to limit the data you are rendering.For this you can use the angular filter limitTo. Set the limitTo to any value which you want to load for default.

7 Comments

if you're trying to limit the data for performance reasons, it would make more sense to limit it on the server
@aw04 what do you mean by this? Because I want to increase performance. What can you advise for it?
@LevS So what this answer is saying is to limit the data, which would mean you need to add paging or some sort of load more functionality. My point was that doing this in angular wouldn't help as much because you're still loading all the data from the server, which likely takes much more time than the rendering
@LevS Having said that, if you build something into your server to only grab x records at a time (then again, paging or load more), this could potentially improve performance considerably
@LevS More or less, yes. You can research server side paging and see if it's a fit for what you're doing. I would only recommend this if your performance problem is a result of the amount of data coming from the server.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.