使用AngularJS过滤对象属性为空时的ng-repeat

时间:2014-11-07 16:53:31

标签: javascript angularjs filter

我有一组具有多个属性的对象。所有这些对象都具有object.comment属性,但在某些对象中填充了字符串('comment' : 'comment text'),其他对象为空('comment' : '')。

我用ng-repeat列出结果,如下所示:

<div class="row msf-row" 
     ng-repeat="record in filteredRecords = (recordlist | filter:dateFilter | filter: search )" 
>

我要完成的是添加一个复选框过滤器,以便仅在选中复选框时显示object.comment属性填充的结果,以及未选中时的所有结果。

以下是我的过滤器到目前为止的显示方式:

<form role="form">
            <div class="form-group col-md-3">
              <input type='daterange' 
              placeholder="Date range" 
              class="form-control"
              format="DD/MM/YYYY" 
              ng-model="dates"
              ranges="ranges" />
            </div>

            <div class="form-group col-md-1">
              <input class="form-control" placeholder="Time" ng-model="search.time">
            </div>

            <div class="form-group col-md-1">
              <input class="form-control" placeholder="Car" ng-model="search.car">
            </div>

            <div class="form-group col-md-2">
              <input class="form-control" placeholder="Driver" ng-model="search.driver">
            </div>

            <div class="form-group col-md-2">
              <input class="form-control" placeholder="From" ng-model="search.from">
            </div>

            <div class="form-group col-md-2">
              <input class="form-control" placeholder="Destination" ng-model="search.destination">
            </div>

            <div class="form-group col-md-1">
              <input class="form-control" placeholder="Pax" ng-model="search.pax">
            </div>

            <div class="col-md-1">
              <div class="checkbox">
                <label>
                  <input type="checkbox" 
                         ng-model="search.cancelled" 
                         ng-change="search.cancelled = search.cancelled ? true : undefined"
                  > Cancelled 
                </label>
              </div>
            </div> 

            <div class="col-md-2">
              <div class="checkbox">
                <label>
                  <input type="checkbox" 
                         ng-model="search.comment" 
                         ng-change="search.comment = search.comment ? true : undefined"
                  > Commented records
                </label>
              </div>
            </div>

</form>

正如您所看到的,我已经有一个过滤器可以处理object.cancelled是真还是假,但是当object.comment为空或有空时,我无法做同样的事情一个字符串。

任何指针?

2 个答案:

答案 0 :(得分:2)

您可以创建自定义过滤器,请参阅下面的演示

app.filter('emptyString', [
  function() {
    return function(input, param) {
      if (!param) return input

      var ret = [];
      if (!angular.isDefined(param)) param = true;


      if (param) {
        angular.forEach(input, function(v) {
          if (angular.isDefined(v.comment) && v.comment) {
            v.comment = v.comment.replace(/^\s*/g, '');
            ret.push(v);
          }

        });
      }



      return ret;
    };
  }
])

&#13;
&#13;
var app = angular.module('app', []);

app.controller('homeCtrl', function($scope) {

  $scope.recordlist = [{
      time: "10/11/2014",
      comment: "super"
    }, {
      time: "10/11/20004",
      comment: ""
    }, {
      time: "10/11/2005",
      comment: ""
    }, {
      time: "11/1/2014",
      comment: "that was ok"
    }


  ];


});

app.filter('emptyString', [
  function() {
    return function(input, param) {
      if (!param) return input

      var ret = [];
      if (!angular.isDefined(param)) param = true;


      if (param) {
        angular.forEach(input, function(v) {
          if (angular.isDefined(v.comment) && v.comment) {
            v.comment = v.comment.replace(/^\s*/g, '');
            ret.push(v);
          }

        });
      }



      return ret;
    };
  }
])
&#13;
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>



<div ng-app="app">
  <div ng-controller="homeCtrl">
    <form role="form">
      <div class="form-group col-md-3">
        <input type='daterange' placeholder="Date range" class="form-control" format="DD/MM/YYYY" ng-model="dates" ranges="ranges" />
      </div>

      <div class="form-group col-md-1">
        <input class="form-control" placeholder="Time" ng-model="search.time">
      </div>

      <div class="form-group col-md-1">
        <input class="form-control" placeholder="Car" ng-model="search.car">
      </div>

      <div class="form-group col-md-2">
        <input class="form-control" placeholder="Driver" ng-model="search.driver">
      </div>

      <div class="form-group col-md-2">
        <input class="form-control" placeholder="From" ng-model="search.from">
      </div>

      <div class="form-group col-md-2">
        <input class="form-control" placeholder="Destination" ng-model="search.destination">
      </div>

      <div class="form-group col-md-1">
        <input class="form-control" placeholder="Pax" ng-model="search.pax">
      </div>

      <div class="col-md-1">
        <div class="checkbox">
          <label>
            <input type="checkbox" ng-model="search.cancelled" ng-change="search.cancelled = search.cancelled ? true : undefined">Cancelled
          </label>
        </div>
      </div>

      <div class="col-md-2">
        <div class="checkbox">
          <label>
            <input type="checkbox" ng-model="search.comment">Commented records
          </label>
        </div>
      </div>

    </form>
    <div class="container">
      <div class="row " ng-repeat="record in filteredRecords = (recordlist |  emptyString : search.comment   )">
        <div class="col-xs-12" <span class="label">{{record.time}} <span> <strong>{{record.comment}}</strong></p> </div>
    </div>
    </div>
  </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

一个简单的解决方案是使用ng-hide指令:

<div class="row"
    ng-repeat="record in filteredRecords"
    ng-hide="search.comment && (!record.comment || record.comment === '')">
</div>

它的含义是:如果选中search.comment并且record.comment未定义或为空,则隐藏此元素。

查看这个plunker:http://plnkr.co/edit/m8rA0rVxqgWS0NWukJdf?p=preview

这是预期的行为吗?