When populating the cells, append the cell value at the following conditions:
- We are at the header row
- We are at columns 1-6 (
colIndex
0-5), or - We are at columns 7 or 8, given that column 6 has the value of “error”
Which we can select using this:
if( rowIndex == 0 || colIndex < 6 || (colIndex == 6 || colIndex == 7) && r[5].toLowerCase().trim() === "error") {
row.append(cell);
}
Here’s a running demo (I’ve changed some parts of your code slightly):
data = [
['col1', 'col2', 'col3', 'col4', 'col5', 'col6', 'col7', 'col8'],
['jack', 'smith', '23', 'Y', 'Y', 'error', 'error_code', 'error_desc'],
['jack2', 'smith2', '232', 'Y2', 'Y2', 'no_error', 'error_code2', 'error_desc2']
];
function makeTable(container) {
var table = $("<table/>");
$.each(data, function(rowIndex, r) {
var row = $("<tr/>");
$.each(r, function(colIndex, c) {
var cellMarkup = "<t" + (rowIndex == 0 ? "h" : "d") + "/>";
var cell = $(cellMarkup);
cell.text(c);
if (colIndex == 5) {
if (c.toLowerCase().trim() === "error") {
cell.addClass("error blink");
}
}
if (rowIndex == 0 || colIndex < 6 || (colIndex == 6 || colIndex == 7) && r[5].toLowerCase().trim() === "error") {
row.append(cell);
}
});
table.append(row);
});
return container.html(table);
}
makeTable($('.container'))
.error {
color: red;
}
.blink {
/* oh god no ;-) */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container"></div>