2014-10-22 34 views
0

我有一个JSON数据填充标签动态与AngularJS与过滤数据

[ 
    { 
     "name": "a", 
     "data": "north", 
     "value": "10", 
     "finished": "50" 
    }, 
    { 
     "name": "b", 
     "data": "south", 
     "value": "100", 
     "finished": "10" 
    }, 
    { 
     "name": "c", 
     "data": "north", 
     "value": "20", 
     "finished": "50" 
    }, 
    { 
     "name": "a", 
     "data": "south", 
     "value": "80", 
     "finished": "10" 
    } 
    .... 
] 

我要动态地添加标签,如果“名称”值是不同的,并添加标签模板的内容。可能有多个具有相同名称的json数组,并且应该放在同一个选项卡中。

最初我:

for(var i=0;i<$scope.operation.length;i++) { 
    if($scope.operation[i].name === "a") { 
     $scope.tab1 = { 
      "title": "Data A" ,"path": "/data" 
     }; 

    } 
    if($scope.operation[i].name === "b"){ 
     $scope.tab2 = { 
      "title": "Data B " ,"path": "/datab" 
     }; 

    } 
} 
and so on ... 

这种方法并不好,如果有多个不同名称的值。有没有我可以做的任何优化过滤。 而对于我使用这又是一个坏的方法过滤器显示数据模板:

// custom filters - should return data with name value "a" 
$scope.isDCone = function(data,id){ 
    return data.name === "a"; 
}; 

    // custom filters - should return data with name value "b" 
    $scope.isDCtwo = function(data){ 
    return data.name === "b"; 
}; 

回答

0

尝试减少功能

$scope.tabs = $scope.operation.reduce(function(a,b){ 
    if (a[b.name]){ 
     a[b.name].push(b); 
    } else{ 
     a[b.name] = [b]; 
    } 
    return a; 
}, {});