简体   繁体   中英

AngularJS - limitTo & filter

I am trying to use angular's limitTo and filter in conjunction but it does not seem to be working properly. I want to be able to enter a value into the search box, then press search. Afterwards, I want to limit the results displayed by entering a number in the filter box and press apply filter. However, when I enter a number there, instead of restricting the results to that amount, it instead clears all results. Any suggestions? For example, try searching simon. Then, enter 2 in the top box and click apply filter and see what happens.

Plunker:

https://plnkr.co/edit/Y522GAdShVhseAKNWouK?p=preview

angular.module('userApp', []).controller('myCtrl', function($scope) {

  var vm = this;

   $scope.users = [
    {'username':'david', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'michael', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'ben', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'simon', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'allen', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'crystal', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'meth', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'bryan', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'simon', 'email':'something@gmail.com', 'name':'Some Name'},
    {'username':'simon', 'email':'something@gmail.com', 'name':'Some Name'}
    ];

    vm.showAll = function() {
      $scope.limit = undefined;
    };

    vm.showMore = function(display) {
      $scope.limit = display;
    };

    vm.search = function(searchBar) {
      $scope.searchKeyword = searchBar;
    };

});

<!DOCTYPE html>
<html>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="userApp" ng-controller="myCtrl as main">
    <h1>Hello Plunker!</h1>

  <input type="text" ng-model="display">
      <button type="button" ng-click="main.showMore(display);">Apply Filter</button> 
    <button type="button" ng-click="main.showAll();">Show All</button> 
    <br>
    <br>
      <input type="text" ng-model="searchBar" place="enter search keyword">
      <button ng-click="main.search(searchBar);" type="button">Search</button>



    <table>
      <tr>
        <th>Username</th>
        <th>Email</th>
        <th>Name</th>
      </tr>
      <tr ng-repeat="person in users | limitTo: limit | filter: searchKeyword">
        <td>{{ person.username }}</td>
        <td>{{ person.email }}</td>
        <td>{{ person.name }}</td>
      </tr>
    </table>
  </body>

</html>

You should turn them around, as is it limits first, and then it does the filtering. So change your html to:

<tr ng-repeat="person in users | filter: searchKeyword | limitTo: limit">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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