I followed jQuery Datatable - Sliding child rows example (just look at "Complete code" section on that page) in my ASP.NET MVC project and I could listed master & static details data properly. However, when I want to retrieve details data dynamically via AJAX, I cannot listed them properly due to an error TypeError: table.fnOpen is not a function. There is a solution changing DataTable to dataTable, but in this case I encounter another errors. The problem is exactly on the click & format method and I think I made a mistake for some definition. Could you please have a look at and clarify me about where the mistake is? Thanks in advance...
function format(d) {
return '<div class="slider">' +
'<table style="width:100%">' +
'<tr>' +
'<th>Name</th>' +
'<th>Surname</th> ' +
'<th>Number</th>' +
'</tr>' +
'<tr>' +
'<td>' + d.StudentName + '</td>' +
'<td>' + d.StudentSurname + '</td> ' +
'<td>' + d.StudentNumber + '</td>' +
'</tr>' +
'</table>'
'</div>';
}
$(document).ready(function () {
var table;
table = $('#dtbLabGroup')
.DataTable(
//code omitted for brevity
"columns": [
{
"class": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "name": "Lab" },
{ "name": "Schedule" },
{ "name": "Term" },
{ "name": "Status" }
],
"order": [[1, 'asc']],
});
// Add event listener for opening and closing details
$('#dtbLabGroup tbody').on('click', 'td.details-control', function () {
// !!! There might be a problem regarding to these 3 parameters
var tr = $(this).closest('tr');
var row = table.row(tr);
var nTr = $(this).parents('tr')[0];
//
if (row.child.isShown()) {
// This row is already open - close it
$('div.slider', row.child()).slideUp(function () {
row.child.hide();
tr.removeClass('shown');
});
}
else {
// Open this row
row.child(format(row.data()), 'no-padding').show();
tr.addClass('shown');
$('div.slider', row.child()).slideDown();
// !!! There is PROBABLY a problem
// !!! I added the following code for retrieving data via AJAX call.
var id = 8; //used static id for testing
$.get("GetStudents?id=" + id, function (students) {
table.fnOpen(nTr, students, 'details');
});
}
});
});
Update I: Here is the modified format() method below:
function format(d) {
var htmlResult = '<div class="slider">' +
'<table style="width:100%">' +
'<tr>' +
'<th>Name</th>' +
'<th>Surname</th> ' +
'<th>Number</th>' +
'</tr>';
$.each(d, function (i, d) {
htmlResult += '<tr><td>' + d[i].StudentName + '</td><td>' + d[i].StudentSurname + '</td><td>' + d[i].StudentNumber + '</td></tr>';
});
htmlResult += '</table>' +
'</div>';
return htmlResult;
}