I would like to highlight the rows which are having previous date by comparing with most recent date from the DataTable Column.
Please advise. Thanks in advance!
I would like to highlight the rows which are having previous date by comparing with most recent date from the DataTable Column.
Please advise. Thanks in advance!
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 24, 2023, 4:34 AM EST
Could you clarify your question more concisely with some example and please explain what you have accomplished so far?
Satish Bingi39 reputationPosted Nov 24, 2023, 6:04 AM EST
Here is the output I am looking for.
Amit MohantyRank #112 · 20K reputationPosted Nov 24, 2023, 7:31 AM EST
Still not clear. What is the logic behing it and explain what you have done till now?
Satish Bingi39 reputationPosted Nov 24, 2023, 12:35 PM EST
I tried with the below.. however, it's highlighting only one cell instead of entire row and the other rows with older dates
@foreach (var item in Model)
{
@Html.DisplayFor(model => item.birthDate)
@Html.DisplayFor(modelItem => item.name)
@Html.DisplayFor(modelItem => item.group)
$(function (td, cellData, rowData, row, col) {
var maxCell = undefined;
var maxVal = undefined;
$('.dateRow').each(function () {
var that = $(this);
var dt = new Date(that.text());
if (!maxCell || dt < maxVal) {
maxCell = that;
maxVal = dt;
}
});
maxCell.addClass('highlight');
});
.highlight {
background-color: red;
}
Sam HobbsRank #107 · 20.5K reputationPosted Nov 24, 2023, 8:02 PM EST
Your code has:
maxCell is set to $(this) but I do not know what $(this) is. The name maxCell implies it is a cell. You say you want an entire row to be Red but you are only getting a single cell highlighted. I assume that highlighted means Red. The only place I see the color Red is for the highlight class. If you are setting only a cell to have that class then you are getting only a cell with that class.
Satish Bingi39 reputationPosted Nov 27, 2023, 10:36 AM EST
Thanks for your quick support!
Here, I tried with the below code which is working fine.
Howevr, in the date place ('9/8/2000') I want to call the max date variable, so that all the rows except max date will highlight.
"targets": [0],
"createdCell": function (td, cellData, rowData, row, col) {
debugger;
if (cellData < '9/8/2000') {
var rowIndex = row + 1;
$($('#tblData tbody tr:nth-child(' + rowIndex + ')')).addClass('highlight');
}
}