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

indymxindymx Posts: 63Questions: 3Answers: 0
edited March 2014 in DataTables 1.10
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]

Replies

  • indymxindymx Posts: 63Questions: 3Answers: 0
    anyone?
  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin
    We'd need a link to a test case showing the problem I think, so it can be debugged.

    This example shows createdRow working with Ajax loaded here: http://live.datatables.net/menecol/1/edit

    Allan
  • indymxindymx Posts: 63Questions: 3Answers: 0
    I sent you a link in a PM. The page is defendant on login info for a few things.
  • indymxindymx Posts: 63Questions: 3Answers: 0
    http://live.datatables.net/gekuhol/2/edit


    This one fails.. I can't see much difference between it and the link you sent, other than the columns definition..
  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin
    http://live.datatables.net/jasumig/1/edit

    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
  • indymxindymx Posts: 63Questions: 3Answers: 0
    ok.. Hmmm.. I get what you're saying, but I'm not 100% sure how to fix it.
  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin
    In the link you sent me you are checking `data[9]` - which is fine, if your data was an array with 10 or more elements. But it is not. it is an object.

    So access the data from the object, like you would with any Javascript object. `data.status` probably in this case.

    Allan
  • indymxindymx Posts: 63Questions: 3Answers: 0
    edited March 2014
    Ok, got it.. going to try on my code now.
  • indymxindymx Posts: 63Questions: 3Answers: 0
    Works like I need it to, however the last condition isn't evaluating, but that's likely something else entirely..

    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.. ;)
  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin
    Just the way programming is... :-)

    How DataTables handles data is documented here: http://next.datatables.net/manual/data#Data-source-types

    Allan
  • indymxindymx Posts: 63Questions: 3Answers: 0
    I've read that.. I try to read as much as I can before asking stupid questions.. ;)

    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.
This discussion has been closed.