I really need your help in aligning the child row columns along with parent row columns. I found an example exactly matches what I did, So I am not putting the entire code here as there is a restriction in my company. So I am giving the reference here where the code/example is available. Example: JQuery Datatable with parent child relationship in same dataset. How to display it as parent child rows in the table?
I have attached the screenshot of the alignment. Can anyone please help.
Note: I tried putting some CSS html table cellspacing/cellpadding, but it didn't workout.
var g_dataFull = [];
/* Formatting function for row details - modify as you need */
function format ( d ) {
var html = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;" width="100%">';
var dataChild = [];
var hasChildren = false;
$.each(g_dataFull, function(){
if(this.parent === d.name){
html +=
'<tr><td>' + $('<div>').text(this.name).html() + '</td>' +
'<td>' + $('<div>').text(this.position).html() + '</td>' +
'<td>' + $('<div>').text(this.office).html() +'</td>' +
'<td>' + $('<div>').text(this.salary).html() + '</td></tr>';
hasChildren = true;
}
});
if(!hasChildren){
html += '<tr><td>There are no items in this view.</td></tr>';
}
html += '</table>';
return html;
}
$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": {
"url": "https://api.myjson.com/bins/3mbye",
"dataSrc": function(d){
g_dataFull = d.data;
var dataParent = []
$.each(d.data, function(){
if(this.parent === "null"){
dataParent.push(this);
}
});
return dataParent;
}
},
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "salary" }
],
"order": [[1, 'asc']]
} );
// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('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');
}
} );
} );
td.details-control {
background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.shown td.details-control {
background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_close.png') no-repeat center center;
}
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<table id="example" class="display">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
If someone is still looking for answer then you need to add the $
to the child element HTML. Below is the example showing the same.
function format ( d ) {
return $('<tr>'+
'<td></td>'+
'<td>'+d.name+'</td>'+
'<td>'+d.position+'</td>'+
'<td>'+d.office+'</td>'+
'<td>'+d.extn+'</td>'+
'<td>'+d.start_date+'</td>'+
'<td>'+d.salary+'</td>'+
'</tr>');
}
The $
after the return statement does the trick. For more reading you can visit the this thread from datatable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With