In my data base dates are arranged like below (dd/mm/yy)
在我的数据库日期安排如下(dd/mm/yy)
Date
-----------
10/12/2014
22/10/2014
14/12/2015
I am using angularjs template and trying to sort the date field as(sorting using both month and year number) below:
我正在使用angularjs模板并尝试将日期字段(使用月号和年号进行排序)排序如下:
Date
-----------
14/12/2015
10/12/2014
22/10/2014
But it is sorting ascending as
但它是升序的
Date
-----------
10/12/2014
14/12/2015
22/10/2014
descending as
降为
Date
----------
22/10/2014
14/12/2015
10/12/2014
Here is my code
这是我的代码
<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>
controller.js
controller.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;
};
});
getCustomer.php:
getCustomer.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;
How to obtain my desired sorting
如何获得我想要的分类?
1 个解决方案
#1
3
In your case, the date items are sorting like strings, not like dates.
在您的例子中,日期项像字符串一样排序,而不是像日期一样排序。
You must set them as Date objects to sort it normally.
您必须将它们设置为日期对象,以便对其进行正常排序。
EDIT
编辑
If I correctly understand your code, you are getting the data in the controller here:
如果我正确地理解了您的代码,那么您正在获取控制器中的数据:
$http.get('ajax/getCustomers.php').success(function(data){
$scope.list = data;
Following the problem diagnose, try to make something like this in the next line:
在问题诊断之后,试着在下一行中做出这样的事情:
$scope.list.time_stamp = new Date(data.time_stamp); // convert string date to Date object
I didn't test the code, but I hope it works, because we know what's the problem.
我没有测试代码,但是我希望它能够工作,因为我们知道问题出在哪里。
WORKING PLUNKER
工作恰好
I made functional sorting date in plunker based on this hypothesis and it works like a charm!
基于这个假设,我在plunker中设计了功能排序日期,它的工作效果简直棒极了!
In controller I asociate the property date
with the Date
object like date:new Date('9/1/2013')
and made a order like in AngularJS sorting documentation.
在控制器中,我使用日期对象date:new date(‘9/1/2013’)确定属性日期,并在AngularJS排序文档中做了类似的排序。
#1
3
In your case, the date items are sorting like strings, not like dates.
在您的例子中,日期项像字符串一样排序,而不是像日期一样排序。
You must set them as Date objects to sort it normally.
您必须将它们设置为日期对象,以便对其进行正常排序。
EDIT
编辑
If I correctly understand your code, you are getting the data in the controller here:
如果我正确地理解了您的代码,那么您正在获取控制器中的数据:
$http.get('ajax/getCustomers.php').success(function(data){
$scope.list = data;
Following the problem diagnose, try to make something like this in the next line:
在问题诊断之后,试着在下一行中做出这样的事情:
$scope.list.time_stamp = new Date(data.time_stamp); // convert string date to Date object
I didn't test the code, but I hope it works, because we know what's the problem.
我没有测试代码,但是我希望它能够工作,因为我们知道问题出在哪里。
WORKING PLUNKER
工作恰好
I made functional sorting date in plunker based on this hypothesis and it works like a charm!
基于这个假设,我在plunker中设计了功能排序日期,它的工作效果简直棒极了!
In controller I asociate the property date
with the Date
object like date:new Date('9/1/2013')
and made a order like in AngularJS sorting documentation.
在控制器中,我使用日期对象date:new date(‘9/1/2013’)确定属性日期,并在AngularJS排序文档中做了类似的排序。