How to combine 2 order type for a column?

How to combine 2 order type for a column?

lenamtllenamtl Posts: 265Questions: 65Answers: 1
edited June 2019 in Free community support

Hi,

I'm having a column that have date / or 1 of 2 strings
for example
24/10/2019 or this may be a string not set or never expire

I usually use https://datatables.net/plug-ins/sorting/date-uk to order the date column

and something like this for not conventional column
I'm not sure if I can associate a date instead of a number here
or maybe set a date in the past for the string
$.fn.dataTable.ext.type.order['contact_contacttype_php-pre'] = function(q) {
switch (q) {
case '<?php echo ('not_set'); ?>': return 1;
case '<?php echo ('never_expire'); ?>': return 2;
}
return 0;
};

So how can I combine both date uk and string so it will order dates correctly and then the strings

Note I don't want to use data_type as it causing issue with other plugin

Thanks

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Hi @lenamtl ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited September 2020

    Hi,

    I'm still looking for a way
    I have set a date instead of 1 and 2 for the values that are not a date.

    But this not seems possible to assign 2 types to the same target in columnDefs

     "columnDefs": [    
    
        { "type": "date_php-pre", "targets": 4 },
        { "type": "date-uk", "targets": 4 },            
                ],
    

    Here is the JSfiddle example
    https://jsfiddle.net/lenamtl/y3x08jd2/

    How can I achieved that?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    I'm not following. Can you give steps on how to reproduce, please?

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited September 2020

    I need to order data that have 2 formats = string and date.

    The order must be:

    not_set
    never_expire
    31/06/2020
    25/07/2020

    date_php-pre will set a date for ordering
    then date-uk will order the date

    So how could I set 2 type to the same column?

    "columnDefs": [   
    
       { "type": "date_php-pre", "targets": 4 },
       { "type": "date-uk", "targets": 4 },           
    
     ],
    

    For now when it is not_set and never_expire I echo nothing but I would like to change it and display a string then be able to order the data.

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    That's not standard, so you would need to create your own filter plugin for that.

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Is there a way to use 2 type per target in columnDefs?

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    Answer ✓

    The columns.type allows for only a string. You can only apply one columns.type per column which means only one type is supported per column. If you have a mix of strings and dates in a column Datatables will set the type to string during its type detection. Datatables has no builtin features to support multiple types in a column for sorting purposes.

    You will need to create your own custom sorting process with a Sorting Plugin.

    Kevin

This discussion has been closed.