【问题标题】:AngularJS, faceted search is very slowlyAngularJS,多面搜索非常慢
【发布时间】:2016-08-20 07:24:31
【问题描述】:

我在我的项目中使用了这个多面搜索https://plnkr.co/edit/wNbRidDCLmwIBWUOz5bz,但是有 430 个项目,过滤器计数器非常慢......当我使用搜索框时,我有输入延迟。

在 HTML 中:

({{ (filteredItems | filter:query | filter:count(group.name, facet)).length }})

在控制器中:

$scope.count = function (prop, value) {
    return function (el) {
        return el[prop] == value;
    };
};

你有其他选择吗?

【问题讨论】:

  • 一种避免输入延迟和避免过多无用计算的快速解决方法是通过添加ng-model-options="{ debounce: 200 }"来消除搜索输入中的模型竞价
  • 没有变化。问题在于计数功能。
  • 你能更新小提琴以包含所有项目吗?您发布的内容基本上是即时响应的。
  • 1.我会从 Angular 1.01 升级到更新的版本。 2. 我尝试将您的代码包装在 IIFE 中并添加 use strict ,但当我这样做时它失败了。我会努力清理这些东西,然后看看这是否仍然是一个问题。自 1.01 以来,Angular 的性能有了很多改进。
  • 谢谢!我的角度版本是 1.3.14。我无法用 jsfiddle 重现 :(

标签: angularjs filter


【解决方案1】:

我在这里分叉了你的小提琴:http://jsfiddle.net/ema7wpse/ 并解决了我上面提到的问题。我没有看到任何性能问题,但你原来的小提琴对我来说表现不错,所以你的里程可能会有所不同。

(function() {

  angular.module('myApp', []);

  var uniqueItems = function(data, key) {
    var result = [];

    for (var i = 0; i < data.length; i++) {
      var value = data[i][key];

      if (result.indexOf(value) == -1) {
        result.push(value);
      }

    }
    return result;
  };

   angular.module('myApp').controller("MyCtrl",MyCtrl)

  function MyCtrl($scope, filterFilter) {
    $scope.usePants = {};
    $scope.useShirts = {};
    $scope.useShoes = {};

    $scope.players = [{
      name: 'Bruce Wayne',
      shirt: 'XXL',
      pants: '42',
      shoes: '12'
    }, {
      name: 'Wayne Gretzky',
      shirt: 'XL',
      pants: '38',
      shoes: '10'
    }, {
      name: 'Michael Jordan',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'Rodman',
      shirt: 'XSXL',
      pants: '42',
      shoes: '11'
    }, {
      name: 'Jake Smitz',
      shirt: 'XXL',
      pants: '42',
      shoes: '12'
    }, {
      name: 'Will Will',
      shirt: 'XXLL',
      pants: '42',
      shoes: '12'
    }, {
      name: 'Youasdf Oukls',
      shirt: 'XL',
      pants: '38',
      shoes: '10'
    }, {
      name: 'Sam Sneed',
      shirt: 'XL',
      pants: '38',
      shoes: '10'
    }, {
      name: 'Bill Waxy',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'Javier Xavior',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'Bill Knight',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'One More',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'Player One',
      shirt: 'XXL',
      pants: '42',
      shoes: '11'
    }, {
      name: 'Space Cadet',
      shirt: 'XXL',
      pants: '42',
      shoes: '12'
    }, {
      name: 'Player Two',
      shirt: 'XXXXL',
      pants: '42',
      shoes: '12'
    } {
      name: 'Bill Knight',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'One More',
      shirt: 'M',
      pants: '32',
      shoes: '9'
    }, {
      name: 'Player One',
      shirt: 'XXL',
      pants: '42',
      shoes: '11'
    }, {
      name: 'Space Cadet',
      shirt: 'XXL',
      pants: '42',
      shoes: '12'
    }];

    // Watch the pants that are selected
    $scope.$watch(function() {
      return {
        players: $scope.players,
        usePants: $scope.usePants,
        useShirts: $scope.useShirts,
        useShoes: $scope.useShoes
      }
    }, function(value) {
      var selected;

      $scope.count = function(prop, value) {
        return function(el) {
          return el[prop] == value;
        };
      };

      $scope.pantsGroup = uniqueItems($scope.players, 'pants');
      var filterAfterPants = [];
      selected = false;
      for (var j in $scope.players) {
        var p = $scope.players[j];
        for (var i in $scope.usePants) {
          if ($scope.usePants[i]) {
            selected = true;
            if (i == p.pants) {
              filterAfterPants.push(p);
              break;
            }
          }
        }
      }
      if (!selected) {
        filterAfterPants = $scope.players;
      }

      $scope.shirtsGroup = uniqueItems($scope.players, 'shirt');
      var filterAfterShirts = [];
      selected = false;
      for (var j in filterAfterPants) {
        var p = filterAfterPants[j];
        for (var i in $scope.useShirts) {
          if ($scope.useShirts[i]) {
            selected = true;
            if (i == p.shirt) {
              filterAfterShirts.push(p);
              break;
            }
          }
        }
      }
      if (!selected) {
        filterAfterShirts = filterAfterPants;
      }

      $scope.shoesGroup = uniqueItems($scope.players, 'shoes');
      var filterAfterShoes = [];
      selected = false;
      for (var j in filterAfterShirts) {
        var p = filterAfterShirts[j];
        for (var i in $scope.useShoes) {
          if ($scope.useShoes[i]) {
            selected = true;
            if (i == p.shoes) {
              filterAfterShoes.push(p);
              break;
            }
          }
        }
      }
      if (!selected) {
        filterAfterShoes = filterAfterShirts;
      }

      $scope.filteredPlayers = filterAfterShoes;
    }, true);


    $scope.$watch('filtered', function(newValue) {
      if (angular.isArray(newValue)) {
        console.log(newValue.length);
      }
    }, true);
  }

  angular.module('myApp').filter('count', function() {
    return function(collection, key) {
      var out = "test";
      for (var i = 0; i < collection.length; i++) {
        //console.log(collection[i].pants);
        //var out = myApp.filter('filter')(collection[i].pants, "42", true);
      }
      return out;
    }
  });

  angular.module('myApp').filter('groupBy',
    function() {
      return function(collection, key) {
        if (collection === null) return;
        return uniqueItems(collection, key);
      };
    });

})();

【讨论】:

  • 谢谢!是的,我也不用 jsfiddle 复制 :(
  • 好的,我可以重现了。我的数据有更多的属性。 8 正好。
  • 我将 console.log(prop, value) 添加到 count 函数中,每当更改搜索值时,每个项目都会调用它两次。我不完全确定如何消除这种情况,但显然,这会有所帮助。
  • 我也觉得但是不知道怎么弄
  • 我为每个对象添加了 console.log(el.Id) 和它被调用 3 次的函数,但这是正常的,因为每个对象有 3 个方面
猜你喜欢
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 2012-01-18
  • 2023-03-03
  • 1970-01-01
  • 2014-05-04
相关资源
最近更新 更多