如何使用角度js按月和年正确过滤日期
How filter date properly with angular js by month and year
在我的数据库中,日期排列如下(日/月/年)
Date
-----------
10/12/2014
22/10/2014
14/12/2015
我正在使用angularjs模板并尝试将日期字段排序为(使用月份和年份编号进行排序):
Date
-----------
14/12/2015
10/12/2014
22/10/2014
但它的排序升序为
Date
-----------
10/12/2014
14/12/2015
22/10/2014
降序为
Date
----------
22/10/2014
14/12/2015
10/12/2014
这是我的代码
<div class="row">
<div class="col-md-12" ng-show="filteredItems > 0">
<table class="table table-striped table-bordered">
<thead>
<th>Date <a ng-click="sort_by('time_stamp');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Reference number <a ng-click="sort_by('tran_id');"><i class="glyphicon glyphicon-sort"></i></a></th>
</thead>
<tbody>
<tr ng-repeat="data in filtered = (list | filter:search | orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
<td>{{data.time_stamp}}</td>
<td>{{data.tran_id}}</td>
</tr>
</tbody>
</table>
</div>
控制器.js
var app = angular.module('myApp', ['ui.bootstrap']);
app.filter('startFrom', function() {
return function(input, start) {
if(input) {
start = +start; //parse to int
return input.slice(start);
}
return [];
}
});
app.controller('customersCrtl', function ($scope, $http, $timeout) {
$http.get('ajax/getCustomers.php').success(function(data){
$scope.list = data;
$scope.currentPage = 1; //current page
$scope.entryLimit = 5; //max no of items to display in a page
$scope.filteredItems = $scope.list.length; //Initially for no filter
$scope.totalItems = $scope.list.length;
});
$scope.setPage = function(pageNo) {
$scope.currentPage = pageNo;
};
$scope.filter = function() {
$timeout(function() {
$scope.filteredItems = $scope.filtered.length;
}, 10);
};
$scope.sort_by = function(predicate) {
$scope.predicate = predicate;
$scope.reverse = !$scope.reverse;
};
});
获取客户.php:
$query="select tran_id,tran_type,sender,receiver,amount,fee,DATE_FORMAT(time_stamp, '%d/%m/%Y') as time_stamp,status from transaction where sender ='demo@gmail.com' or receiver ='demo@gmail.com' order by time_stamp DESC";
//$query="select * from transaction";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row;
}
}
# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;
如何获得我想要的排序
在您的情况下,日期项的排序类似于字符串,而不是日期。
必须将它们设置为 Date 对象才能正常排序。
编辑
如果我正确理解了您的代码,您将在此处获取控制器中的数据:
$http.get('ajax/getCustomers.php').success(function(data){
$scope.list = data;
在问题诊断之后,尝试在下一行中制作类似这样的内容:
$scope.list.time_stamp = new Date(data.time_stamp); // convert string date to Date object
我没有测试代码,但我希望它能工作,因为我们知道问题出在哪里。
工作堆积器
我根据这个假设在 plunker 中制作了函数排序日期,它就像一个魅力!
在控制器中,我将属性date
与Date
对象相关联,如date:new Date('9/1/2013')
,并像在 AngularJS 排序文档中一样下订单。
相关文章:
- ui网格日期单元格过滤器,过滤日期格式导致显示错误的日期
- 如何从Wijmo过滤flexgrid中的日期
- 过滤日期范围 angularjs 时出错
- AngularJS过滤格式化的日期
- 在JavaScript中,按日期和之后的名称过滤数组
- 如何在 Angular js 中按日期过滤数据
- JavaScript 智能日期过滤
- 按输入日期过滤 ng 重复
- 拉拉维尔角度如何过滤碳串上的日期
- Dojo 网格:将过滤条件发送到服务器,具有有效日期
- 如何使用角度js按月和年正确过滤日期
- 酒窝.js使用日期范围过滤 X 轴
- 角度JS过滤日期
- ng 表过滤毫秒日期数据
- angular - 过滤开始日期和结束日期在所选月份和年份范围内的所有对象
- 如何使用php和mysql过滤高图表中的日期
- 如何使用AngularJS过滤日期范围
- 如何使用JS/Jquery过滤日期和JSon数据
- 使用angularjs过滤日期时出现ReferenceError
- Angularjs orderBy filter 无法正确过滤日期