2014-10-22 37 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; 
}, {});