Nested tables
Nested tables
Is it possible to have another data table within a data table? In my mind I am trying to display a file/folder tree with folders being clicked on and providing a nested table with the folder contents.
I have seen jquery file manager examples that do this but was wondering if data tables can be tweaked to achieve something similar?
Regards,
Euan
I have seen jquery file manager examples that do this but was wondering if data tables can be tweaked to achieve something similar?
Regards,
Euan
This discussion has been closed.
Replies
Yes, DataTables can nest tables, however, they are entirely independent from either other. So if you apply sorting to the outer table, it doesn't apply to the inner table etc - I'm sure that could be done using the API if you are looking for that, but the short answer is yes, nested tables should work fine in DataTables.
Allan
Allan
I have tried doing it inside the live event of the root table right after the fnOpen but it does not work.
I'm adding a table inside a row as suggested but I can not interact with the inner table:
If I put my cursor over the row containing the new table, the whole row is grayed out and I can not interact with the table inside...
Any suggestion to be able to interact with the inner table?
Thanks,
Nicolas
...
oTable.fnOpen( nTr, fnFormatDetails(oTable, data), 'detail' )
...
function fnFormatDetails ( oTable, data )
{
var numberOfResults = data.StudyInstanceUID.length;
var i = 0;
var content = 'UIDNb';
for(i = 0; i < numberOfResults; ++i)
{
content += '';
content += ''+data.SeriesInstanceUID[i][0]+'';
content += ''+data.NumberOfSeriesRelatedInstances[i][0]+'';
content += '';
}
content += '';
$('#'+data.StudyInstanceUID[0]+'').dataTable();
return content;
}
}
It uses an AJAX data source and returns JSON that I parse into the new inner-table. This inner table has a button on it too to allow the inner record to be deleted, also using AJAX. I use the jQuery .css() function to sync all of the columns to the header widths.
I'm using 6 of the same type of "row details" tables on one page. 5 of the tables have 5 columns and one has 4 columns of data. For the 5 tables with 5 columns, I was able to use some dynamic Javascript to format and handle creating the inner tables using a "parameter" of a sort to cut down on the amount of code I had to write. Of coruse for people with 5 different sized tables, this is not possible, but for me at least, it worked.
Give me a couple of hours and I'll post it up.
JSFiddle: http://jsfiddle.net/86nBA/5/
It could use more work of course, but hopefully this helps get the basic idea down. I left some old code in there so you could see how I make a request at least in a Java platform.
http://stackoverflow.com/questions/11841486/datatables-drill-down-rows-with-nested-independent-table/12409660#12409660
UPDATE. I have this working. Below is the code that works. Note the .parentNode usage. I am not sure why in one case the I can get the row data and in the fnOpen() I need to go up to the parent of the row.
[code]
$("#tblAssays").on("click", "tbody td img", function () {
var thisTR = this.parentNode.parentNode;
if (this.src.match(openImagePath)) {
//alert('show the child data');
// change icon to hide child data
this.src = closeImagePath;
// create the child order Detail table
var rowIndex = oAssayTable.fnGetPosition(thisTR)[0];
var aData = oAssayTable.fnGetData(rowIndex);
var orderDetailTableID = "OrderTable_" + aData.ID;
// Remove any older occurance of the table
$(orderDetailTableID).remove();
// Create and inject html for new occurance table
var orderDetailTableHTML = 'Order Details here';
var openedTR = oAssayTable.fnOpen(thisTR.parentNode, orderDetailTableHTML, 'OrderDetails');
// Initialize the table as a datatable
var oOrderDetailTable = $("#" + orderDetailTableID).dataTable(dtChildOrderDefault);
// load data into the datatable
oOrderDetailTable.fnAddData(aData.AssayGroupOrders);
}
else {
// alert('hide the child data');
// change icon to show child data
this.src = openImagePath;
oAssayTable.fnClose(thisTR.parentNode);
}
});
[/code]