【问题标题】:How filter date properly with angular js by month and year如何按月和年使用 Angular js 正确过滤日期
【发布时间】:2015-01-01 21:17:59
【问题描述】:

在我的数据库中,日期排列如下(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;

如何获得我想要的排序

【问题讨论】:

  • 您能否粘贴您的 JSON 响应。

标签: javascript angularjs sorting date


【解决方案1】:

在你的情况下,the date items are sorting like strings, not like dates

您必须将它们设置为 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 中设置了功能排序日期,它就像一个魅力!

在控制器中,我将属性dateDate 对象(如date:new Date('9/1/2013'))相关联,并在AngularJS sorting documentation 中下达订单。

【讨论】:

  • 我编辑了我的帖子,希望我的想法能切实解决这个问题。
  • 你的成功回调是 JSON 格式的吗?看起来如何?
  • 我已经更新了我的帖子。为了您的信息,我正在使用这个grid
  • 请给我包含 time_stamp 属性的 JSON 结构。
  • @arman1991 在您的回答中,它不应该是新日期而不是新数据吗?
猜你喜欢
  • 2023-03-22
  • 2015-01-24
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
相关资源
最近更新 更多