Angular lazy load list with search (filter) box

邮差的信 提交于 2019-12-22 09:49:00

问题


I have a list that I am loading lazily - only 10 items at a time. When the user scrolls to the bottom another 10 are loaded etc. However I also want to have an option of searching for an item within the full list - not the lazy list. My list exists on client side but is 900+ items and rendering takes long. How can i keep my list displayed lazily but have the search box filter items from my full list.

*I am hoping that creating my on custom search with ng-change() is not my only option

Here is a small plnkr. The letters list is my full list that i want to be searchable. And the cachedLetters is what is rendered in the list

http://plnkr.co/edit/8tyLuo4nSTz1q0Z7DilV?p=preview

HTML

<body ng-app="app" ng-controller="Ctrl">
  <div class="container" style="padding-top:40px">
    <input class="form-control" ng-model="search" style="margin-bottom: 15px">


    <ul class="list-group" style="height: 200px; overflow-y: auto" lazy-load>
      <li class="list-group-item" ng-repeat="letter in cachedLetters | filter: {'name': search}"> {{ letter.name }}</li>
    </ul>
  </div>
</body>

ANGULAR var app = angular.module('app', [])

app.controller('Ctrl', function($scope) {

  $scope.letters = [{
    name: 'A'
  }, {
    name: 'B'
  }, {
    name: 'C'
  }, {
    name: 'D'
  }, {
    name: 'E'
  }, {
    name: 'F'
  }, {
    name: 'G'
  }, {
    name: 'H'
  }, {
    name: 'I'
  }, {
    name: 'J'
  }, {
    name: 'K'
  }, {
    name: 'L'
  }, {
    name: 'M'
  }, {
    name: 'N'
  }, {
    name: 'O'
  }, {
    name: 'P'
  }, {
    name: 'Q'
  }, {
    name: 'R'
  }, {
    name: 'S'
  }, {
    name: 'T'
  }, {
    name: 'U'
  }, {
    name: 'V'
  }, {
    name: 'W'
  }, {
    name: 'X'
  }, {
    name: 'Y'
  }, {
    name: 'Z'
  }, ]

  var ind = 0

  $scope.cachedLetters = $scope.letters.slice(0, 10)

  $scope.loadMore = function() {
    ind = ind + 10
    var r = 10
    if (ind + 10 > $scope.letters.length) {
      r = $scope.letters.length - ind
    }
    console.log("Loading")
    $scope.cachedLetters = $scope.cachedLetters.concat($scope.letters.slice(ind, r + ind))
    }

  })

app.directive('lazyLoad', function() {
  return {
    restrict: 'A',
    link: function(scope, elem) {
      var scroller = elem[0]
      $(scroller).bind('scroll', function() {
        if (scroller.scrollTop + scroller.offsetHeight >= scroller.scrollHeight) {
          scope.$apply('loadMore()')
        }
      })
    }
  }
})

回答1:


Here is a solution i have come up with. not sure if there is a better solution.

http://plnkr.co/edit/seshxi?p=preview

see plnkr for solution


来源:https://stackoverflow.com/questions/32074486/angular-lazy-load-list-with-search-filter-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!