在Angular JS中尝试创建两个下拉列表

时间:2018-10-29 06:03:23

标签: angularjs dropdown

我正在尝试在Angular JS中创建两个下拉菜单 第一个下拉菜单将具有以下值: “ A”,“ B”,“ C” 当我选择A时,它应该在第二个下拉菜单中显示A1,A2,A3,A4。类似地,它应该显示B1,B2,B3,B4和C,它应该显示C1,C2,C3,C4

。下面是我的两个JSON

$scope.column = [{
        colid: 1,
        name:"A"
        }, {
        colid: 2,
        name: "B"
       }, {
        colid: 3,
        name: "C"
       }];

$scope.Value=[{"A":"A1","B":"B1","C":"C1"},
{"A":"A2","B":"B2","C":"C2"},
{"A":"A3","B":"B1","C":"C3"},
{"A":"A1","B":"B3","C":"C1"},
{"A":"A4","B":"B4","C":"C4"},
];

下面是我尝试过的代码

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app = angular.module('colFilter', ['ui', 'ui.filters']);
app.controller("colFilterCtrl", function ($scope, $timeout) {
$scope.columnList = [{
        colid: 1,
        name:"A"
        }, {
        colid: 2,
        name: "B"
       }, {
        colid: 3,
        name: "C"
       }];

$scope.Value=[{"A":"A1","B":"B1","C":"C1"},
              {"A":"A2","B":"B2","C":"C2"},
              {"A":"A3","B":"B1","C":"C3"},
              {"A":"A1","B":"B3","C":"C1"},
              {"A":"A4","B":"B4","C":"C4"},
];
};
</script>
<div id="colFilterApp" data-ng-app="colFilter">
    <div id="colFilterAppCtrl" data-ng-controller="colFilterCtrl" >
<select  id="column" name="columns" ng-options="column.name for column in columnList" ng-model="selectedItem">
     <option value="All">--Select--</option>
</select>
<select  id="value" name="values">
     <option value="All">--Select--</option>
     <option ng-repeat="val in Value"  value="{{val.selectedItem}}">{{val.selectedItem}}</option>
</select> 
</div>
</div>

有帮助吗?预先感谢

3 个答案:

答案 0 :(得分:1)

使用以下代码段。

  1. $scope.$watch放在selectedItem
  2. 使用$scope.Value.filter

function colFilterCtrl($scope) {

  $scope.selectedItem = '';
  $scope.search = [];

  $scope.columnList = [{
    colid: 1,
    name: "A"
  }, {
    colid: 2,
    name: "B"
  }, {
    colid: 3,
    name: "C"
  }];

  $scope.Value = [
    {"A":"A1","B":"B1","C":"C1"},
    {"A":"A2","B":"B2","C":"C2"},
    {"A":"A3","B":"B1","C":"C3"},
    {"A":"A1","B":"B3","C":"C1"},
    {"A":"A4","B":"B4","C":"C4"}
  ];


  $scope.$watch('selectedItem', function() {
    $scope.search = [];
    $scope.Value.filter(function(obj) {
      let values = Object.values(obj)[Object.keys(obj).indexOf($scope.selectedItem.name)];
      $scope.search.push(values);
    });
    
    // remove duplicate values
    $scope.search = $scope.search.filter(function(item, pos) {
    return $scope.search.indexOf(item) == pos;
})

  });

};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app>
<div ng-controller="colFilterCtrl">
    
    <select name='columns' id="column"  ng-model="selectedItem" ng-options="column as column.name for column in columnList"> <option value="" disabled>Select</option> </select>
            
    <select name='values' id="value" ng-model="selectSubCategory" ng-options="o as o for o in search" required> <option value="" disabled>Select</option> </select>
      
  </div> 
      
</div>

答案 1 :(得分:0)

这称为级联下拉列表。在这里,我创建了一个小提琴,请对其进行检查,并让我知道https://jsfiddle.net/vinothsm92/u50hgn5a/1741/

$scope.$watch('first', function () {
        $scope.Seconds = allSeconds.filter(function (s) {
            return s.firstId == $scope.first.Id;
        });
        $scope.city = {};
        $scope.Second = {};
        $scope.cities = [];
    });

在这里,我创建了一个$watch,它将在第一个下拉菜单更改生效时生效。您可以使用$watch click事件来绑定数据,而不是绑定到ng-click

答案 2 :(得分:0)

这里是另一个示例。在此示例中,将创建一个数据对象,其中包含每个第一个下拉项的映射,这些映射可在不久的将来通过数据库进行填充。这是JSFiddle https://jsfiddle.net/whb1saxg/1/

$scope.columnList = [
                    {
                        colid: 1,
                        name: "A"
                    }, {
                        colid: 2,
                        name: "B"
                    }, {
                        colid: 3,
                        name: "C"
                    }
                ];

                $scope.selectedItem = $scope.selectedItemValues  = [];

                $scope.childValues = {
                    "A": [{
                        colid: 1,
                        name: "A1"
                    }, {
                        colid: 2,
                        name: "A2"
                    }, {
                        colid: 3,
                        name: "A3"
                    }],

                    "B": [{
                        colid: 4,
                        name: "B1"
                    }, {
                        colid: 5,
                        name: "B2"
                    }, {
                        colid: 6,
                        name: "B3"
                    }],
                    "C": [{
                        colid: 7,
                        name: "C1"
                    }, {
                        colid: 8,
                        name: "C2"
                    }, {
                        colid: 9,
                        name: "C3"
                    }]
                };
$scope.ddlChange = function () {
                $scope.Value = ($scope.selectedItem !== undefined) ? $scope.childValues[$scope.selectedItem.name] : [];
            };