DataTable Change Row Background Color Based On Cell Value

DataTable Change Row Background Color Based On Cell Value
----------------------------------------------

This is how managed to change my data table row background (DataTables 1.10.19)

$('#memberList').DataTable({
    "processing": true,
    "serverSide": true,
    "pageLength":25,
    "ajax":{
        "dataType": "json",
        "type": "POST",
        "url": mainUrl+"/getMember",
       },
    "columns": [
        { "data": "id" },
        { "data": "name" },
        { "data": "email" },
        { "data": "phone" },
        { "data": "country_id" },
        { "data": "created_at" },
        { "data": "action" },
    ],
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
        switch(aData['country_id']){
            case 1:
                $('td', nRow).css('background-color', '#dacfcf')
                break;
        }
    }
});

You can use fnRowCallback method function to change the background.

 

Categories: Java Script Tags: #JavaScript,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.