Highlighting Rows
Highlighting Rows
In one of my tables there is an Enabled column that shows either Yes or No. I would like to have all the Yes rows be a light green color while all the No rows being a faded red color. Is there a way to make this happen?
I checked the examples and all I saw were the hover highlight rows example.
I checked the examples and all I saw were the hover highlight rows example.
This discussion has been closed.
Replies
Regards,
Allan
Could you show a simple example of how to get at them through the dom?
$script = '
$(document).ready(function() {
$("#example").dataTable( {
"aaSorting": [[ 3, "asc" ]],
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "'.$data_url.'",
"bAutoWidth": false,
"aoColumns": [
{ "bVisible": false },
{ "bVisible": false },
{ "bSortable": false, "bSearchable": false },
null,
null
]
} );
} );
';
A wee three line liner might do just the trick:
[code]
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
nRow.className = aData[0 /* or whatever */]=="Yes" ? "class_yes" : "class_no";
}
[/code]
Regards,
Allan
Error: A node was not returned by fnRowCallback
$script = '
$(document).ready(function() {
$("#example").dataTable( {
"aaSorting": [[ 3, "asc" ]],
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
nRow.className = aData[4]=="Yes" ? "enabled_row" : "disabled_row";
},
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/ispadmin/keyword/repdata/campaign/'.$campaign.'/",
"bAutoWidth": false,
"aoColumns": [
{ "bVisible": false },
{ "bVisible": false },
{ "bSortable": false, "bSearchable": false },
null,
null
]
} );
} );
';
thanks Allan that worked perfectly i just had to return nRow;
thanks again
Either way - sorted now :-)
Allan
thanks a lot guys ;)
[code]"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
nRow.className = aData[0 /* or whatever */]=="Yes" ? "class_yes" : "class_no";
return nRow;
}[/code]
Although it worked perfectly for me, I missed the "even" and "odd" classes inside the !
Anybody knows how to solve this problem?!
Thanks!
[code]"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
if(aData[0]=="Yes"){
nRow.className = "class_yes";
}
//please note that there are no else.
return nRow;
}[/code]
[code]
nRow.className = aData[0]=="Yes" ? "class_yes" : "class_no";
[/code]
OR
[code]
if (aData[0]=="Yes")
$(nRow).addClass("class_yes")
else
$(nRow).addClass("class_no")
[/code]
Is it fast nRow.className ="class" than $(nRow).addClass(''class'') ?
My HTML sctructure is:
[code]
Test
teste.professor@test.com
Professor
10/09/2009
Change User
Active
[/code]
The javascript that i'm using:
[code]"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
if(aData[0] == "Test"){
$(nRow).addClass("off");
}
return nRow;
}
[/code]
I need to find the html below:
[code]
Change User
Active
[/code]
not the :
[code]Test[/code]
[]z
glad to hear that it works for you.
Edited:
and about the issue, may be you like to use regular expression like:
[code]"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
if(aData[0].search("expression")>-1){
$(nRow).addClass("off");
}
return nRow;
}[/code]
You will need to replace expression to match the part you like to check with the appropriate regular expression.
I have the problem, that usually, the rows were highlighted differently depending on the fact if they were odd or even.
Now I added a fnRowCallback. It is applied, but it doesn't show the different coloring anymore.
Do I have to do this myself if I work with fnRowCallback?
edit: here is my callback:
[code]
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
var this_class = "no_change";
if ( aData[4] == "+" ) {
this_class = "this_is_good";
}else if( aData[4] == "-" ) {
this_class = "this_is_bad";
}
nRow.className = this_class;
return nRow;
},
[/code]
edit: got it. I have to *append* the class, not to override the class attribute. Instead of nRow.className = this_class; I now do: nRow.className = nRow.className + " " + this_class;
@nm_alex - heh - this follows on perfectly from what I just wrote above. One needs to be careful when accessing .className directly, and jQuery way is a little more flexible and forgiving, but comes at the cost of speed (although in this case we would likely never notice the difference).
Regards,
Allan
Basically if the word CREDIT is found in the column the css would make it green or something, if DEBIT then it would make the column red.
I was just looking at the columns sClass stuff and this seems to be what I want just not sure how to make it switch classes, something like this?
[code]
$(document).ready(function() {
$('#example').dataTable( {
"aoColumnDefs": [
{ "sClass": (/* how to reference column here? */ == 5) ? "credit" : "debit", "aTargets": [ 0 ] }
]
} );
} );
[/code]
Thanks,
Joseph Crawford
[code]
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
/* numbers less than or equal to 0 should be in red text */
if ( aData[2].indexOf('D') == 0 ) {
jQuery('td:eq(2)', nRow).addClass('transactionDebit');
} else {
jQuery('td:eq(2)', nRow).addClass('transactionCredit');
}
return nRow;
},
[/code]
I did try using nRow.className = 'blah' however that changed the entire rows text color and not just the column.