gpt4 book ai didi

javascript - 如何按月和年使用 Angular JS 正确过滤日期

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

在我的数据库中,日期排列如下 (dd/mm/yy)

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&nbsp;<a ng-click="sort_by('time_stamp');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Reference number&nbsp;<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

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:

$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;

如何获得我想要的排序

最佳答案

在你的例子中,the date items are sorting like strings, not like dates .

您必须将它们设置为 Date 对象才能正常排序。

编辑

如果我正确理解了您的代码,您将在此处获取 Controller 中的数据:

 $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 中对日期进行了功能排序基于这个假设,它就像一个魅力!

在 Controller 中,我将属性 dateDate 对象相关联,例如 date:new Date('9/1/2013') 和在AngularJS sorting documentation中下了订单.

关于javascript - 如何按月和年使用 Angular JS 正确过滤日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734780/

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