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?
Welcome!
Best 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.- $(myTable).DataTable({
- data: reorderedData,
- lengthMenu: itemsPerPageOptions,
- columns: colNames,
- createdRow: function(row, data, dataIndex) {
- // This formatting applies during the row creation phase
- var daysOld = data[0]; // First column value (Days Old)
- if (daysOld == 1) {
- $(row).css('background-color', 'lightyellow');
- } else if (daysOld > 1) {
- $(row).css('background-color', 'lightcoral');
- }
- },
- // Add a drawCallback function to apply additional formatting
- drawCallback: function() {
- var table = $(myTable).DataTable();
- // Iterate through all rows to ensure formatting applies
- table.rows().every(function(rowIdx, tableLoop, rowLoop) {
- var data = this.data();
- var row = this.node();
- var daysOld = data[0]; // Get the value of the first column
- if (daysOld == 1) {
- $(row).css('background-color', 'lightyellow');
- } else if (daysOld > 1) {
- $(row).css('background-color', 'lightcoral');
- }
- });
- }
- });
or target the specific cell in the first column rather than the row.
- $(myTable).DataTable({
- data: reorderedData,
- lengthMenu: itemsPerPageOptions,
- columns: colNames,
- createdRow: function(row, data, dataIndex) {
- var daysOld = data[0]; // First column value (Days Old)
- // Apply background color to the first column (specific cell)
- var firstCell = $('td', row).eq(0); // Target the first column
- if (daysOld == 1) {
- $(row).css('background-color', 'lightyellow'); // Entire row
- firstCell.css('background-color', 'lightyellow'); // First column
- } else if (daysOld > 1) {
- $(row).css('background-color', 'lightcoral'); // Entire row
- firstCell.css('background-color', 'lightcoral'); // First column
- }
- }
- });
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 - $(myTable).DataTable({
Answers
-
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.- $(myTable).DataTable({
- data: reorderedData,
- lengthMenu: itemsPerPageOptions,
- columns: colNames,
- createdRow: function(row, data, dataIndex) {
- // This formatting applies during the row creation phase
- var daysOld = data[0]; // First column value (Days Old)
- if (daysOld == 1) {
- $(row).css('background-color', 'lightyellow');
- } else if (daysOld > 1) {
- $(row).css('background-color', 'lightcoral');
- }
- },
- // Add a drawCallback function to apply additional formatting
- drawCallback: function() {
- var table = $(myTable).DataTable();
- // Iterate through all rows to ensure formatting applies
- table.rows().every(function(rowIdx, tableLoop, rowLoop) {
- var data = this.data();
- var row = this.node();
- var daysOld = data[0]; // Get the value of the first column
- if (daysOld == 1) {
- $(row).css('background-color', 'lightyellow');
- } else if (daysOld > 1) {
- $(row).css('background-color', 'lightcoral');
- }
- });
- }
- });
or target the specific cell in the first column rather than the row.
- $(myTable).DataTable({
- data: reorderedData,
- lengthMenu: itemsPerPageOptions,
- columns: colNames,
- createdRow: function(row, data, dataIndex) {
- var daysOld = data[0]; // First column value (Days Old)
- // Apply background color to the first column (specific cell)
- var firstCell = $('td', row).eq(0); // Target the first column
- if (daysOld == 1) {
- $(row).css('background-color', 'lightyellow'); // Entire row
- firstCell.css('background-color', 'lightyellow'); // First column
- } else if (daysOld > 1) {
- $(row).css('background-color', 'lightcoral'); // Entire row
- firstCell.css('background-color', 'lightcoral'); // First column
- }
- }
- });
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 - $(myTable).DataTable({
-
Thanks! I'm going to give these a try.
Is this something that should be reported as an issue?0 -
I went with the second option. It works perfectly. Thanks!
1
Welcome!
Welcome!
Categories
- All Categories
- 2K Product Ideas
- 2K Ideas Exchange
- 1.6K Connect
- 1.3K Connectors
- 311 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 3.8K Transform
- 659 Datasets
- 117 SQL DataFlows
- 2.2K Magic ETL
- 816 Beast Mode
- 3.3K Visualize
- 2.5K Charting
- 83 App Studio
- 46 Variables
- 777 Automate
- 190 Apps
- 481 APIs & Domo Developer
- 83 Workflows
- 23 Code Engine
- 41 AI and Machine Learning
- 20 AI Chat
- 1 AI Playground
- 2 AI Projects and Models
- 18 Jupyter Workspaces
- 411 Distribute
- 120 Domo Everywhere
- 280 Scheduled Reports
- 11 Software Integrations
- 145 Manage
- 141 Governance & Security
- 8 Domo Community Gallery
- 48 Product Releases
- 12 Domo University
- 5.4K Community Forums
- 41 Getting Started
- 31 Community Member Introductions
- 115 Community Announcements
- 4.8K Archive