Trying to using ajax to fill child rows.
Trying to using ajax to fill child rows.
rbyrns
Posts: 36Questions: 9Answers: 0
I decided that is was better to get my notes from a separate ajax call.
I have the function:
function format ( notes ){
//var noteArray = notes; //used to be notes.notes
if(notes.length === 0){ return "<td>No Notes Found</td>"};
console.log(notes.length);
var i = 0;
var len = notes.length;
var out;
for(;i<len; i++){
out += '<tr><td>'+notes[i].type+'</td><td>'+notes[i].note+'</td></tr>'
}
return out;
};
Below I have this:
var detailRows = []; //sub row stuff
$('#vert_test tbody').on('click','td.details-control', function(){
var tr = $(this).closest('tr');
var row = table.row( tr );
var idx = $.inArray(tr.attr('id'), detailRows ); //??
$.ajax({
type:'GET',
url: './php/table.vert_test.php?p=1',
data: 'row='+tr.attr('id'),
})
if (row.child.isShown()){
tr.removeClass('details');
row.child.hide();
// remove from the 'open' array
detailRows.splice(idx,1); //??
}
else {
tr.addClass('details');
row.child(format(row.data())).show();
//add to the open array
if (idx=== -1){
detailRows.push(tr.attr('id'));
}
}
}); //end of click function
the ajax Get is firing, an array of notes is returning. Everything looks right, but the error is "TypeError: row.child(...) is undefined.
This discussion has been closed.
Answers
Are you using
$().DataTable()
to initialise the table? It sounds like you might be using$().dataTable()
(note the difference in capitalisation) - FAQs.Regards,
Allan
This is my initialization code.
Because this ajax call is just for the child rows and not the entire table, do I need to use DataSrc?
You con't appear to assign
$('#vert_test').DataTable(...);
to anything. So where does thetable
variable in the code in your previous post come from?Allan
Sorry, didn't get that part.
}(jQuery));
Okay, that looks like it should probably work. I think I'd need a link to the page to see the error "in action" to be able to offer any help with it I'm afraid.
Allan
I pm'd you the link
For anyone else dealing with this issue: The following is my solution.
The "format" function is
The main issue is that ajax is asynchronous. Also take note of the dataType:json. Without it, it was treating the results as a string.