gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:32:20 25 4
gpt4 key购买 nike

我有一个具有多个属性的对象数组。所有这些对象都有一个 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 为空或有一个字符串。

有什么建议吗?

最佳答案

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

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;
};
}
])

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;
};
}
])
<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>

关于javascript - 使用 AngularJS 在对象属性为空时过滤 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com