I am using datatables plugin and currently the code I have expands / collapse when an image in the td is clicked but I would like to be able to click the row to expand please can anyone help with this? Here is the code:
$(document).ready(function() {
/*
* Insert a 'details' column to the table
*/
var nCloneTh = document.createElement( 'th' );
var nCloneTd = document.createElement( 'td' );
nCloneTd.innerHTML = '<img src="../examples_support/details_open.png">';
nCloneTd.className = "center";
$('#example thead tr').each( function () {
this.insertBefore( nCloneTh, this.childNodes[0] );
} );
$('#example tbody tr').each( function () {
this.insertBefore( nCloneTd.cloneNode( true ), this.childNodes[0] );
} );
/*
* Initialse DataTables, with no sorting on the 'details' column
*/
var oTable = $('#example').dataTable( {
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0 ] }
],
"aaSorting": [[1, 'asc']]
});
/* Add event listener for opening and closing details
* Note that the indicator for showing which row is open is not controlled by DataTables,
* rather it is done here
*/
$('#example tbody td').live('click', function () {
var nTr = this.parentNode.parentNode;
if ( this.src.match('details_close') )
{
/* This row is already open - close it */
this.src = "../examples_support/details_open.png";
oTable.fnClose( nTr );
}
else
{
/* Open this row */
this.src = "../examples_support/details_close.png";
oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
}
} );
} );
Secondly I would like the expand and collapse to be a smooth animation could someone advise me how to do that?
Thanks
My solution:
JS
CSS
HTML
For the first part of your question:
For the second part, find
this.fnOpen = function( nTr, sHtml, sClass )
in jquery.dataTables.js and then find this line inside the fnOpen method:Change it to this:
Or whatever your chosen animation is.
This is untested but something like it will definitely work.
If you have a div within your new row code, you can do the following. In my new rows I have a div with a class of innerDetails. Here's what I did:
Line 5648 of jquery.dataTables.js is:
Change it to:
This is the way it should work out of the box, imo.