javascript - Checkboxlist ng-repeat过滤器动态angularjs

标签 javascript angularjs filter checkboxlist

我正在尝试在 AngularJS 中使用动态过滤器(位置 - 值,如美国、印度、加利福尼亚州等)显示员工详细信息,作为基于从数据库获取的数据的复选框列表。我尝试了多种方法都没有成功。请帮助实现 Checkboxlist 的动态过滤器。

我的代码示例如下:

  <html>
<body ng-app="myapp" ng-controller="myController">


     <div >Location</div>
        <table>
            <tbody>
                <tr ng-repeat="empL in EmpResult | unique : 'Location'">
                    <td>
                        <span>
                            <input type="checkbox" ng-model="loc" value={{empL.Location}} />
                            {{empL.Location}}
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
    <table align="left" style="width: 100%" class="table">
        <thead>
            <tr>

                <th align="left" style="width: 30%">Employee</th>
                <th align="left" style="width: 20%">Address</th>
                <th align="left" style="width: 15%">Location</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="empN in EmpResult | filter : loc">

                <td align="left" style="width: 30%">{{empN.EmpName}}</td>
                <td align="left" style="width: 10%">{{empN.Address}}</td>
                <td align="left" style="width: 15%">{{empN.Location}}</td>

            </tr>
        </tbody>
    </table>

    <script type="text/javascript">

        var myapp = angular.module('myapp', ['ui.unique'])
        .controller("myController", function ($scope, $http) {

            $http({
                method: 'Get',
                params: { strName: $scope.strName },
                url: 'Emp.asmx/GetEmpbyName'
            }).then(function (response) {
                $scope.EmpResult = response.data;
            })

        });
    </script>
</body>
</html>

最佳答案

我已经为你的问题创建了一个镜像,请看一下。我认为它应该适用于您的情况。

Plunker

<!DOCTYPE html>
<html>

  <head>
      <meta charset="utf-8" />
      <title>AngularJS Plunker</title>
      <script>
          document.write('<base href="' + document.location + '" />');
      </script>
      <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
  </head>

  <body ng-app="myApp" ng-controller="myController">
      <div ng-init="init()">Location</div>
      <table>
          <tbody>
              <tr ng-repeat="empL in locations">
                  <td>
                      <span>
                              <input type="checkbox" name="locations + $index" data-ng-model="locChkBox.loc[$index]" ng-true-value="'{{empL}}'" ng-false-value="" ng-change="repopulate()"/>
                              {{empL}}
                      </span>
                  </td>
              </tr>
          </tbody>
      </table>
      <table align="left" style="width: 100%" class="table">
          <thead>
              <tr>
                  <th align="left" style="width: 30%">Employee</th>
                  <th align="left" style="width: 20%">Address</th>
                  <th align="left" style="width: 15%">Location</th>
              </tr>
          </thead>
          <tbody>
              <tr ng-repeat="empN in EmpResult | filter : locFilter ">
  
                  <td align="left" style="width: 30%">{{empN.EmpName}}</td>
                  <td align="left" style="width: 10%">{{empN.Address}}</td>
                  <td align="left" style="width: 15%">{{empN.Location}}</td>
  
              </tr>
          </tbody>
      </table>
    <script>
      var myApp = angular.module('myApp', []);
      
      myApp.controller("myController", ['$scope', '$http', function($scope, $http) {
      
          $scope.locations = [];
          $scope.search = {};
          $scope.locChkBox = {};
          $scope.locChkBox.loc = [];
          $scope.locChkBox.loc.push("US");
      
          $scope.init = function() {
              $scope.EmpResult = JSON.parse('[{"EmpName":"jondoe","Address":"dummyAddr","Location":"US"},{"EmpName":"jondoe2","Address":"dummyAddr2","Location":"IN"},{"EmpName":"jondoe3","Address":"dummyAddr3","Location":"CA"},{"EmpName":"jondoe4","Address":"dummyAddr4","Location":"US"},{"EmpName":"jondoe5","Address":"dummyAddr5","Location":"IN"},{"EmpName":"jondoe6","Address":"dummyAddr6","Location":"CA"},{"EmpName":"jondoe7","Address":"dummyAddr7","Location":"US"},{"EmpName":"jondoe8","Address":"dummyAddr8","Location":"IN"},{"EmpName":"jondoe9","Address":"dummyAddr9","Location":"CA"},{"EmpName":"jondoe11","Address":"dummyAddr11","Location":"US"},{"EmpName":"jondoe22","Address":"dummyAddr22","Location":"IN"}]');
      
              var flags = [],
                  output = [],
                  l = $scope.EmpResult.length,
                  i;
              for (i = 0; i < l; i++) {
                  if (flags[$scope.EmpResult[i].Location]) continue;
                  flags[$scope.EmpResult[i].Location] = true;
                  output.push($scope.EmpResult[i].Location);
              }
      
              $scope.locations = output;
      
          };
      
          $scope.locFilter = function(item) {
            for (var i = 0; i < $scope.locChkBox.loc.length; i++) {
              if (item.Location === $scope.locChkBox.loc[i])
                  return true;
            }
            return false;
          };
      }]);
    </script>
  
  </body>

</html>


编辑 2

如果没有选中任何复选框,此代码将显示所有值。

<!DOCTYPE html>
<html>

  <head>
      <meta charset="utf-8" />
      <title>AngularJS Plunker</title>
      <script>
          document.write('<base href="' + document.location + '" />');
      </script>
      <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
  </head>

  <body ng-app="myApp" ng-controller="myController">
      <div ng-init="init()">Location</div>
      <table>
          <tbody>
              <tr ng-repeat="empL in locations">
                  <td>
                      <span>
                              <input type="checkbox" name="locations + $index" data-ng-model="locChkBox.loc[$index]" ng-true-value="'{{empL}}'" ng-false-value="" ng-change="repopulate()"/>
                              {{empL}}
                      </span>
                  </td>
              </tr>
          </tbody>
      </table>
      <table align="left" style="width: 100%" class="table">
          <thead>
              <tr>
                  <th align="left" style="width: 30%">Employee</th>
                  <th align="left" style="width: 20%">Address</th>
                  <th align="left" style="width: 15%">Location</th>
              </tr>
          </thead>
          <tbody>
              <tr ng-repeat="empN in EmpResult | filter : locFilter ">
  
                  <td align="left" style="width: 30%">{{empN.EmpName}}</td>
                  <td align="left" style="width: 10%">{{empN.Address}}</td>
                  <td align="left" style="width: 15%">{{empN.Location}}</td>
  
              </tr>
          </tbody>
      </table>
    <script>
      var myApp = angular.module('myApp', []);
      
      myApp.controller("myController", ['$scope', '$http', function($scope, $http) {
      
          $scope.locations = [];
          $scope.search = {};
          $scope.locChkBox = {};
          $scope.locChkBox.loc = [];
      
          $scope.init = function() {
              $scope.EmpResult = JSON.parse('[{"EmpName":"jondoe","Address":"dummyAddr","Location":"US"},{"EmpName":"jondoe2","Address":"dummyAddr2","Location":"IN"},{"EmpName":"jondoe3","Address":"dummyAddr3","Location":"CA"},{"EmpName":"jondoe4","Address":"dummyAddr4","Location":"US"},{"EmpName":"jondoe5","Address":"dummyAddr5","Location":"IN"},{"EmpName":"jondoe6","Address":"dummyAddr6","Location":"CA"},{"EmpName":"jondoe7","Address":"dummyAddr7","Location":"US"},{"EmpName":"jondoe8","Address":"dummyAddr8","Location":"IN"},{"EmpName":"jondoe9","Address":"dummyAddr9","Location":"CA"},{"EmpName":"jondoe11","Address":"dummyAddr11","Location":"US"},{"EmpName":"jondoe22","Address":"dummyAddr22","Location":"IN"}]');
      
              var flags = [],
                  output = [],
                  l = $scope.EmpResult.length,
                  i;
              for (i = 0; i < l; i++) {
                  if (flags[$scope.EmpResult[i].Location]) continue;
                  flags[$scope.EmpResult[i].Location] = true;
                  output.push($scope.EmpResult[i].Location);
              }
      
              $scope.locations = output;
      
          };
      
          $scope.locFilter = function(item) {
            if($scope.locChkBox.loc.isNull()) return true;
            for (var i = 0; i < $scope.locChkBox.loc.length; i++) {
              if (item.Location === $scope.locChkBox.loc[i])
                  return true;
            }
            return false;
          };
      }]);
      
      Array.prototype.isNull = function (){
          return this.join().replace(/,/g,'').length === 0;
      };
    </script>
  
  </body>

</html>

关于javascript - Checkboxlist ng-repeat过滤器动态angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691323/

相关文章:

PHP删除数组的第一个索引并重新索引

javascript - 同位素附加似乎不起作用

javascript - 将angular模板编译成html的正确方法,将结果转换成字符串

javascript - 获取被 angularjs 指令替换之前的原始模板

html - 使用来自 REST 服务的数据时,分页不适用于智能表

Javascript/Angular : clear image select form input

javascript - 带有几行代码的 CodeMirror 在触发重绘之前不会显示

javascript - jquery 试图通过名称获取按钮位置

使用wireshark显示过滤器过滤流量

angular - 银格复位过滤器