[英]Filter Json in Code AngularJS
Hello I have the following JSONs 您好,我有以下JSON
$scope.Facilities=
[
{
Name: "-Select-",
Value: 0,
RegionNumber: 0
},
{
Name: "Facility1",
Value: 1,
RegionNumber: 1
},
{
Name: "Facility2",
Value: 2,
RegionNumber: 1
},
{
Name: "Facility3",
Value: 3,
RegionNumber: 2
}
];
$scope.Regions=
[
{
Name: "-Select-",
RegionNumber: 0
},
{
Name: "USA",
RegionNumber: 1
},
{
Name: "Mexico",
RegionNumber: 2
}
];
I would have two DropdownLists in my app which will have one of these Jsons assigned to it. 我的应用程序中将有两个DropdownLists,其中将为其分配这些Json之一。
Whenever you select a Region, a ng-change would be triggered. 每当您选择一个地区时,就会触发ng-change。 What I want, is to make the Facility DDL to update it's values. 我想要的是使Facility DDL更新其值。 It would only show the Facilities which have a RegionNumber equivalent to the selected Region's RegionNumber. 它只会显示其RegionNumber等于所选Region的RegionNumber的设施。
How could I achieve this? 我怎样才能做到这一点? I'm using Angular JS, MVC... 我正在使用Angular JS,MVC ...
Note: The -Select- Value must always appear, even if it's value is zero and is not equivalent to the selected Region. 注意:-Select-值必须始终出现,即使它的值为零且不等同于所选的Region。
I think you need a data structure like below: 我认为您需要如下数据结构:
$scope.Regions=
[
{
Name: "-Select-",
facilities : {
facilityId: 1,
facilityName: "facility1"
},
{
facilityId: 2,
facilityName: "facility2"
}
},
{
Name: "USA",
facilities : [{
facilityId: 1,
facilityName: "facility1"
},
{
facilityId: 2,
facilityName: "facility2"
}]
},
];
So, you could reference them like below: 因此,您可以像下面这样引用它们:
For the dropdown of Regions, you can iterate through above Data structure. 对于Regions下拉列表,您可以遍历上面的Data结构。 Store the selectedRegion in selectedRegion
Then use that for the dropdown for facilities. 存储在selectedRegion selectedRegion
然后利用它来进行下拉的设施。
While a data structure, like greengrassbluesky may simplify the result, you can accomplish the same thing with an onchange that leverages javascript filtering 虽然像greengrassbluesky这样的数据结构可以简化结果,但是您可以通过利用JavaScript过滤的onchange来完成同一件事
$scope.Facilities = masterFacilities.filter(function (el) {
return regionNumber = el.RegionNumber == $scope.SelectedRegion || el.RegionNumber == 0;
});
Here's a fiddle with an example using your lists. 这里有一个小提琴使用您的列表的一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.