Search code examples
laravel-5datatablesyajra-datatable

datatables change row background colour on basis of cell column


I have a table in a Laravel application and on the basis of a cell content I want to change the colour of the row.

Part of my datatables javascript is

ajax:"{{ route('propertiesData') }}",
          columns: [
              { data: 'address', name: 'address' },
              {data: 'town', name: 'town'},
              {data: 'postcode', name: 'postcode'},
              {data: 'units',name: 'units'},
              { data: 'examination', name: 'examination' },
              {data: 'priority', name:'priority', searchable: false},
              {data: 'completed', name:'completed'},
              {data: 'action', name: 'action', orderable: false, searchable: false},
          ],
          "createdRow": function( row, data, index ) {
                if ( data[6] == "1" )
                {
                  $(row).addClass( 'redRow' );
                }
            },

The priority field is either 1 or 0.

The table works but the background colour is always white.


Solution

  • data is most likely an object so you have to access the values differently, try this:

    "createdRow": function (row, data, index) {
        if (data.priority === "1") {
            $(row).addClass('redRow');
        }
    }