【问题标题】:Create two dynamic dropdown menus创建两个动态下拉菜单
【发布时间】:2017-01-06 17:35:27
【问题描述】:

我目前正在尝试创建一种表单来从两个相关的下拉菜单中选择选项。如果您要更改上方的选项,则下方的数据也应根据上方的选项更改。
因此,我创建了一个小示例,您首先选择一个国家/地区。在此之后,您可以从这个国家/地区选择一个城市。
数据存储在本地数组中,稍后将通过服务获取。数据应该保存在对象数组中,因为这是服务提供数据的方式,因此不需要转换。

在这里你可以看到我非常简单的代码,由于我不知道如何创建这样一个“交流”表单,所以目前只是一个备用骨架。我希望你们能帮助我解决这种问题。

HTML

<body ng-app="AngularApp">
  <div ng-controller="MainCtrl as Main">
    <h1>{{Main.message}}</h1>
    country:
    <select name="country">
      <option></option>
    </select>
    <br/>city:
    <select name="city">
      <option></option>
    </select>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</body>

App.js

var app = angular.module('AngularApp', []);

app.controller("MainCtrl", function() {
  var vm = this;

  vm.message = "Select your destination";
  vm.data = [
    { city: 'Berlin',         country: 'Germany' },
    { city: 'Hamburg',        country: 'Germany' },
    { city: 'Munich',         country: 'Germany' },
    { city: 'New York',       country: 'USA' },
    { city: 'Whashington DC', country: 'USA' },
    { city: 'Paris',          country: 'France' }
  ];
});

var app = angular.module('AngularApp', []);

app.controller("MainCtrl", function() {
  var vm = this;

  vm.message = "Select your destination";
  vm.data = [
    { city: 'Berlin',         country: 'Germany' },
    { city: 'Hamburg',        country: 'Germany' },
    { city: 'Munich',         country: 'Germany' },
    { city: 'New York',       country: 'USA' },
    { city: 'Whashington DC', country: 'USA' },
    { city: 'Paris',          country: 'France' }
  ];
});
<body ng-app="AngularApp">
  <div ng-controller="MainCtrl as Main">
    <h1>{{Main.message}}</h1>
    country:
    <select name="country">
      <option></option>
    </select>
    <br/>city:
    <select name="city">
      <option></option>
    </select>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</body>

补充说明:
因此,如果您选择“德国”作为您的国家/地区,则在城市选择中只有选项“柏林”、“汉堡”和“慕尼黑”应该可用,因为属性 country。不会显示其他城市,例如巴黎或纽约。


编辑 如果默认选择一个国家/地区也很好,这样就不会出现空的下拉菜单。

【问题讨论】:

  • 随心所欲地查看我的更新答案。

标签: javascript jquery html angularjs


【解决方案1】:

var app = angular.module('AngularApp', ['angular.filter']);

app.controller("MainCtrl", function() {
  var vm = this;

  vm.message = "Select Your Destination";
  vm.data = [
    { city: 'Berlin',         country: 'Germany' },
    { city: 'Hamburg',        country: 'Germany' },
    { city: 'Munich',         country: 'Germany' },
    { city: 'New York',       country: 'USA' },
    { city: 'Whashington DC', country: 'USA' },
    { city: 'Paris',          country: 'France' }
  ];
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.11/angular-filter.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<body ng-app="AngularApp">
  <div ng-controller="MainCtrl as Main" class="container">
    <h1>{{Main.message}}</h1>
    <form>
      <div class="form-group">
        <label>Country</label>
        <select name="country" ng-model="country" class="form-control">
          <option value="" selected="selected">Choose one</option>
          <option ng-repeat="option in Main.data | unique: 'country'" value="{{option.country}}">{{option.country}}</option>
        </select>
      </div>
      <div class="form-group">
        <label>City</label>
        <select name="city" ng-model="city" class="form-control">
          <option value="" selected="selected">Choose one</option>
          <option ng-repeat="option in Main.data" ng-if="option.country == country" value="{{option.city}}">{{option.city}}</option>
        </select>
      </div>
      <br />
      <button type="submit" class="btn btn-default">Submit</button>
    </form>
  </div>
</body>

【讨论】:

    【解决方案2】:

    您可以使用'angular-filter' 模块来显示独特的选项。

    <select name="country" ng-model="country">
        <option ng-repeat="option in Main.data | unique: 'country'"
                value="{{option.country}}">{{option.country}}</option>
    </select>
    
    <select name="city" ng-model="city">
        <option ng-repeat="option in Main.data" ng-if="option.country == country"
                value="{{option.city}}">{{option.city}}</option>
    </select>
    

    示例

    我在表单中添加了一些 Bootstrap 类以清理其表示。 :)

    var app = angular.module('AngularApp', ['angular.filter']);
    
    app.controller("MainCtrl", function() {
      var vm = this;
    
      vm.message = "Select Your Destination";
      vm.data = [
        { city: 'Berlin',         country: 'Germany' },
        { city: 'Hamburg',        country: 'Germany' },
        { city: 'Munich',         country: 'Germany' },
        { city: 'New York',       country: 'USA' },
        { city: 'Whashington DC', country: 'USA' },
        { city: 'Paris',          country: 'France' }
      ];
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.11/angular-filter.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    
    <body ng-app="AngularApp">
      <div ng-controller="MainCtrl as Main" class="container">
        <h1>{{Main.message}}</h1>
        <form>
          <div class="form-group">
            <label>Country</label>
            <select name="country" ng-model="country" class="form-control">
              <option ng-repeat="option in Main.data | unique: 'country'" value="{{option.country}}">{{option.country}}</option>
            </select>
          </div>
          <div class="form-group">
            <label>City</label>
            <select name="city" ng-model="city" class="form-control">
              <option ng-repeat="option in Main.data" ng-if="option.country == country" value="{{option.city}}">{{option.city}}</option>
            </select>
          </div>
          <br />
          <button type="submit" class="btn btn-default">Submit</button>
        </form>
      </div>
    </body>

    【讨论】:

    • 好一个。保留它。
    • 有没有办法默认设置一个国家,这样你的性感表单在加载时就不会是空的;-)?
    • 您的意思是为每个国家/地区设置城市默认值?
    • 您可以在您的国家/地区选项中设置ng-init 以指向默认城市。尝试在此处查找信息:"Angular js init ng-model from default values"
    • 我想将列表中的第一个国家和该国家的第一个城市设置为默认值。由于数据的变化,无法将ng-init-value 设置为静态字符串。如何将第一个国家和合适的城市设置为默认值?
    【解决方案3】:

    HTML 代码

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>Login</title>
      </head>
      <body ng-app="AngularApp">
    
      <div ng-controller="MainCtrl as Main">
        <h1>{{Main.message}}</h1>
        country:
        <select name="country" ng-model="cont" ng-init="cont = cont || 'Germany'">   
            <option ng-selected="$first" ng-repeat="con in Main.data | unique:'country'">{{con.country}}</option> 
        </select>
        <br/>
        city:
        <select name="city" ng-model="cit">
            <option ng-selected="$first" ng-repeat="ct in Main.data | filter:{country: cont}">{{ct.city}}</option>
        </select>
      </div>
    
    
    
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js" type="text/javascript"></script>
      <script src="app.js"></script>
      </body>
    </html>
    

    在您的 app.js 中创建一个名为“unique”的过滤器。所以你的app.js 像这样..

    var app = angular.module('AngularApp', []);
    app.controller("MainCtrl", function() {
        var vm = this;
    
        vm.message = 'select your destination';
    
        vm.data = [{city: 'Berlin', country: 'Germany'},
                   {city: 'Hamburg', country: 'Germany'},
                   {city: 'Munich', country: 'Germany'},
                   {city: 'New York', country: 'USA'},
                   {city: 'Whashington DC', country: 'USA'},
                   {city: 'Paris', country: 'France'}
                ];
    });
    app.filter('unique', function() {
      return function (arr, field) {
        var o = {}, i, l = arr.length, r = [];
        for(i=0; i<l;i+=1) {
          o[arr[i][field]] = arr[i];
        }
        for(i in o) {
          r.push(o[i]);
        }
        return r;
      };
    });
    

    【讨论】:

    • 好一个。保留它。
    【解决方案4】:

    您可以使用以下 HTML:

    Country:
    <select name="country" ng-model="country">
      <option ng-repeat="option in Main.data" value="{{option.country}}">{{option.country}}</option>
    </select>
    
    City:
    <select name="city" ng-model="city">
      <option ng-repeat="option in Main.data" ng-if="option.country == country" value="{{option.city}}">{{option.city}}</option>
    </select>
    

    【讨论】:

    • 感谢您的回答,但“德国”在选择中出现了 3 次。它应该只显示一次,以防止不必要的长列表。
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2012-09-03
    • 2022-01-06
    • 2012-08-11
    • 2023-03-22
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多