columns defs : combine render function and choose the sort value

columns defs : combine render function and choose the sort value

MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi,

I'm using 2 methods for columns def depending on the needs.

The simple one for this kind of data :

"dateFin": {
            "display": "12/01/2021",
            "sort": "2021-01-12",
            "annee": 2021,
            "moisLong": "Janvier",
            "moisCourt": "Jan",
            "special": "Jan / 2021"
        },

The column def :

{ data: { // signée le
            _: 'dateFin',
            display: 'dateFin.special',
            sort: 'dateFin.sort'
            }
        },

And sometimes I'm using the renderer for reasons like combining multiple row sources for the data or presentation. But in those cases, I don't how how to define the data to use for sorting.

For exemple I have this 2 data sources, and I want to sort the column width confirmDate.Valeur_date

"confirmNumero": {
            "Code_affaire": "BC21-FV008",
            "Code_rubrique": "NN",
            "Valeur_alpha": "20219426",
            "Valeur_date": "0000-00-00",
            "Valeur_num": 0,
            "num_ordre": 1,
            "Groupe": "",
            "ID": 855608,
            "affaire": {
                "__KEY": "BC21-FV008"
            },
            "rubrique": {
                "__KEY": "NN"
            },
            "Nature": 2,
            "libelle": "20219426"
        },
        "confirmDate": {
            "Code_affaire": "BC21-FV008",
            "Code_rubrique": "DO",
            "Valeur_alpha": "",
            "Valeur_date": "2021-01-12",
            "Valeur_num": 0,
            "num_ordre": 1,
            "Groupe": "",
            "ID": 855607,
            "affaire": {
                "__KEY": "BC21-FV008"
            },
            "rubrique": {
                "__KEY": "DO"
            },
            "Nature": 3,
            "libelle": "12/01/2021",
            "mois": 1,
            "annee": 2021,
            "jour": 12,
            "semaine": 2
        },

How can I change this column def to add the sort and keep the function render ?
I'm sure this basic question has an answer somewhere in the doc but I didn't find it.

{ data: 'confirmDate',
            render: function ( data, type, row ) {
                return 'n° ' + row['confirmNumero']['libelle'] + '<br>' + data.libelle ;
            }
        }

PS : I tried this but, as expected, it didn't work

{ data: { 
            _: 'confirmDate',
            display: render: function ( data, type, row ) {
                return 'n° ' + row['confirmNumero']['libelle'] + '<br>' + data.libelle ;
            },
            sort: 'confirmDate.Valeur_date'
            }
        }

Thank you

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited October 2023 Answer ✓

    Use Orthogonal data similar to the Computed Values section. Something like this:

    { data: 'confirmDate',
                render: function ( data, type, row ) {
                    if ( type === 'display' ) { // Data to display
                      return 'n° ' + row['confirmNumero']['libelle'] + '<br>' + data.libelle ;
                    }
                    if ( type === 'sort' ) {  // Data for sort operation
                      return data.Valeur_date;
                    }
                    return data;  // filter and type operations
                }
            }
    

    Kevin

  • MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

    Thank you once again Kevin for a five stars answer !
    It's so easy with the good info/doc :)

Sign In or Register to comment.