How to correctly determine filter date with angular js by month and year

In my dates the databases are located as shown below (dd / mm / yy)

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

      

I am using angularjs template and am trying to sort the date field like (sorting using both day of month and day) below:

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

      

But it sorts ascending as

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

      

descending like

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

      

How to get the sort you want

+2


source to share


1 answer


In your case , date elements are sorted as strings, not dates .

You have to set them as Date objects in order to sort them normally.

EDIT

If I understand your code correctly, you get data in the controller here:

 $http.get('ajax/getCustomers.php').success(function(data){
        $scope.list = data;

      

Following the diagnosis of the problem, try something like this on the following line:



$scope.list.time_stamp = new Date(data.time_stamp); // convert string date to Date object

      

I haven't tested the code, but I hope it works because we know what the problem is.

WORK PLANKER

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

In the controller, I map a property date

to an object date

, for example date:new Date('9/1/2013')

, and did an order like in AngularJS Sorting Documentation .

+3


source







All Articles