createdRow not parsing when using JSON data to populate table with child rows
createdRow not parsing when using JSON data to populate table with child rows
![indymx](https://datatables.net/forums/uploads/userpics/131/n9D32HYOVZPK7.jpg)
I'm trying to implement a table with child rows. But I need to use createdRow to highlight certain rows based on data in those rows.
I have the following in my datatables initialization code:
[code]"createdRow": function (row, data, dataIndex) {
if (data[9] == "Scheduled") {
tr.addClass('success');
}
if (data[9] == "Late" || data[9] == "Broke Down") {
tr.addClass('warning');
}
else if (data[8] <= date) {
tr.addClass('danger');
}
},[/code]
A form of that works on the table I created with PHP, that doesn't have child rows. But I've since created child rows that load from JSON data, which works well, however, the created row code doesn't function at all. I've placed a couple of alert() functions in just to test to see if the code is matching on those cells, but never get the alerts either.
I'm guessing that the createdRow part of this is running before the rows are actually created by the ajax: portion.. am I wrong there?
Any suggestions to get this working?
the full code:
[code]
var date = dateFormat(new Date(), "mm-dd HH:MM");
$(document).ready(function() {
var table = $('#table').DataTable( {
"lengthChange": true,
"sort": false,
"info": false,
"length": 15,
"paging": true,
"pagingType": "simple",
"lengthMenu": [ [15, 30, 50, -1], [15, 30, 50, "All"] ],
"serverSide": true,
"ajax": "Scripts/loadData.php",
"columns": [
{
"class": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "customer" },
{ "data": "shipper" },
{ "data": "consignee" },
{ "data": "tracking" },
{ "data": "carrier" },
{ "data": "pickupTime" },
{ "data": "deliveryTime" },
{ "data": "nextUpdate" },
{ "data": "status" }
],
"createdRow": function (row, data, dataIndex) {
if (data[9] == "Scheduled") {
tr.addClass('success');
}
else if (data[9] == "Late" || data[9] == "Broke Down") {
tr.addClass('warning');
}
else if (data[8] <= date) {
tr.addClass('danger');
}
}
} );
// Add event listener for opening and closing details
$('#table tbody').on('click', 'td.details-control', function () {
var tr = $(this).parents('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
} );
[/code]
I have the following in my datatables initialization code:
[code]"createdRow": function (row, data, dataIndex) {
if (data[9] == "Scheduled") {
tr.addClass('success');
}
if (data[9] == "Late" || data[9] == "Broke Down") {
tr.addClass('warning');
}
else if (data[8] <= date) {
tr.addClass('danger');
}
},[/code]
A form of that works on the table I created with PHP, that doesn't have child rows. But I've since created child rows that load from JSON data, which works well, however, the created row code doesn't function at all. I've placed a couple of alert() functions in just to test to see if the code is matching on those cells, but never get the alerts either.
I'm guessing that the createdRow part of this is running before the rows are actually created by the ajax: portion.. am I wrong there?
Any suggestions to get this working?
the full code:
[code]
var date = dateFormat(new Date(), "mm-dd HH:MM");
$(document).ready(function() {
var table = $('#table').DataTable( {
"lengthChange": true,
"sort": false,
"info": false,
"length": 15,
"paging": true,
"pagingType": "simple",
"lengthMenu": [ [15, 30, 50, -1], [15, 30, 50, "All"] ],
"serverSide": true,
"ajax": "Scripts/loadData.php",
"columns": [
{
"class": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "customer" },
{ "data": "shipper" },
{ "data": "consignee" },
{ "data": "tracking" },
{ "data": "carrier" },
{ "data": "pickupTime" },
{ "data": "deliveryTime" },
{ "data": "nextUpdate" },
{ "data": "status" }
],
"createdRow": function (row, data, dataIndex) {
if (data[9] == "Scheduled") {
tr.addClass('success');
}
else if (data[9] == "Late" || data[9] == "Broke Down") {
tr.addClass('warning');
}
else if (data[8] <= date) {
tr.addClass('danger');
}
}
} );
// Add event listener for opening and closing details
$('#table tbody').on('click', 'td.details-control', function () {
var tr = $(this).parents('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
} );
[/code]
This discussion has been closed.
Replies
This example shows createdRow working with Ajax loaded here: http://live.datatables.net/menecol/1/edit
Allan
This one fails.. I can't see much difference between it and the link you sent, other than the columns definition..
You were checking an array ( `data[1]` ), but the data being loaded is an object ( `data.position` ).
I haven't checked the link yet (will do tomorrow if I can), but it might be the same issue?
Allan
So access the data from the object, like you would with any Javascript object. `data.status` probably in this case.
Allan
Thanks a lot Allan.
Now to figure out how to get a sub table working.
Why do I always try to do crap that's hard and mostly undocumented.. ;)
How DataTables handles data is documented here: http://next.datatables.net/manual/data#Data-source-types
Allan
If I could only get someone with decision making ability here at work to approve this app.. You know how that goes.. ;)
Good day friend.