Javascript 从动态JSON数据级联父子选择框
我已经从服务器接收的JSON数据中动态创建了一些链式选择框。链接/级联的工作方式是,每个选择框都是具有以下属性的命名对象:Javascript 从动态JSON数据级联父子选择框,javascript,angularjs,Javascript,Angularjs,我已经从服务器接收的JSON数据中动态创建了一些链式选择框。链接/级联的工作方式是,每个选择框都是具有以下属性的命名对象: 父属性:此选择框对象的父对象的名称 选项:选项对象数组,其中每个对象包含:(a)选项值(b)父选项值-映射当前值的父选择框值。(c) 选项ID 选定选项:具有两个属性的对象:(a)当前选定值(b)当前选定值的ID 我正在使用“选项”标记中的ng repeat或“选择”标记中的ng option创建选择框,然后使用自定义过滤器,通过将选项值(2>a)的父选项值(2>b)与其父
<div ng-repeat="selection in vm.selectionData">
<div ng-repeat="(key, attribute) in selection.attributes">
<span>{{key}}</span>
<select class="form-control" ng-model="attribute.selectedOption.name">
<option ng-repeat="option in attribute.options | optionFilter : selection.attributes[attribute.parentAttr]">{{option.name}}</option>
</select>
</div>
</div>
<div ng-repeat="selection in vm.selectionData">
<div ng-repeat="(key, attribute) in selection.attributes">
<span>{{key}}</span>
<select ng-model="attribute.selectedOption" ng-options="attribute.name for attribute in (attribute.options | optionFilter : selection.attributes[attribute.parentAttr]) track by attribute.id">
</select>
</div>
</div>
myApp.filter('optionFilter', function() {
return function(items, parent) {
var result = [];
if (parent) {
for (var i = 0; i < items.length; i++) {
console.log(items[0].parent, parent.selectedOption.name);
if (items[i].parent === parent.selectedOption.name) {
result.push(items[i]);
}
}
return result;
} else {
return items;
}
}
});
myApp.controller("MyCtrl", function($scope) {
this.selectionData = [{
selectionType: "Geography",
attributes: {
country: {
parentAttr: "none",
options: [{
name: "India",
parent: "None",
id: 1
}, {
name: "Bangladesh",
parent: "None",
id: 2
}, {
name: "Afganistan",
parent: "None",
id: 3
}],
selectedOption: {
name: "India",
id: 1
}
},
state: {
parentAttr: "country",
options: [{
name: "Rajasthan",
parent: "India",
id: 1
}, {
name: "Haryana",
parent: "India",
id: 2
}, {
name: "Dhaka",
parent: "Bangladesh",
id: 3
}, {
name: "Kabul",
parent: "Afganistan",
id: 4
}],
selectedOption: {
name: "Rajasthan",
id: 1
}
},
city: {
parentAttr: "state",
options: [{
name: "Kota",
parent: "Rajasthan",
id: 1
}, {
name: "Sirsa",
parent: "Haryana",
id: 2
}, {
name: "Alwar",
parent: "Rajasthan",
id: 3
}, {
name: "Gurgaon",
parent: "Haryana",
id: 4
}, {
name: "Kabul",
parent: "Kabul",
id: 5
},{
name: "Dhaka",
parent: "Dhaka",
id: 6
}
],
selectedOption: {
name: "Kota",
id: 1
}
}
},
}];
});
<div ng-repeat="(key, item) in data">
<span>{{key}}</span>
<select ng-model="item.selectedOption" ng-options="option.value for option in (item.availableOptions | optionFilter : data[item.parent] : item) track by option.id">
</select>
</div>
this.data = {
Country: {
parent: "None",
availableOptions: [{
value: "United States",
parentValue: "None",
id: 1
}, {
value: "China",
parentValue: "None",
id: 2
}, {
value: "India",
parentValue: "None",
id: 3
}],
selectedOption: {
value: "United States",
parentValue: "None",
id: 1
}
},
State: {
parent: "Country",
availableOptions: [{
value: "California",
parentValue: "United States",
id: 1
}, {
value: "Shanghai",
parentValue: "China",
id: 2
}, {
value: "Delhi",
parentValue: "India",
id: 3
}],
selectedOption: {
value: "California",
parentValue: "United States",
id: 1
}
},
City: {
parent: "State",
availableOptions: [{
value: "Greenfield",
parentValue: "California",
id: 1
}, {
value: "Shanghai",
parentValue: "Shanghai",
id: 2
}, {
value: "New Delhi",
parentValue: "Delhi",
id: 3
}],
selectedOption: {
value: "Greenfield",
parentValue: "California",
id: 1
}
}
};
{{key}}
{{option.name}
HTML(ng选项):
<div ng-repeat="selection in vm.selectionData">
<div ng-repeat="(key, attribute) in selection.attributes">
<span>{{key}}</span>
<select class="form-control" ng-model="attribute.selectedOption.name">
<option ng-repeat="option in attribute.options | optionFilter : selection.attributes[attribute.parentAttr]">{{option.name}}</option>
</select>
</div>
</div>
<div ng-repeat="selection in vm.selectionData">
<div ng-repeat="(key, attribute) in selection.attributes">
<span>{{key}}</span>
<select ng-model="attribute.selectedOption" ng-options="attribute.name for attribute in (attribute.options | optionFilter : selection.attributes[attribute.parentAttr]) track by attribute.id">
</select>
</div>
</div>
myApp.filter('optionFilter', function() {
return function(items, parent) {
var result = [];
if (parent) {
for (var i = 0; i < items.length; i++) {
console.log(items[0].parent, parent.selectedOption.name);
if (items[i].parent === parent.selectedOption.name) {
result.push(items[i]);
}
}
return result;
} else {
return items;
}
}
});
myApp.controller("MyCtrl", function($scope) {
this.selectionData = [{
selectionType: "Geography",
attributes: {
country: {
parentAttr: "none",
options: [{
name: "India",
parent: "None",
id: 1
}, {
name: "Bangladesh",
parent: "None",
id: 2
}, {
name: "Afganistan",
parent: "None",
id: 3
}],
selectedOption: {
name: "India",
id: 1
}
},
state: {
parentAttr: "country",
options: [{
name: "Rajasthan",
parent: "India",
id: 1
}, {
name: "Haryana",
parent: "India",
id: 2
}, {
name: "Dhaka",
parent: "Bangladesh",
id: 3
}, {
name: "Kabul",
parent: "Afganistan",
id: 4
}],
selectedOption: {
name: "Rajasthan",
id: 1
}
},
city: {
parentAttr: "state",
options: [{
name: "Kota",
parent: "Rajasthan",
id: 1
}, {
name: "Sirsa",
parent: "Haryana",
id: 2
}, {
name: "Alwar",
parent: "Rajasthan",
id: 3
}, {
name: "Gurgaon",
parent: "Haryana",
id: 4
}, {
name: "Kabul",
parent: "Kabul",
id: 5
},{
name: "Dhaka",
parent: "Dhaka",
id: 6
}
],
selectedOption: {
name: "Kota",
id: 1
}
}
},
}];
});
<div ng-repeat="(key, item) in data">
<span>{{key}}</span>
<select ng-model="item.selectedOption" ng-options="option.value for option in (item.availableOptions | optionFilter : data[item.parent] : item) track by option.id">
</select>
</div>
this.data = {
Country: {
parent: "None",
availableOptions: [{
value: "United States",
parentValue: "None",
id: 1
}, {
value: "China",
parentValue: "None",
id: 2
}, {
value: "India",
parentValue: "None",
id: 3
}],
selectedOption: {
value: "United States",
parentValue: "None",
id: 1
}
},
State: {
parent: "Country",
availableOptions: [{
value: "California",
parentValue: "United States",
id: 1
}, {
value: "Shanghai",
parentValue: "China",
id: 2
}, {
value: "Delhi",
parentValue: "India",
id: 3
}],
selectedOption: {
value: "California",
parentValue: "United States",
id: 1
}
},
City: {
parent: "State",
availableOptions: [{
value: "Greenfield",
parentValue: "California",
id: 1
}, {
value: "Shanghai",
parentValue: "Shanghai",
id: 2
}, {
value: "New Delhi",
parentValue: "Delhi",
id: 3
}],
selectedOption: {
value: "Greenfield",
parentValue: "California",
id: 1
}
}
};
{{key}}
JS:
<div ng-repeat="selection in vm.selectionData">
<div ng-repeat="(key, attribute) in selection.attributes">
<span>{{key}}</span>
<select class="form-control" ng-model="attribute.selectedOption.name">
<option ng-repeat="option in attribute.options | optionFilter : selection.attributes[attribute.parentAttr]">{{option.name}}</option>
</select>
</div>
</div>
<div ng-repeat="selection in vm.selectionData">
<div ng-repeat="(key, attribute) in selection.attributes">
<span>{{key}}</span>
<select ng-model="attribute.selectedOption" ng-options="attribute.name for attribute in (attribute.options | optionFilter : selection.attributes[attribute.parentAttr]) track by attribute.id">
</select>
</div>
</div>
myApp.filter('optionFilter', function() {
return function(items, parent) {
var result = [];
if (parent) {
for (var i = 0; i < items.length; i++) {
console.log(items[0].parent, parent.selectedOption.name);
if (items[i].parent === parent.selectedOption.name) {
result.push(items[i]);
}
}
return result;
} else {
return items;
}
}
});
myApp.controller("MyCtrl", function($scope) {
this.selectionData = [{
selectionType: "Geography",
attributes: {
country: {
parentAttr: "none",
options: [{
name: "India",
parent: "None",
id: 1
}, {
name: "Bangladesh",
parent: "None",
id: 2
}, {
name: "Afganistan",
parent: "None",
id: 3
}],
selectedOption: {
name: "India",
id: 1
}
},
state: {
parentAttr: "country",
options: [{
name: "Rajasthan",
parent: "India",
id: 1
}, {
name: "Haryana",
parent: "India",
id: 2
}, {
name: "Dhaka",
parent: "Bangladesh",
id: 3
}, {
name: "Kabul",
parent: "Afganistan",
id: 4
}],
selectedOption: {
name: "Rajasthan",
id: 1
}
},
city: {
parentAttr: "state",
options: [{
name: "Kota",
parent: "Rajasthan",
id: 1
}, {
name: "Sirsa",
parent: "Haryana",
id: 2
}, {
name: "Alwar",
parent: "Rajasthan",
id: 3
}, {
name: "Gurgaon",
parent: "Haryana",
id: 4
}, {
name: "Kabul",
parent: "Kabul",
id: 5
},{
name: "Dhaka",
parent: "Dhaka",
id: 6
}
],
selectedOption: {
name: "Kota",
id: 1
}
}
},
}];
});
<div ng-repeat="(key, item) in data">
<span>{{key}}</span>
<select ng-model="item.selectedOption" ng-options="option.value for option in (item.availableOptions | optionFilter : data[item.parent] : item) track by option.id">
</select>
</div>
this.data = {
Country: {
parent: "None",
availableOptions: [{
value: "United States",
parentValue: "None",
id: 1
}, {
value: "China",
parentValue: "None",
id: 2
}, {
value: "India",
parentValue: "None",
id: 3
}],
selectedOption: {
value: "United States",
parentValue: "None",
id: 1
}
},
State: {
parent: "Country",
availableOptions: [{
value: "California",
parentValue: "United States",
id: 1
}, {
value: "Shanghai",
parentValue: "China",
id: 2
}, {
value: "Delhi",
parentValue: "India",
id: 3
}],
selectedOption: {
value: "California",
parentValue: "United States",
id: 1
}
},
City: {
parent: "State",
availableOptions: [{
value: "Greenfield",
parentValue: "California",
id: 1
}, {
value: "Shanghai",
parentValue: "Shanghai",
id: 2
}, {
value: "New Delhi",
parentValue: "Delhi",
id: 3
}],
selectedOption: {
value: "Greenfield",
parentValue: "California",
id: 1
}
}
};
myApp.filter('optionFilter',function(){
返回函数(项目、父项){
var结果=[];
如果(家长){
对于(变量i=0;i
参考资料:
myApp.filter('optionFilter', function() {
return function(items, parent, self) {
var result = [];
if (parent) {
for (var i = 0; i < items.length; i++) {
if (parent.selectedOption !== null && items[i].parentValue === parent.selectedOption.value) {
result.push(items[i]);
}
}
if (self.selectedOption === null) {
self.selectedOption = result[0];
}
return result;
} else {
return items;
}
}
});
如何在原生js中应用它,或者如何在m中应用它