How to sort dates?
ive spent hours googling this issue with no luck so il post here...
How do you sort a column by date? (in my case, dd.MM.yyy) Is there an out of the box solution or can you define a custom sort function?
Hi,
Check my plunk on how I did it with ng-grid.
http://plnkr.co/edit/C72P5V?p=info
hth
John
From: equilerex [mailto:[email protected]] Sent: Tuesday, June 03, 2014 6:59 AM To: esvit/ng-table Subject: [ng-table] How to sort dates? (#336)
ive spent hours googling this issue with no luck so il post here...
How do you sort a column by date? (in my case, dd.MM.yyy) Is there an out of the box solution or can you define a custom sort function?
— Reply to this email directly or view it on GitHub https://github.com/esvit/ng-table/issues/336 . https://github.com/notifications/beacon/1877376__eyJzY29wZSI6Ik5ld3NpZXM6QmVhY29uIiwiZXhwaXJlcyI6MTcxNzQxMjMyOCwiZGF0YSI6eyJpZCI6MzM3MDc0OTV9fQ==--b349d7b1c9be65d179928eb6a925feff6ef89072.gif
No virus found in this message. Checked by AVG - www.avg.com Version: 2014.0.4570 / Virus Database: 3955/7584 - Release Date: 05/29/14
yes, it has a nice custom sorting function but how would i hook that up to ng-table? the documentation on it is pretty much non existent. ive tried all kinds of combinations but none of them have worked so i cant figure out how to use functions with sortable="'xx'" nor how to use date objects with it.
Ok, I will try this tomorrow.
From: equilerex [mailto:[email protected]] Sent: Thursday, June 05, 2014 5:49 AM To: esvit/ng-table Cc: John R. Tomaselli Subject: Re: [ng-table] How to sort dates? (#336)
yes, it has a nice custom sorting function but how would i hook that up to ng-table? the documentation on it is pretty much non existent. ive tried all kinds of combinations but none of them have worked so i cant figure out how to use functions with sortable="'xx'" nor how to use date objects with it.
— Reply to this email directly or view it on GitHub https://github.com/esvit/ng-table/issues/336#issuecomment-45200584 . https://github.com/notifications/beacon/1877376__eyJzY29wZSI6Ik5ld3NpZXM6QmVhY29uIiwiZXhwaXJlcyI6MTcxNzU4MDk0OSwiZGF0YSI6eyJpZCI6MzM3MDc0OTV9fQ==--2436cf97d18cd5d4f1fc17a902d0cf0cf625bb8e.gif
No virus found in this message. Checked by AVG - www.avg.com Version: 2014.0.4570 / Virus Database: 3955/7620 - Release Date: 06/04/14
I'm interested in that feature too, in order to sort on a translated object property.
Well, i create a secondary date field in the json served with format 'YYYY-MM-DD' and sort the table with it. The display date has format 'DD-MM-YYYY'.
hah, thats a neat hack :P thanks! guess il use that until a proper solution comes around :+1:
There are two different ways:
- Use native
Dateobject. Your data model must contain Date objects, not strings. You can make this, after you recived data from server. Then parse it using native date or moment.js, as mentioned @evoratec. Then use format filter for date - Write your custom ordering function.
@maxaon how would you go about writing your custom ordering function?
I wouldn't create custom filter for date, becouse parsing data in getData functoin is bad idea. Data must be parsed after it was recived from server. And sorting/filtering also must be made on server.
Demo of sorting http://plnkr.co/edit/vsXP2CFaKKjE0jVNhfvd?p=preview All data from the server must be normalized and standartized. Date must be in ISO format and sallary as float number. But if you cannot do this, data should be normalized by you ater it is recieved.
Implementing of custom filter is possible but is not recomended http://plnkr.co/edit/XwUHGPXuYFADJqmgd84e?p=preview
Date is not the only case. As I say earlier if you want to sort on translate field (e.g. angular-translate) you have to use the ugly method (custom filter). Translation is not something you can do server-side or normalized.
@ctjhoa-linagora Then translate your data after reciving it from server.
As you suggest, I tried @maxaon way with an immediate translation after receiving from server (In my plunker sampled as 'translations' property). Manual sorts are ok but switch current lang won't reload sorting (even with table.reload).
use case:
- Switch to english
- Sort 'asc' filter -> Moroni is first, which is ok because is translated in english 'A_EN'
- Switch lang
- Moroni is translated in deutch 'E_DE' so it should be the last of the table but it stay at first position
http://plnkr.co/edit/Kv8qqhbeyqYL435GuEkk?p=preview
That's because the sortable HTML attribute is not updated during switch lang (in the use case sorting is still using { "translations.en": "desc" } in step 4 ). I find no other way than use custom filter.
Yes, it wouldn't work becouse you haven't updated to correct sorting object this will work http://plnkr.co/edit/UdtLQG0CWfotgxt4gklc?p=preview But i suposed that you TRANSLTAE text and then use it http://plnkr.co/edit/rbOSsGinG3bDHbZCT3im?p=preview
Oh I see. Thanks for this examples, putting the second one in doc could be a good idea.
I am also facing the same issue. I want to sort a column by date and not alphabetically. Please let me know if anyone has a solution to this.
hi everyone, i have found a way to fix this issue, in the directive add an angularjs filter date and specify a format you want.
eg: {{user.date | date:'dd-MM-yyyy'}}
I use this in my code:
this.tableParams = new NgTableParams({
counts: filteredData.length,
count: config.records.defaultCount
}, {
counts: config.records.counts,
getData: (params) => {
function sort(data) {
if (params.sorting()) {
var order = params.orderBy();
if (order.length) {
var m = order[0].match(/([-+])(.*)/);
var sign = m[1];
var name = m[2];
if (isDate(name)) {
data.sort(function(a, b) {
if (!a[name] && !b[name]) {
return 0;
} else {
a = moment(a[name], config.dateFormat.display);
b = moment(b[name], config.dateFormat.display);
if (sign == '+') {
return a.diff(b);
} else {
return b.diff(a);
}
}
});
} else {
return $filter('orderBy')(data, order);
}
}
}
return data;
}
function paginate(data) {
params.total(data.length);
var page = params.page();
var count = params.count();
return data.slice((page - 1) * count, page * count);
}
var data = sort(filteredData); // data came from ajax request
this.count = data.length;
return paginate(data);
}
});