0
votes

I am using datatables and would like to over-ride the contents of a cell depending on the value it contains. I am using '1' to flag true in the underlying database and '0' for false. I am attempting to use the Columndefs render function to do this.

Here is my code..

xample_table = $('#treatment_list').DataTable(
    {
    select: true,
    destroy: true,
     "order": [ 0, 'desc' ],
        data:json,
        columns : [
            {'data':'treatment_name'},
            {'data':'description'},
             {'data':'measured_in'},
            {'data':'exclusive'}
            ],
       "columnDefs": [
            {      
              "targets":3,
              "data" : "exclusive",
                "render": function ( data, type, row ) {
                    if (type === 'display'){
               
                        if (row.exclusive == '0')
                            {
                             return 'No';
                            }
                        else
                         return 'Yes';
                    }
                    
                    
                }
            }
        ]

}

);

The problem is I get an erro message from datatables that reads..

DataTables warning: table id=treatment_list - Requested unknown parameter 'exclusive' for row 0, column 3....

Apart from the error message, it is in fact working.

1
You can probably see for yourself, by adding console.print( data ); and console.print( row ); to the body of your column render function. If that does not help, then you can edit your question to show us the JSON. (Use F12 to open the browser's console, if you are not familiar with it.)andrewJames

1 Answers

0
votes

thanks for the advice,but the json was fine and the other variables did not reveal much as I had console logged them before posting here. I did find a solution and am posting it here in case it helps anyone else. This expression works :

 "columnDefs": [
        {
            "render": function ( data, type, row ) {
                if (data =='1') {
                    return "True" ;
                    
                }
                else{
                    return "False" ;
                }
                
            },
            "targets": [3]
        }
    ]

In all of the examples I have found, they often included the name of the column being affected and I think this is what caused the error message , whereas - you really just need to include the "targets" : [n] where 'n' is the column number. Perhaps it depends on how the table is constructed ?

Thanks.