Javascript 使用过滤器的结果显示基于值的视图
我试图根据过滤器的值显示和隐藏页面的某些部分。如果数据长度为1,则应显示某个字段,如果该字段等于零,则应显示页面的不同部分Javascript 使用过滤器的结果显示基于值的视图,javascript,angularjs,Javascript,Angularjs,我试图根据过滤器的值显示和隐藏页面的某些部分。如果数据长度为1,则应显示某个字段,如果该字段等于零,则应显示页面的不同部分 <div ng-show="results.length=0"> <h4 class="redtext" align="center">Sorry, Search found no records </h4> </div> <div ng-show="results.length!=0" ng-repeat="ite
<div ng-show="results.length=0">
<h4 class="redtext" align="center">Sorry, Search found no records </h4>
</div>
<div ng-show="results.length!=0" ng-repeat="item in record | dateRange : from : to">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="70%"><div align="left">
<p><strong>Bill Period</strong> </p>
</div></td>
<td width="82%">{{item.Date |date: 'MMMM yyyy'}}</td>
</tr>
<tr>
<td><p><strong>Reading Date</strong></p></td>
<td>{{item.Date |date: 'dd MMMM yyyy'}}</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
抱歉,搜索未找到任何记录
票据期限
{{item.Date}日期:'MMMM-yyyy'}
阅读日期
{{item.Date}日期:'dd-MMMM-yyyy'}
JS
$scope.state\u request=function(){
$http.post(“http://localhost/server/statement.php", {
“id”:$scope.account\u编号
}).成功(功能(数据){
log(JSON.stringify(data));
$scope.record=数据;
})
$scope.from=$filter('date')($scope.sdate,“yyyy-MM-dd”+'T00:00:00')+'Z';
$scope.to=$filter('date')($scope.edate,“yyyy-MM-dd”+'T00:00:00')+'Z';
}
})
.filter('dateRange',函数(){
返回函数(记录、从、到){
//如果输入未定义或不是数组,则返回空数组
如果(!records | |!angular.isArray(records)){
返回[];
}
var结果=记录。过滤器(函数(记录){
//在返回之前…在此运行控制台日志测试
return record.Date>=from&&record.Date如果我清楚地理解了您的意思,作为结果
在过滤器之后
(在您的情况下:ng repeat=“记录中的项目|日期范围:从:到作为结果”
)将有助于:
angular.module('app',[]).controller('ctrl',['$scope',function($scope){
$scope.record=['1','2','3','4'];
}]);
抱歉,搜索未找到任何记录
- {item}
$scope.state_request = function() {
$http.post("http://localhost/server/statement.php", {
'id': $scope.account_number
}).success(function(data) {
console.log(JSON.stringify(data));
$scope.record = data;
})
$scope.from = $filter('date')($scope.sdate, "yyyy-MM-dd" + 'T00:00:00') + 'Z';
$scope.to = $filter('date')($scope.edate, "yyyy-MM-dd" + 'T00:00:00') + 'Z';
}
})
.filter('dateRange', function() {
return function(records, from, to) {
// return empty array if input not defined or not array
if (!records || !angular.isArray(records)) {
return [];
}
var results = records.filter(function(record) {
// run console log tests here...before the return
return record.Date >= from && record.Date <= to;
});
if (results.length = 0) {
console.log('its empty')
} else {
console.log('results found')
}
console.log('Number of results:', results.length);
return results;
}
})