How to Force columns data type in order to reorder correctly

How to Force columns data type in order to reorder correctly

Filipe_ApostoloFilipe_Apostolo Posts: 4Questions: 1Answers: 0

Hi to all,
I tried recently this datatables.js and I immediately get in love with it, congrats to developers!!!

I am creating a performance table of my co-workers, the intent is to keep track of their work and understand our overall efforts.

The efforts are showed by current month totals / total year
So in the first column I put the name and the other columns have their efforts indicators. Here goes an example

NAME | Visits realized | Opportunities Created | Projects Conluded
Jhon S | 10/ 300 | 2/44 |1/25

again: 10/300 means 10 this month and 300 in the current year.

Problem:
When I try to reorder by column it does not order by the number at left of the slash, it seems that it is somehow considering the data as a date I try to change the / by an # and the result is the same

How can I force to be interpreted as a String instead of date, or witch is the prettiest way to present this type of information

Thanks in advance!!

Answers

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975

    See if setting columns.type to string resolves the issue. The Datatables automatic type detection might be detecting that column as a date.

    Kevin

  • Filipe_ApostoloFilipe_Apostolo Posts: 4Questions: 1Answers: 0

    Thanks kthorngren,
    I read your answer and tried.
    However I forced the columns to be trated as string:

     $(document).ready( function () {
            $('#datatablesSimple').DataTable(
                {
                    "columns": [
                    { "type": "string" },
                    { "type": "string" },
                    { "type": "string" },
                    { "type": "string" },
                    { "type": "string" },
                    { "type": "string" },
                    { "type": "string" },
                    { "type": "string" }
                    ]
                }
            );
        } );
    

    The column is ordered as the previews picture

  • Filipe_ApostoloFilipe_Apostolo Posts: 4Questions: 1Answers: 0

    Well I've understood, what happens!! It was so obvious that I wasn't seeing...

    When it does not match any of the automatic type detection ti falls back to string as explained here https://datatables.net/reference/option/columns.type

    However when it is a string the ordering will consider 7 higher than 45 ahahaha, 7 is higher than char 4 duh. it will not work using string as data type

  • Filipe_ApostoloFilipe_Apostolo Posts: 4Questions: 1Answers: 0

    After some experiments, in which i tried to place the total as tooltip , using column_type as htm-num-fmt, and other combinations without success I quited and used str_pad to format the string with leading zeros.

     str_pad($currentMonth,3,'0',STR_PAD_LEFT).'/'.$totalYear
    

    Output:

  • kthorngrenkthorngren Posts: 21,459Questions: 26Answers: 4,975

    Don't know if either of these will work for your case but you might try the any-number or natural sorting plugins. You can create your own sorting plugin. Or maybe just using orthogonal data, similar to what you did above, will work.

    If you want help with any of these please provide a simple test case with an example of your data.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

Sign In or Register to comment.