Changing colour of cells after column hiding ?
Changing colour of cells after column hiding ?
Khalid Teli
Posts: 251Questions: 71Answers: 0
Hi,
In my datatable I am using rowcallback function to change colour of some cells based on the data :
"rowCallback": function( row, data, index ) {
if (($(row).find('td:nth-child(14)').text()) < 0 ) {
$('td:eq(13)', row).addClass('diff_color');
}
if (($(row).find('td:nth-child(20)').text()) >0 ) {
$('td:eq(19)', row).addClass('value_color');
}
if (($(row).find('td:nth-child(21)').text()) >0 ) {
$('td:eq(20)', row).addClass('value_color');
}
} ,
However when I use DrawCallback function to hide some columns base on condition, it doesn't seem to work well with column hiding
drawCallback: function () {
var sum = $('#agreement').DataTable().column(12).data().sum();
},
"drawCallback": function () {
var tabletwo = $('#agreement').DataTable();
var api = this.api();
tabletwo.columns().every( function () {
var columnHeader = api.columns().header();
var columnHeader = $(this.header()).text();
if (columnHeader == "0"){
this.visible(false);
}
else {
this.visible(true);
}
});
},
How can I fix this so the rowcallback function works well with column hiding?
Thank you in advance
This discussion has been closed.
Answers
See if using
column().index()
using thevisible
parameter in therowCallback. Then replace the
td:nth-child(14)with something
td:nth-child(' + visIndex +')where
visIndex is the result of the -api column().index()`.Kevin
Hi @kthorngren
Used this but it doesnt seem to work
Thank you
What happens?
Have you debugged the values of
idx
?Do you also need to do the same with
$('td:eq(13)', row)
?Kevin
Hi @kthorngren
Tried to debug and I think I found the issue:
this did the trick.
The problem is when the column on which the operation is being performed is hidden. For example, in my case if column 14 is hidden and then when I try to make it visible again , the console.log(idx) is NULL and hence doesn't work.
Please provide a test case showing the problem so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
I updated the example from your other thread an it works when making a column visible.
http://live.datatables.net/lequreko/1/edit
Kevin
Hi @kthorngren
In the example you provided, if you do this console.log(table.column(1).index('visible')) it outputs null
That is exactly what is happening, with my example , some times based on the condition in
callDrwabackfunction
the column (14) gets hidden too and that is when the output of console.log(idx) is nulli am trying my best to produce a test case which replicates the issue
THANK YOU
Yes, if you have that column hidden then there is no visible index. Plus you won't be able to use the jQuery selector
td:eq(13)
to add the class.For the
($(row).find('td:nth-child(14)').text()
part just use thedata
parameter, of therowCallback
function, to access the column data via Datatables instead of trying to access atd
that might be hidden.Kevin
@kthorngren thank you.
I have implemented this and accesses the column data via datatable.
How should I deal with $('td:eq('+idxx+')', row).addClass('diff_color'); because it is hidden sometimes as well based on condition and when it is hidden the
console.log(idxx) output is null which is creating issue.
Kevin may reply, but I'm not following that thread. Could you update that test case to demonstrate the problem, please.
Colin
I have tried my best to replicate the issue and here is the link :
live.datatables.net/favivicu/1/edit
When you hide the column (age) the addClass is not reflected on the next page
Thank you
See this example:
http://live.datatables.net/favivicu/2/edit
I changed it to hide the Age column, looks like that is what you intended to do. Also added the use of
column().index()
with thevisible
parameter. Look at the console. It showsrowCallback
runs beforedrawCallback
.In your example the
addClass('diff_color')
is applied initially totd:eq(5)
because it is in the DOM. ThenrowCallback
. runs an hides the column. Go to Page 2 the column is already hidden sotd:eq(5)
is not in the DOM.In my example the index is based on the visible index and is applied appropriately.
Kevin
Hi @kthorngren
Thank you very much. I understand that index is based on the columns which are visible.
How will one approach to a problem, when the column in question is not visible?
I am sorry if I am not able to explain properly, I have update the example to replicate the issue:
live.datatables.net/dahiloka/1/
1) when you open the link and run it, it works fine. As the salary column colour is change
2) if you hide the salary column using the column hide button
salary
at top and then go topage 2
, the change is not reflectedI can understand the reason for the change not being reflected because index is based on visible columns only
Is there a way to reflect the change even if the column is hidden at initialization ?
Thank you
When you look at the console you will see the
null
output from yourrowCallback
. Then you will see thedrawCallback
runs and makes the column visible since its notAge
.Instead of using
rowCallback
you will need to loop through the rows after adjusting the column visibility indrawCallback
. Userows().every()
with aselector-modifier
of{page: "current"}
. In the loop usecell().node()
to get thetd
to useaddClass()
. See this example:http://live.datatables.net/dahiloka/2/edit
Kevin
@kthorngren
that you very much for your support.
You guys are amazing!