Search code examples
javascriptjquerydatatabledatatablesserver-side

Datatables change color background


I'm developing a table based on Datatables with server side process.

I want to change the background color for each cell if the value is different from the previous row in the same cell position

This is my output table generated with server side process:

<table>
    <tr>
        <td>Acer computer</td>
        <td>Ram 32 gb</td>
        <td>$500</td>
    </tr>
    <tr>
        <td>Acer computer</td>
        <td>Ram 32 gb</td>
        <td>$500</td>
    </tr>
    <tr>
        <td>Acer computer</td>
        <td>Ram 16 gb</td>
        <td>$320</td>
    </tr>
</table>

I want to change the background color of the TD for the second TR because the value changed.

I tryed with createdCell but with bad results.

https://datatables.net/reference/option/columns.createdCell

Is possible with datatables? Is there a jsfiddle available for help?


Solution

  • Solved with Datatables API here the code:

    "columnDefs": [ {
        "targets": [1,2,3,4,5,6,7,8,9,10,11,12,13,14],
        "createdCell": function (td, cellData, rowData, row, col) {
            if (row > 0) {
                old = table.data(row - 1);
                if ( cellData != old.row(row).column(col).data()[row-1] ) {
                    previous_row = old.row(row).column(col).nodes()[row-1]
                    $(previous_row).css('background-color', '#FFFF99')
                }
            }
        }
        } ]