List.js - by task sorting by date

In my application, I use List.js to sort. All string values ​​work fine. But I have a “modified date” in the column.

When I click "change by date" - sort, this is just a consideration of the date values, what is the text, for example: 1/4/11 .. and sort accordingly. because of this approach, I get the wrong sort orders.

how can I do instead, it should sort by the real value of the number, what are the dates?

Here is my code:

new List('mfi-col2', { valueNames: ['companyLegalName', 'phazeName', 'contactName', 'number', 'enrollId', 'accountType'] }); 

Instead of "number" you can send $(".number").data-number ? So let him use the timestamp that I get from the server?

Or can anyone suggest an alternative for this plugin?

+6
source share
2 answers

You can also use the data attribute:

Js

 valueNames: ['date', { name: 'timestamp', attr: 'data-timestamp' } 

HTML

 <td class="date timestamp" data-timestamp="1427713871">30/03/2015</td> 

Now you can sort by timestamp without a second or hidden field.

+7
source

I know this is an old question, but this answer can help new visitors.

Associate your date field with timestamps and then compare / sort the timestamps . It is up to you, but one suggestion is to add it as a second hidden field at the same level where you display each of your dates, and then when u sort, the whole div / tr will be executed.

Here is an example of what I used:

My list works like a table, and here <td> is the container, c_start is the actual date displayed , I show it as I want (dd / mm / yyyy, dd-mm-yy) , because it is only a display. As for sorting, I use c_start_ts and hide it. in c_start_ts I set the value to timestamp , this is a great view. Even if you later want to apply filtering, it will be easier to handle timestamps, as they can be converted to real ints.

 <td class="xlarge-head "> <span class="c_start">PUT FORMATTED DATE HERE</span> <span class="c_start_ts hide">PUT TIME STAMP HERE</span> </td> 

Example

 <td class="xlarge-head "> <span class="c_start">01/01/2015</span> <span class="c_start_ts hide">1420070400</span> </td> 

And then I pass c_start_ts to list.js to sort, td in the table will be sorted.

Just recently started to get used to list.js, it has potential, but later it will move to another library. I hope I helped :)

+6
source

Source: https://habr.com/ru/post/946789/


All Articles