【问题标题】:Issue with AngularJS DropdownAngularJS下拉问题
【发布时间】:2016-08-29 08:29:25
【问题描述】:

我已经创建了 AngularJS 可靠的下拉菜单,它可以很好地处理来自脚本文件的静态数据。现在如何将数据从 URL 绑定到这些下拉列表。我没有用于下拉菜单的单独 url,只有一个 url 提供所有内容。

我应该如何解析数据以获得所需的输出?我对此很陌生,如果有人能提供帮助,将不胜感激!

【问题讨论】:

  • 你能提供 JSON 结构的链接吗?
  • @Ritesh Kashyap:我在本地主机上使用它
  • States(country)和cities(states)应该有一个共同的key来根据下拉列表的选定值过滤数据。
  • @Ritesh Kashyap:您能否为此提供一个示例格式
  • 您可以使用控制器上的 $filter 服务从给定的主列表中搜索州和城市,并根据该过滤器为城市创建一个单独的列表,并将城市下拉列表与该细化列表绑定到状态变化。

标签: javascript angularjs json url multi-select


【解决方案1】:

这里是第一个工作的朋克:

http://plnkr.co/edit/okKOeQViflRseqrOYeOY?p=preview

问题是您的问题说明了一些内容,而您的演示显示了其他内容。

您可以使用 angular 直接解析 JSON 数据,然后您没有 ng-change 函数来检测选择了哪个下拉菜单。请参阅以下代码,了解我如何解析 html 本身中的数据。

                <div class="col-sm-4">
                    <am-multiselect class="input-lg"
                                    template-url="multiselect.tmpl.html"
                                    ng-model="selectedcountry" ms-header="Select country" style="width:200px;"
                                    options="c.CountryName for c in table"
                                    ng-change="setState(selectedcountry)"
                                    change="selected()"></am-multiselect>



                </div>
            </div>
            <div class="form-group">

                <div class="col-sm-4">
                    <am-multiselect class="input-lg" multiple="true" ms-selected="{{selectedState.length}} State(s) selected"
                                    ng-model="selectedState" ms-header="Select States"
                                    options="s.STATE for s in table"
                                    ng-change="setCity(selectedState)"
                                    template-url="multiselect.tmpl.html" style="width:500px;"
                                    change="selected()">
                    </am-multiselect>

                </div>
            </div>

            <div class="form-group">

                <div class="col-sm-4">
                    <am-multiselect class="input-lg" multiple="true" ms-selected="{{selectedCity.length}} City(ies) selected"
                                    ng-model="selectedCity" ms-header="Select City"
                                    options="m.CityName for m in table"
                                    template-url="multiselect.tmpl.html" style="width:500px;"
                                    change="selected()"></am-multiselect>

                </div>
            </div>

下一个下拉菜单 STATE 不会在更改时触发,即使范围发生更改。

所以使用$watch 来查看范围的更改,当它发生时将其应用到CITY

所以你的js:

angular.module('app', ['am.multiselect']);

angular.module('app')
  .controller('ctrl', function($scope, $http, $location, $filter,$window) {

    var tableData = {
      "Table": [{
        "CountryUid": 3,
        "CountryName": "INDIA",
        "STATE": "AndraPradesh",
        "CityId": 3,
        "CityName": "Vijayawada"
      }, {
        "CountryUid": 2,
        "CountryName": "USA",
        "STATE": "Florida",
        "CityId": 3,
        "CityName": "Tampa"
      }, {
        "CountryUid": 3,
        "CountryName": "INDIA",
        "STATE": "Assam",
        "CityId": 3,
        "CityName": "Jorhat"
      }]
    };

    var countries = [];
    var states = [];
    var cities = [];

    $scope.table = tableData.Table;
    $scope.countries = countries;
    $scope.states = states;
    $scope.cities = cities;
    $scope.selectedsite = null;
    $scope.selectedState = [];
    $scope.selectedCity = [];

    $scope.setState = function(country) {
      $scope.selectedStates = tableData.Table.filter(function(el) {
        return el.CountryName === country
      });
      $scope.selectedState = $scope.selectedStates.map(function(elm) {return elm.STATE;});
    }
    $scope.setCity = function(state) {
      selectedCity = tableData.Table.filter(function(el) {
        return el.STATE === state
      });
      $scope.selectedCity = selectedCity.map(function(elm) {return elm.CityName;});
    }
    $scope.$watch('selectedStates', function(newVal, oldVal, theScope) {
      $scope.selectedCity = newVal.map(function(elm) {return elm.CityName;});
      console.log(newVal,$scope.selectedCity);
    });

PS:上面的plunker不是很完美,你可能会遇到一些小问题,你应该能够解决。

【讨论】:

  • :但是我需要的主要是当我选择一个国家时,我应该只能看到该特定国家的州,而不是所有国家的州,如果我选择一个州,我应该能够单独看到该州的城市.. 上面的 plunker 选择了该国所有相应的州和城市,但也显示了所有城市和州的列表
  • 足够简单,只需复制table 的范围,并将一个范围分配给常量,即country,另一个分配给动态,即CItyNameSTATE 更新的plunker:@987654322 @
  • np!很乐意帮助你。 GL编码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多