过滤器如何正确日期与月份和年份角的js [英] How filter date properly with angular js by month and year

查看:179
本文介绍了过滤器如何正确日期与月份和年份角的js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据的基础日期安排如下图所示(DD / MM / YY)

In my data base dates are arranged like below (dd/mm/yy)

Date
-----------
10/12/2014
22/10/2014
14/12/2015

我使用angularjs模板,并试图理清日期字段如下(同时使用年份和月份数排序):

I am using angularjs template and trying to sort the date field as(sorting using both month and year number) below:

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

下面是我的code

<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

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

推荐答案

在你的情况,的日期项目进行排序,例如字符串,不像日期的。

您必须将它们作为Date对象正常排序。

You must set them as Date objects to sort it normally.

修改

如果我理解正确的code,你所得到的数据在这里控制器:

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

我没有测试code,但我希望它的作品,因为我们知道有什么问题。

I didn't test the code, but I hope it works, because we know what's the problem.

工作PLUNKER

WORKING PLUNKER

plunker 做功能排序日期基于这种假设,它就像一个魅力!

I made functional sorting date in plunker based on this hypothesis and it works like a charm!

在控制器I asociate属性日期日期对象像时间:新日期(2013年9月1日')并取得了令像的 AngularJS整理文档

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.

这篇关于过滤器如何正确日期与月份和年份角的js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆