javascript - How to sort and filter ngTable date (milliseconds) data? -
i pretty new angularjs need sorting date in milliseconds in descending order filter table columns. created plunker here when key in filter param not see filtered data , data lost table.
please me in sorting date column , doing case insensitive filter search, below providing code.
<table ng-table="tableparams" show-filter="true" class="table" > <tr ng-repeat="item in $data" height="10px"> <td data-title="'date'" filter="{ 'item[0]': 'date' }" sortable="'item[0]'">{{translate(item[0])}}</td> <td data-title="'count'" filter="{ 'item[1]': 'text' }" sortable="'item[1]'">{{item[1]}}</td> </tr> </table> $scope.translate = function(value) { if (value == null || value == undefined) return value; var monthnames = [ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" ]; var mydate = new date( value ); return mydate.getdate() + " " + monthnames[mydate.getmonth()] + " "+mydate.getfullyear(); } $scope.tableparams = new ngtableparams({ page: 1, // show first page count: 10 // count per page }, { total: $scope.tasksrundatafortable.length, // length of data getdata: function($defer, params) { // use build-in angular filter var sorteddata = params.sorting() ? $filter('orderby')($scope.tasksrundatafortable, params.orderby()) : $scope.tasksrundatafortable; var ordereddata = params.filter() ? $filter('filter')(sorteddata, params.filter()) : sorteddata; params.total(ordereddata.length); // set total recalc pagination $defer.resolve(ordereddata.slice((params.page() - 1) * params.count(), params.page() * params.count())); } });
update
i able filter work on count, no luck date , sorting on both columns. updated plunker
<td data-title="'count'" filter="{ '1': 'text' }" sortable="'1'">{{item[1]}}</td>
date sorting
this not issue of ngtable how underlying angular filter 'orderby' works.
just use valueof()[0]
, valueof()[1]
respectively 0 & 1 indexes inner array. here html , there no need change getdata callback.
<table ng-table="tableparams" show-filter="true" class="table" > <tr ng-repeat="item in $data" height="10px"> <td data-title="'date'" filter="{ '0': 'text' }" sortable="valueof()[0]">{{ item[0] | date: 'd mmm yyyy hh:mm' }}</td> <td data-title="'count'" filter="{ '1': 'text' }" sortable="valueof()[1]">{{ item[1] }}</td> </tr> </table>
please note don't need translate presenting dates milliseconds supported other angular filter 'date'.
see somehow related post https://stackoverflow.com/a/15858739/61577
date filtering (from string)
in order achieve filtering date need use custom "comperator" function third argument when filtering on controller. trick
var datecomperator = function(obj, text) { var valueastext = obj + ''; if (valueastext.length == 13) { // must milliseconds. valueastext = $filter('date')(obj, 'd mmm yyyy hh:mm'); } return !text || (obj && valueastext.tolowercase().indexof(text.tolowercase()) > -1); };
then on controller getdata callback:
getdata: function($defer, params) { // use build-in angular filter var sorteddata = params.sorting() ? $filter('orderby')($scope.tasksrundatafortable, params.orderby()) : $scope.tasksrundatafortable; var filterinfo = params.filter(); var comparer = (filterinfo && filterinfo['0']) ? datecomparer : undefined; $log.log(angular.tojson(filterinfo)) var ordereddata = filterinfo ? $filter('filter')(sorteddata, filterinfo, comparer) : sorteddata; params.total(ordereddata.length); // set total recalc pagination $defer.resolve(ordereddata.slice((params.page() - 1) * params.count(), params.page() * params.count())); }
update: updated answer tackle filtering issue. update 2: updated answer tackle when searching both date , count.
here complete plunker http://plnkr.co/edit/d2n7mdafugxpkekogosl?p=preview
hope helps.
Comments
Post a Comment