Load child rows from external data source in HTML
Load child rows from external data source in HTML
Hi
I have followed the example here: https://datatables.net/examples/api/row_details.html
What i would like to do is when the child rows are loaded, get them from an external source.
Ive searched the web and have confused myself with this.
Any help would be much appreciated.
here is my code:
Pete
```function format ( d ) {
return d.childrow;
}
$(document).ready(function() {
var table = $('#example').DataTable( {
"Dom": "fWlrtip",
"scrollY": "500px",
"scrollCollapse": true,
"paging": false,
"ajax": "../functions/company_assetregister_ajax.php",
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "assetid" },
{ "data": "assetname" },
{ "data": "serialnumber" },
{ "data": "location" },
{ "data": "intlocation" },
{ "data": "pateligable" },
{ "data": "pattestduedate" },
],
"order": [[1, 'asc']]
} );
$('#example tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
row.child.hide();
tr.removeClass('shown');
}
else {
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
} );```
Answers
Ive done this, i can show you. But can you first properly format the code? Use the markdown thats at the borrom of the reply input
Hi, apologies
All you really need to do, is add an ajax request into the
format()
function, or even better, throw it into the else statement of the$('#example tbody').on('click', 'td.details-control'
, then have thesuccess
of the ajax execute therow.child( format(row.data()) ).show();
hi
Thanks for your help, Can you give me an example of that?
Will it be ok with an HTML response?
You want the HTML in the response to be the exact content of the child row?
If so, this should suffice
Just make sure you change the
url: "/url/here/"
to the proper URLThank you so much for that. How do I pass the assetid field along with the ajax request to get the child rows. So it would send the get request as /url/here.php?assetid=000001
I believe this would work..
As you can see, I used the
columns.name
for each column, then in thedata
for the ajax, I referenced theassetid
value from the row.This wasnt tested, but I believe it should work just fine
Hi
Thanks for that, on your secdond example, this doesnt load. it crashes the whole datatable.
The first one works just fine. any ideas?
Pete
Hi
I took some of your code and ammended it.
This works just fine.