Use Jquery on a datable that is created from an Ajax call
Use Jquery on a datable that is created from an Ajax call
growlingflea
Posts: 3Questions: 1Answers: 0
I have a datatable that is getting its data from an ajax call. I want to change the css on certain cells based on the value (YES, NO, BLANK) on cells of a certain class. Here is part of my table:
`
oTable=$('#show_sms_table').DataTable({
dom: 'Bfrtip',
buttons: [
'copy', 'excel', 'pdf'
],
ajax:{ type: "POST",
url: "../../library/ajax/sms_notification_log_report_ajax.php",
data: {
func:"show_sms_sent",
to_date: "<?php echo $form_to_date; ?>",
from_date:" <?php echo $form_from_date; ?> "
}
},
columns:[
{ 'data': 'iLogId' , },
{ 'data': 'lastUpdate', },
{ 'data': 'pid' },
{ 'data': 'eventDate' },
{ 'data': 'full_name'},
{ 'data': 'phone_cell'},
{ className: "hipaa_allowsms", 'data': 'hipaa_allowsms'},
{ 'data': 'ptSMS'},
{ 'data': 'relation1sms'},
{ 'data': 'relation2sms'},
{ 'data': 'relation3sms'},
{ 'data': 'relation4sms'},
{ 'data': 'teenConfidential'},
{ 'data': 'message'},
{ 'data': 'sentTo'},
{ 'data': 'dSentDateTime'},
{ 'data': 'alert_error_class'}
],
"iDisplayLength": 100,
"select":true,
"searching":true,
"retrieve" : true
});
`
Before I started using Datables, I used a table and the following script accomplished what I wanted to do
`
$("#sms_log_table tr").each(function() {
//color code the contact permissions
$(this).find(".hipaa_allowsms").each(function(){
var hipaa_allowsms = $(this).text().replace(/\s/g, "");
if(hipaa_allowsms === "YES"){
$(this).css('color', 'green');
}else if (hipaa_allowsms === "NO") {
$(this).css('color', 'red');
}else{
$(this).text("X").css('color', 'rgb(219, 84, 17)');
}
});
});
`
Basically, I want to iterate through each row and each cell of a particular class, and change the color of the text based on the value of the cell. Thanks for advice!!
This discussion has been closed.
Answers
Sounds like you want the
columns.createdCell
option. But you could also trycreatedRow
ordrawCallback
.Kevin
It looks like the drawCallback option might be helpful. I'm going to take a stab at it and report back. Thanks for the advice.
Its been a while since I've logged in but I was able to do the following. I'll post all my code so people needing help can see what I did.
`$(document).ready(function() {
`