过滤器在状态=" ACTIVE"

时间:2016-11-16 07:49:07

标签: javascript angularjs json filter

以下代码应根据所选状态显示用户列表, JSP代码

<table>
    <thead>
      <tr>
        <th>Aggregate</th>
        <th>User id</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Phone Number</th>
        <th>Email</th>
        <th class="text-center">Status</th>
      </tr>
    </thead>

    <tbody >

        <tr>
            <td><input type="text" class="form-control" ng-model="search.agId" /></td>
            <td><input type="text" class="form-control" ng-model="search.id" /></td>
            <td><input type="text" class="form-control" ng-model="search.firstName" /></td>
            <td><input type="text" class="form-control" ng-model="search.lastName" /></td>
            <td><input type="text" class="form-control" ng-model="search.mobileNo" /></td>
            <td><input type="text" class="form-control" ng-model="search.emailId" /></td>
            <td>
                <select class="form-control" ng-model="search.status">
                    <option value=""> Select </option>
                    <option value="ACTIVE"> Active </option>
                    <option value="INACTIVE"> In Active </option>
                    <option value="B"> Blocked </option>
                </select>
            </td>
        </tr>


        <tr ng-repeat="userone in filtereddata = (users.data | filter:search)">
            <td>{{ userone.agId }}</td>
            <td>{{ userone.id }}</td>
            <td>{{ userone.firstName }}</td>
            <td>{{ userone.lastName }}</td>
            <td>{{ userone.mobileNo }}</td>
            <td>{{ userone.emailId }}</td>
            <td class="text-center" ng-switch on="userone.status">
                <span class="inac label label-success" ng-switch-when="ACTIVE">Active</span> 
                <span class="inac label label-danger" ng-switch-when="INACTIVE">In Active</span> 
                <span class="inac label label-danger" ng-switch-when="B">Blocked</span>
            </td>
        </tr>

    </tbody>
</table>

控制器JS代码

$scope.$watch('search', function (newVal, oldVal) {
            $scope.filtered = filterFilter($scope.users.data, newVal);

            console.log($scope.filtered);

            $scope.bigTotalItems = (typeof $scope.filtered == 'undefined' ) ? 0 : $scope.filtered.length;
            $scope.noOfPages = Math.ceil($scope.bigTotalItems/$scope.entryLimit);
            $scope.currentPage = 1;
        }, true);

它对INACTIVE工作正常并阻止(即B)用户,但选择时 ACTIVE显示所有ACTIVE和INACTIVE状态的用户

我在Controller js代码中给出了一个简单的console.log($scope.filtered) 当选择Active时,它实际上返回过滤数据中的ACTIVE和INACTIVE成员。它适用于其他选择。

我该如何解决这个问题?

如果需要任何其他数据,请告诉我

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
angular.module('test', []).controller('Test', Test);

function Test($scope, $filter) {
  $scope.data = [{
    name: 'apple',
    status: 'inactive'
  }, {
    name: 'banana',
    status: 'active'
  }, {
    name: 'grape',
    status: 'deleted'
  }, {
    name: 'orange',
    status: 'inactive'
  }, {
    name: 'pear',
    status: 'deleted'
  }, {
    name: 'watermelon',
    status: 'active'
  }]
  
  $scope.filter = function(actual, expected) {
    if (expected == '') return true;
    
    return angular.equals(actual, expected);
  }
  
  $scope.$watch('search', function(newVal) {
    $scope.filtered = $filter('filter')($scope.data, newVal, $scope.filter);
  });
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app='test' ng-controller='Test'>
  Search:
  <select ng-model="search">
    <option value="">Select</option>
    <option value="active">Active</option>
    <option value="inactive">Inactive</option>
    <option value="deleted">Deleted</option>
  </select>
  <hr>
  <div ng-repeat="item in data | filter:search:filter">
    {{item.name}} - {{item.status}}
  </div>
  <hr>
  <div ng-repeat="item in filtered">
    {{item.name}} - {{item.status}}
  </div>
</div>
&#13;
&#13;
&#13;

通过阅读API reference,您可以通过在第3个参数中传递true来强制执行严格的比较。

<tr ng-repeat="userone in filtereddata = (users.data | filter:search:true)">

如果你的控制器需要正确的结果,你也需要传递第3个参数。

$scope.filtered = filterFilter($scope.users.data, newVal, $scope.filter);

编辑:要在选择空过滤器时修复空结果问题,我们需要使用自定义过滤功能。更新了代码。