Angularjs 使用下拉菜单过滤数据 [英] Angularjs Filter data with dropdown

查看:41
本文介绍了Angularjs 使用下拉菜单过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angularjs 和 javascript 的新手,所以请善待,我有两个下拉项(Ionic Select),它们都保存来自服务的数据.问题是我需要过滤它们才能一起工作,例如:如果我在第一个下拉列表中选择一家公司,则只有该公司内部的代表应显示在另一个下拉列表中.

我尝试使用 |filter: byID 正如我在 Angularjs 文档中遵循的那样,但我不认为这是正确的做法,我不知道.

HTML:

<选择><option ng-repeat="x in company">{{x.compname}}</option><选择的选项>选择</选项></选择><div class="list"><label class="item item-input item-select"><div class="input-label">代表:

<选择><option ng-repeat="x 代表">{{x.repname}}</option><选择的选项>选择</选项></选择>

Javascript:

/*==========================获取所有公司==========================*/$http.get("http://localhost:15021/Service1.svc/GetAllComp").成功(功能(数据){var obj = 数据;var SComp = [];angular.forEach(obj, function(index, element) {angular.forEach(索引,函数(索引N,元素N){SComp.push({compid: indexN.CompID, compname: indexN.CompName});$scope.company = SComp;});});})/*========================获取所有公司==========================*//*========================获取所有代表==========================*/$http.get("http://localhost:15021/Service1.svc/GetAllReps").成功(功能(数据){var obj = 数据;var SReps = [];angular.forEach(obj, function(index, element) {angular.forEach(索引,函数(索引N,元素N){SReps.push({repid: indexN.RepID, repname: indexN.RepName, fkc :indexN.fk_CompID});$scope.represent = SReps;});});})/*========================获取所有代表==========================*/

解决方案

你可以像我的解决流程一样解决这个问题:我的解决方案就像你的问题.首先显示地区列表,然后根据选择的地区显示Thana列表.使用过滤器表达式

在 HTML 中:

<form class="form-horizo​​ntal"><div class="form-group"><div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i>地区列表</label></div><div class="col-md-4"><select class="form-control" ng-model="selectedDist" ng-options="district.name for District in Districts"><option value="">选择</option></选择>

<div class="form-group"><div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i>塔纳列表</label></div><div class="col-md-4"><select class="form-control" ng-model="selectedThana" ng-options="thana.name for thana in thanas | filter: filterExpression"><option value="">选择</option></选择>

</表单>

在控制器中:

 $scope.selectedDist={};$scope.districts = [{id: 1, name: 'Dhaka'},{id: 2, name: 'Goplaganj'},{id: 3, name: 'Faridpur'}];$scope.thanas = [{id: 1, name: 'Mirpur', dId: 1},{id: 2, name: 'Uttra', dId: 1},{id: 3, name: 'Shahabag', dId: 1},{id: 4, name: 'Kotalipara', dId: 2},{id: 5, name: 'Kashiani', dId: 2},{id: 6, name: 'Moksedpur', dId: 2},{id: 7, name: 'Vanga', dId: 3},{id: 8, name: 'faridpur', dId: 3}];$scope.filterExpression = 函数(thana){返回 (thana.dId === $scope.selectedDist.id );};

n.b: filterexpression 是一个自定义函数,当所选地区ID相等时返回值.

I am kinda new in angularjs and javascript so please be kind, I have two dropdown items (Ionic Select) both of them holds data from a service. The issue is that I need to filter them in order to work together like: if I choose a company in the first dropdown list, only the reps inside of that company should display in the other dropdown list.

I tried using | filter: byID as I followed in Angularjs documentation but I do not think it is the right way of doing this don't know.

HTML:

<label class="item item-input item-select"">
    <div class="input-label">
      Company:
    </div>
    <select>
      <option ng-repeat="x in company">{{x.compname}}</option>
      <option selected>Select</option>      
    </select>
  </label>
   <div class="list">
  <label class="item item-input item-select">
    <div class="input-label">
      Rep:
    </div>
    <select>
       <option ng-repeat="x in represent">{{x.repname}}</option>
      <option selected>Select</option>
    </select>
  </label>

Javascript:

/*=========================Get All Companies=========================*/
 $http.get("http://localhost:15021/Service1.svc/GetAllComp")
      .success(function(data) {

        var obj = data;
        var SComp = [];


    angular.forEach(obj, function(index, element) {

    angular.forEach(index, function(indexN, elementN) {        

        SComp.push({compid: indexN.CompID, compname: indexN.CompName});

        $scope.company = SComp;
    });    

    });          
            })
/*=========================Get All Companies=========================*/

/*=========================Get All Reps=========================*/
 $http.get("http://localhost:15021/Service1.svc/GetAllReps")
      .success(function(data) {

        var obj = data;
        var SReps = [];


    angular.forEach(obj, function(index, element) {

    angular.forEach(index, function(indexN, elementN) {        

        SReps.push({repid: indexN.RepID, repname: indexN.RepName, fkc :indexN.fk_CompID});

        $scope.represent = SReps;
    });    

    });          
            })
/*=========================Get All Reps=========================*/

解决方案

You may solve this problem like my solution process: my solution like your problem. at first show District list and show Thana list according to selected District. using filter expression

In HTML:

<div>
        <form class="form-horizontal">
            <div class="form-group">
                <div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> District List</label></div>
                <div class="col-md-4">
                    <select class="form-control" ng-model="selectedDist" ng-options="district.name for district in districts">
                        <option value="">Select</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> Thana List</label></div>
                <div class="col-md-4">
                    <select class="form-control" ng-model="selectedThana" ng-options="thana.name for thana in thanas | filter: filterExpression">
                        <option value="">Select</option>
                    </select>
                </div>
            </div>
        </form>
    </div>

In controller:

            $scope.selectedDist={};
            $scope.districts = [
                {id: 1, name: 'Dhaka'},
                {id: 2, name: 'Goplaganj'},
                {id: 3, name: 'Faridpur'}
            ];

            $scope.thanas = [
                {id: 1, name: 'Mirpur', dId: 1},
                {id: 2, name: 'Uttra', dId: 1},
                {id: 3, name: 'Shahabag', dId: 1},
                {id: 4, name: 'Kotalipara', dId: 2},
                {id: 5, name: 'Kashiani', dId: 2},
                {id: 6, name: 'Moksedpur', dId: 2},
                {id: 7, name: 'Vanga', dId: 3},
                {id: 8, name: 'faridpur', dId: 3}
            ];
            $scope.filterExpression = function(thana) {
                return (thana.dId === $scope.selectedDist.id );
            };

N.B: Here filterExpression is a custom function that return values when selected district id equal dId in thana.

这篇关于Angularjs 使用下拉菜单过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆