我有以下格式的數據:angularjs過濾了深刻的JSON對象
{
"A": {
"P": {
"Age": {
"keyName": "Repo1",
"checked": false
},
"Gender": {
"keyName": "Repo2",
"checked": false
}
}
},
"S": {
"D": {
"keyName": "Repo3",
"checked": false
},
"W": {
"keyName": "Repo4",
"checked": false
}
}
}
HTML:
<div ng-controller="MyController">
<ul>
<li ng-repeat="kName in keyNames">
<input type="checkbox" value="{{kName.keyName}}" ng-click="selectedKeyNames($event, kName)"> {{kName.keyName}}
</li>
</ul>
<pre>{{tempData | json}}</pre>
</div>
JS:
var myApp = angular.module('myApp', []);
myApp.controller('MyController',["$scope","$filter", function ($scope, $filter) {
$scope.tempData = {"A":{"P":{"Age":{"keyName":"Repo1","checked":false},"Gender":{"keyName":"Repo2","checked":false}}},"S":{"D":{"keyName":"Repo3","checked":false},"W":{"keyName":"Repo4","checked":false}}};
$scope.keyNames = $filter("buildKeyMap")($scope.tempData);
$scope.selectedKeyNames = function(e, rpt){
$scope.findNode($scope.tempData, "keyName", rpt.keyName, $(e.target).is(":checked"));
};
$scope.findNode = function(obj, key, val, isChecked) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat($scope.findNode(obj[i], key, val, isChecked));
} else if (i == key && obj[key] && obj[key] == val) {
obj.checked = isChecked;
$scope.foundObject = obj;
break;
}
}
return $scope.foundObject;
};
}]).filter("buildKeyMap",function(){
return function(items){
var options = []
var recursiveFilter = function(items){
angular.forEach(items, function(item, parentKey){
if(item.hasOwnProperty("keyName")){
options.push({"keyName": item.keyName});
}else{
recursiveFilter(item);
}
});
};
recursiveFilter(items);
return options;
};
});
,我想只過濾那些節點其中checked = true。它應該是在相同的JSON結構。我已經完成了checked = true,但我無法從json中刪除或添加節點。
這裏是小提琴: http://jsfiddle.net/qsp4dwsd/5/ 它設置檢查= true,但JSON保持不變的情況下,假意或真心
你控制從後端初始數據結構?如果使用陣列結構,會更簡單 – charlietfl
@charlietfl:請問你可以用數組結構來解決嗎?謝謝 – maverickosama92
如果您確實控制後端,則使用所有對象的標準化鍵發回對象數組。您有幾個深度級別,因此嵌套具有相同對象結構的「children」數組也是如此。然後你可以使用嵌套'ng-repeat' – charlietfl