У меня есть список из 1400 мест по всему миру, и я отображаю их с помощью ng-repeat. Я хочу иметь раскрывающиеся списки, чтобы сузить список с помощью функции фильтра, чтобы я мог видеть только местоположения в регионе: Америка, страна: Канада, например. Я заставил раскрывающиеся фильтры работать правильно, но моя проблема заключается в изменении значений раскрывающихся списков. Если я выберу параметр «Регион» в первом раскрывающемся списке, в раскрывающемся списке стран должны быть только страны этого региона.
Вот мой шаблон:
Region:<select ng-model='RegionLocation.Region' ng-options="location.Region for location in RegionOptions.Regions"></select>
Country:<select ng-model='CountryLocation.Country' ng-options="location.Country for location in CountryOptions.Countries"></select>
<div>
<md-card data-ng-repeat="location in LocationCtrl.Locationlist | filter:RegionFilter | filter:CountryFilter ">
<md-card-content>
<h2>{{::location.LID}}</h2>
</md-card-content>
</md-card>
</div>
Вот мой Controller.js
function LocationController(LocationList, $scope) {
var LocationCtrl = this;
LocationCtrl.Locationlist = LocationList;
LocationCtrl.Regions = filterRegions(LocationList);
LocationCtrl.Regions.unshift({
Region: 'Show All'
})
$scope.RegionOptions = {
Regions:LocationCtrl.Regions,
}
$scope.RegionLocation = {
Region: $scope.RegionOptions.Regions[0],
}
$scope.CountryOptions = {
Countries:getCountries()
};
$scope.CountryLocation = {
Country: $scope.CountryOptions.Countries[0]
};
$scope.RegionFilter = function (data) {
if($scope.RegionLocation.Region.Region == 'Show All'){
return true;
} else if(data.Region == $scope.RegionLocation.Region.Region){
return true;
} else{
return false;
}
};
$scope.CountryFilter = function (data) {
if($scope.CountryLocation.Country.Country == 'Show All'){
return true;
} else if(data.Country == $scope.CountryLocation.Country.Country){
return true;
} else{
return false;
}
};
function getCountries(){
LocationCtrl.Countries = filterCountries(LocationList);
LocationCtrl.Countries.unshift({
Country: 'Show All'
});
return LocationCtrl.Countries;
};
function filterRegions(arr) {
var f = []
return arr.filter(function(n) {
return f.indexOf(n.Region) == -1 && f.push(n.Region)
})
};
function filterCountries(arr){
var f = [];
return arr.filter(function(n) {
return f.indexOf(n.Country) == -1 && f.push(n.Country)
})
};
}
Я также понимаю, что мой код не очень чистый и не простой, поэтому предложения по его упрощению более чем приветствуются.
Спасибо!!
[{"LID":"AB02","City":"Calgary","State":"Alberta","Country":"Canada","Region":"Americas","Latitude"XXXXXX"Longitude"XXXXX},{"LID":"AB08","City":"Canmore","State":"Alberta","Country":"Canada","Region":"Americas","Latitude":XXXXXX,"Longitude":XXXXXXX},
- person Wish I Knew this stuff   schedule 03.08.2017