2017-03-27 115 views
1

我需要根据这些多重选择中的国家过滤城市。 我使用ui-select进行多选,感谢@tanmay。基于另一个多重选择的多重选择

请看看这个小提琴。

Fiddle

+0

能否请你扩大你的问题其实是可以做是什么?谢谢。 –

+0

你能解释一下你想达到什么吗? – Nishant123

+0

@ Nishant123正如您在小提琴中所看到的,我需要根据相关国家过滤城市。这意味着当我选择美国时,只有相关的城市出现。请看小提琴。 –

回答

1

您可以添加ng-change一个函数,将返回所有城市被选择的国家

$scope.getCityList=function(){ 

     var sampletemp = []; //temp array to hold filtered values 
     $scope.selected.country.forEach(function(country) { 
     //bjectFromArrayFilter --> A filter function that will do the filtering 
     var temp = objectFromArrayFilter($scope.samples,'country',country); 
     sampletemp = sampletemp.concat(temp); 
     //Filter duplicate city names 
     $scope.uniquecity = $filter('unique')(sampletemp, 'city'); 
     //Reset all the already selected values 
     $scope.selected.city= []; 

     $scope.city = $scope.uniquecity.map(function(item) { 
     return item.city 
     }) 
    } 

过滤功能。

您也可以使用此功能执行自定义过滤。只是传递对象的数组,过滤键和值相匹配

var objectFromArrayFilter=function(arrayOptions, key, value) { 
    var filterResult = arrayOptions.filter(function(val) { 
      return val[key] === value; 
    }); 
    return filterResult; 
}; 

FULL EXAMPLE

类似的功能,可用于过滤其它$scope.samples

+0

我也需要反向过滤。这意味着当我选择这个城市时,国家选项会被填满。你能帮我解答吗? –

+0

过滤两种方式可能会导致错误。过滤国家或城市 – Nishant123