2016-04-26 58 views
1

我使用這個多面搜索https://plnkr.co/edit/wNbRidDCLmwIBWUOz5bz到我的項目,但與430項目,篩選計數器是非常緩慢...我有輸入滯後,當我使用搜索框。AngularJS,多面搜索是非常緩慢的

在HTML:

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

在控制器:

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

你有別的選擇嗎?

+2

一個快速的解決辦法,以避免輸入滯後,並通過添加'NG-模型選項=「{反跳:200}」避免過多的無用的計算是在搜索輸入防抖動模式招標' – Naigel

+0

沒有變化。問題是計數功能。 – Akawan

+0

你能更新小提琴以包括所有的物品嗎?響應基本上是你發佈內容的瞬間。 –

回答

0

我把你的小提琴放在這裏: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); 
     }; 
    }); 

})(); 
+0

)謝謝!是的,我不用jsfiddle重現:( – Akawan

+0

好吧,我可以重現。我的數據有更多的屬性。8確切。 – Akawan

+0

我添加了console.log(prop,value)到計數功能,它被稱爲兩次每一個項目,每當搜索值改變。我不完全確定如何消除這一點,但顯然,這將有所幫助。 –