我有一个Json对象。
[{
"id":"1",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id":"2",
"username": "seenu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui",
"time": "2"
}, {
"id":"3",
"username": "sam",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
},
{
"id":"4",
"username": "vishnu",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "timetracker",
"task": "ui designing",
"time": "1"
},{
"id":"5",
"username": "arun",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id":"6",
"username": "seenu",
"FromDate": "Wed Mar 01 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui",
"time": "2"
}, {
"id":"7",
"username": "sam",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on api",
"time": "6"
},
{
"id":"8",
"username": "vishnu",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "timetracker",
"task": "ui designing",
"time": "4"
}]
我想制作一个自定义过滤器来过滤特定用户(根据用户名),并从过滤后的数据中获取长度。 例如,用户名:Vishnu,我想过滤并检查用户名Vishnu有多少个对象。像那样,我希望得到所有用户。
我创建了一个自定义过滤器,如(类型代码,不正确),
app.filter('myfilter',function(){
return function(mydata){
var filterd data=[];
angular.foreach(mydata,function(s){
if(s.name==$scope.model)
{
filtereddata.push(s)
}
})
return filtereddata;
}
});
现在该怎么办?
答案 0 :(得分:0)
使用
s.name.indexOf($scope.model)>-1
而不是
s.name==$scope.model
用于喜欢搜索或包含搜索
答案 1 :(得分:0)
您可以返回filtereddata.length吗?
而不是filtereddata答案 2 :(得分:0)
根据评论
创建的示例<强> HTML 强>
<body ng-app="myApp">
<div ng-controller="Ctrl">Search:
<input ng-model="searchText">
<div ng-repeat="(k,v) in items | myFilter:searchText">
{{v}}
</div>
</div>
</body>
<强> JS 强>
var myApp = angular.module('myApp', []);
myApp.controller('Ctrl', function($scope) {
$scope.items = [{
"id": "1",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id": "2",
"username": "seenu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui",
"time": "2"
}, {
"id": "3",
"username": "sam",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
}, {
"id": "4",
"username": "vishnu",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "timetracker",
"task": "ui designing",
"time": "1"
}, {
"id": "5",
"username": "arun",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id": "6",
"username": "seenu",
"FromDate": "Wed Mar 01 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui",
"time": "2"
}, {
"id": "7",
"username": "sam",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on api",
"time": "6"
}, {
"id": "8",
"username": "vishnu",
"FromDate": "Wed Mar 03 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "timetracker",
"task": "ui designing",
"time": "4"
}]
});
myApp.filter('myFilter', function() {
return function(items, search) {
var result = [];
angular.forEach(items, function(value, key) {
angular.forEach(value, function(value2, key2) {
if (value2 === search) {
result.push(value2);
}
})
});
return result;
}
});
工作Jsfiddle演示
答案 3 :(得分:0)
您只需以这种方式使用自定义过滤器:过滤器名称加上过滤器后缀。
然后在控制器中简单地使用它:
localparam S_IDLE = 2'b00;
localparam S_RIGHT = 2'b10;
localparam S_LEFT = 2'b01;
localparam BOTH = 2'b11;
reg [1:0] state;
wire [1:0] next_state = {r,l};//bypass toggle flops
always @(posedge slowclk) begin
case(next_state)
S_IDLE : state <= S_IDLE ;
S_RIGHT : state <= S_RIGHT;
S_LEFT : state <= S_LEFT ;
S_BOTH : state <= S_IDLE ;
endcase
end
//Outputs
always @(posedge slowclk) begin
case(state)
S_IDLE : led <= led;
S_RIGHT : led <= led>>1;
S_LEFT : led <= led<<1;
default : led <= led;
endcase
end
现在function myCtrl($scope, filterFilter) {
var filteredRes = filterFilter(data, searchQuery);
}
为您提供与搜索查询匹配的结果长度。