Magic ETL

Magic ETL

HTML Table Conditional Row Formatting

I have implemented some conditional formatting using this code in an HTML table:

$(myTable).DataTable({   

data: reorderedData,   

lengthMenu: itemsPerPageOptions,   

columns: colNames,   

createdRow: function(row, data, dataIndex) {                 

var daysOld = data[0]; // Correctly reference the first column

if (daysOld == 1) {                   

$(row).css('background-color', 'lightyellow');                 

} else if (daysOld > 1) {

$(row).css('background-color', 'lightcoral');                 

}

});

It works except that the first column (daysOld) is not highlighted. I'm not sure why. What am I doing wrong?

domo.png

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In

Best Answer

  • Coach
    Answer ✓

    The issue appears to be how the datatables library handles rendering. It applies the styles after rendering the data, which appears to be causing it to skip the first column.

    You can try applying the formatting during the drawCallback.

    1. $(myTable).DataTable({
    2. data: reorderedData,
    3. lengthMenu: itemsPerPageOptions,
    4. columns: colNames,

    5. createdRow: function(row, data, dataIndex) {
    6.  
    7. // This formatting applies during the row creation phase
    8. var daysOld = data[0]; // First column value (Days Old)

    9. if (daysOld == 1) {
    10. $(row).css('background-color', 'lightyellow');
    11. } else if (daysOld > 1) {
    12. $(row).css('background-color', 'lightcoral');
    13. }
    14. },


    15. // Add a drawCallback function to apply additional formatting
    16. drawCallback: function() {
    17. var table = $(myTable).DataTable();

    18. // Iterate through all rows to ensure formatting applies
    19. table.rows().every(function(rowIdx, tableLoop, rowLoop) {
    20. var data = this.data();
    21. var row = this.node();

    22. var daysOld = data[0]; // Get the value of the first column

    23. if (daysOld == 1) {
    24. $(row).css('background-color', 'lightyellow');
    25. } else if (daysOld > 1) {
    26. $(row).css('background-color', 'lightcoral');
    27. }
    28. });
    29. }
    30. });

    or target the specific cell in the first column rather than the row.

    1. $(myTable).DataTable({
    2. data: reorderedData,
    3. lengthMenu: itemsPerPageOptions,
    4. columns: colNames,

    5. createdRow: function(row, data, dataIndex) {
    6. var daysOld = data[0]; // First column value (Days Old)

    7. // Apply background color to the first column (specific cell)
    8. var firstCell = $('td', row).eq(0); // Target the first column

    9. if (daysOld == 1) {
    10. $(row).css('background-color', 'lightyellow'); // Entire row
    11. firstCell.css('background-color', 'lightyellow'); // First column
    12. } else if (daysOld > 1) {
    13. $(row).css('background-color', 'lightcoral'); // Entire row
    14. firstCell.css('background-color', 'lightcoral'); // First column
    15. }
    16. }
    17. });

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

Answers

  • Coach
    Answer ✓

    The issue appears to be how the datatables library handles rendering. It applies the styles after rendering the data, which appears to be causing it to skip the first column.

    You can try applying the formatting during the drawCallback.

    1. $(myTable).DataTable({
    2. data: reorderedData,
    3. lengthMenu: itemsPerPageOptions,
    4. columns: colNames,

    5. createdRow: function(row, data, dataIndex) {
    6.  
    7. // This formatting applies during the row creation phase
    8. var daysOld = data[0]; // First column value (Days Old)

    9. if (daysOld == 1) {
    10. $(row).css('background-color', 'lightyellow');
    11. } else if (daysOld > 1) {
    12. $(row).css('background-color', 'lightcoral');
    13. }
    14. },


    15. // Add a drawCallback function to apply additional formatting
    16. drawCallback: function() {
    17. var table = $(myTable).DataTable();

    18. // Iterate through all rows to ensure formatting applies
    19. table.rows().every(function(rowIdx, tableLoop, rowLoop) {
    20. var data = this.data();
    21. var row = this.node();

    22. var daysOld = data[0]; // Get the value of the first column

    23. if (daysOld == 1) {
    24. $(row).css('background-color', 'lightyellow');
    25. } else if (daysOld > 1) {
    26. $(row).css('background-color', 'lightcoral');
    27. }
    28. });
    29. }
    30. });

    or target the specific cell in the first column rather than the row.

    1. $(myTable).DataTable({
    2. data: reorderedData,
    3. lengthMenu: itemsPerPageOptions,
    4. columns: colNames,

    5. createdRow: function(row, data, dataIndex) {
    6. var daysOld = data[0]; // First column value (Days Old)

    7. // Apply background color to the first column (specific cell)
    8. var firstCell = $('td', row).eq(0); // Target the first column

    9. if (daysOld == 1) {
    10. $(row).css('background-color', 'lightyellow'); // Entire row
    11. firstCell.css('background-color', 'lightyellow'); // First column
    12. } else if (daysOld > 1) {
    13. $(row).css('background-color', 'lightcoral'); // Entire row
    14. firstCell.css('background-color', 'lightcoral'); // First column
    15. }
    16. }
    17. });

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

  • Thanks! I'm going to give these a try.

    Is this something that should be reported as an issue?

  • I went with the second option. It works perfectly. Thanks!

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In