我正在使用AngularJS并尝试创建一个搜索属性的过滤器。
我有一个像这样的选择框:
<select
class="selectBox"
multiple="multiple"
ng-model="selectedSubArea"
ng-options="property.SubArea as (property.SubArea + ' ('+ filtered.length +')') for property in filtered = (properties | unique:'SubArea') | orderBy:'SubArea'">
</select>
这是一项独特的功能:
myApp.filter('unique', function() {
return function(input, key) {
var unique = {};
var uniqueList = [];
for(var i = 0; i < input.length; i++){
if(typeof unique[input[i][key]] == "undefined"){
unique[input[i][key]] = "";
uniqueList.push(input[i]);
}
}
return uniqueList;
}; });
如何让filtered.length
工作?
这是我的JSFiddle
答案 0 :(得分:0)
这是一个解决方案:
http://jsfiddle.net/odpw6c6q/16/
创建一个过滤器,返回带有count的对象(它们在原始列表中的次数):
myApp.filter('uniqueWithCount', function() {
return function(input, key) {
var unique = {};
var uniqueList = [];
var obj, val;
for(var i = 0; i < input.length; i++){
val = input[i][key];
obj = unique[val];
if (!obj) {
obj = unique[val] = {data: input[i], count: 0};
uniqueList.push(obj);
}
obj.count++;
}
return uniqueList;
};
});
在HTML中使用该过滤器:
<select class="form-control" multiple="multiple" ng-model="selectedSubArea"
ng-options="property.SubArea as (item.data.SubArea + ' ('+ item.count +')') for item in filtered = (properties | uniqueWithCount:'SubArea') | orderBy:'SubArea'"></select>