【问题标题】:Multi select based on another multi select基于另一个多选的多选
【发布时间】:2017-03-27 21:00:30
【问题描述】:

我需要根据这些多选中的国家/地区过滤城市。 我使用ui-select 进行多选,感谢@tanmay。

请看看这个小提琴。

Fiddle

【问题讨论】:

  • 您能否详细说明您的问题是什么?谢谢。
  • 你能解释一下你想要达到的目标吗?
  • @Nishant123 正如您在小提琴中看到的,我需要根据相关国家/地区过滤城市。这意味着当我选择美国时,只会出现相关城市。请看一下小提琴。

标签: angularjs ui-select


【解决方案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

【讨论】:

  • 我还需要反向过滤。这意味着当我选择城市时,填写了国家选项。你能帮我解决这个问题吗?
  • 两种方式过滤可能会导致错误。过滤国家或城市
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多