Changing row colors according to value of some columns from a separate method
Changing row colors according to value of some columns from a separate method
sumitshining
Posts: 12Questions: 0Answers: 0
Hi,
I have a method to render the datatable. The same method is used to render it on different pages with different content.
For a specific page I need to customize my rows by adding background colors to those with a specific value in one of the columns. So, I can't specify anything like creating customized class names to the rows, etc. I need to add a separate method where this task shall be accomplished. How shall I proceed?
I have a method to render the datatable. The same method is used to render it on different pages with different content.
For a specific page I need to customize my rows by adding background colors to those with a specific value in one of the columns. So, I can't specify anything like creating customized class names to the rows, etc. I need to add a separate method where this task shall be accomplished. How shall I proceed?
This discussion has been closed.
Replies
[code]
$('tbody td').each( function () {
if ( $(this).text().indexOf("the text you are searching for") >= 0 ) {
$(this).parent('tr').addClass('specialColor'); // this targeting may not work with FixedColumns
}
});
[/code]
[code]
if ( $(this).text().indexOf("the text you are searching for") >= 0 ) {
[/code]
with something more suitable.
Why don't you create a test case on live.datatables.net ?
you should probably check the function fnGetData.
I guess this is what you need to get the data out of a table cell, put it into a variable, so you can calculate the ratio : http://datatables.net/api
When you initialize the table do it that way :
[code]var oTable = = $('#example').dataTable({});[/code]
so you can access all data using the oTable variable later on.
For example, looping through all rows :
[code]
var total = oTable.fnSettings().fnRecordsTotal();
for(i = 0; i < total; i++) {
var row = oTable.fnGetData(i);
console.log(row);
}
[/code]
But as you will see in the documentation, you may access TDs directly too.
Hope this helps.
Further, it asks to use bRetrieve or bDestroy. When I use these nothing happens. The problem remains same.
The page actually loads in two parts. At first, the page is rendered and then, the table is rendered through an ajax call. Can this be the problem?
Allan
I have updated the test case, http://live.datatables.net/ewemuz/5
[code]$('body').ajaxComplete(function() {
var datatable = $('#orders-table').dataTable();
var total = datatable.fnSettings().fnRecordsTotal();
for(i = 0; i < total; i++) {
console.log(i);
var row = datatable.fnGetData(i);
if(row == null)
return;
if(row[12]/row[13] > 5){
$('td', row).css('background-color', '#FFDDDD');
}
//return row;
}
});
[/code]
The problem is, all the rows turn red if any of them satisfy the condition. What shall be done?