ng-table icon indicating copy to clipboard operation
ng-table copied to clipboard

How to sort dates?

Open equilerex opened this issue 12 years ago • 17 comments

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?

equilerex avatar Jun 03 '14 10:06 equilerex

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

johntom avatar Jun 04 '14 15:06 johntom

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.

equilerex avatar Jun 05 '14 09:06 equilerex

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

johntom avatar Jun 05 '14 17:06 johntom

I'm interested in that feature too, in order to sort on a translated object property.

ctjhoa-linagora avatar Jun 06 '14 10:06 ctjhoa-linagora

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'.

evoratec avatar Jun 27 '14 15:06 evoratec

hah, thats a neat hack :P thanks! guess il use that until a proper solution comes around :+1:

equilerex avatar Jul 02 '14 10:07 equilerex

There are two different ways:

  1. Use native Date object. 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
  2. Write your custom ordering function.

maxaon avatar Jul 19 '14 14:07 maxaon

@maxaon how would you go about writing your custom ordering function?

blakedietz avatar Jul 23 '14 00:07 blakedietz

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

maxaon avatar Jul 23 '14 12:07 maxaon

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 avatar Jul 23 '14 15:07 ctjhoa-linagora

@ctjhoa-linagora Then translate your data after reciving it from server.

maxaon avatar Jul 23 '14 15:07 maxaon

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:

  1. Switch to english
  2. Sort 'asc' filter -> Moroni is first, which is ok because is translated in english 'A_EN'
  3. Switch lang
  4. 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.

ctjhoa-linagora avatar Jul 23 '14 16:07 ctjhoa-linagora

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

maxaon avatar Jul 23 '14 17:07 maxaon

Oh I see. Thanks for this examples, putting the second one in doc could be a good idea.

ctjhoa-linagora avatar Jul 24 '14 07:07 ctjhoa-linagora

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.

savinay avatar Sep 13 '14 07:09 savinay

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'}}

Pradeep-spyan avatar Nov 05 '14 05:11 Pradeep-spyan

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);
                   }
              });

jcubic avatar Apr 12 '17 16:04 jcubic