Multi select based on another multi select

我只是一个虾纸丫 提交于 2019-12-13 17:25:21

问题


I need to filter city based on the country in these multi-selects. I used ui-select for multi-selects, Thanks to @tanmay.

Please take a look at this fiddle.

Fiddle


回答1:


You can add a function on ng-change that will return all the cities for the selected country

 $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
        })
    }

The filtering function.

You can also use this function to perform custom filtering. Just pass the array of objects, filtering key and value to match

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

FULL EXAMPLE

Similar function can be made for filtering on other $scope.samples keys



来源:https://stackoverflow.com/questions/43045705/multi-select-based-on-another-multi-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!