我的答案与 @Chris Everitt 的类似,略有不同。只是为了那些想看到它的人..来吧。
var oTable = $('#masterTable').dataTable( {
"aLengthMenu": [[5,10, 25, 50, 100 , -1], [5,10, 25, 50, 100, "All"]],
"iDisplayLength" : 10,
"aoColumnDefs": [
{"sWidth": "25%", "aTargets": [ 0 ] },
{"sWidth": "10%", "aTargets": [ 1 ] },
{"sWidth": "10%", "aTargets": [ 2 ] },
{"sWidth": "10%", "aTargets": [ 3 ] },
{"sWidth": "10%", "aTargets": [ 4 ] },
{"sWidth": "10%", "aTargets": [ 5 ] },
{"sWidth": "15%", "aTargets": [ 6 ] },
{"sClass": "align-left" , "aTargets": [ 0,1,4, 2,3,5,6] }
],
"aoColumns": [
{ "bSortable": true },
null, null, null,null, null,
{ "bSortable": true }
]
});
在表格中注册所有img(dom属性)的事件 -
oTable.$('td').each( function () {
$(this).on('click','img', function () {
var nTr = $(this).parents('tr')[0];
if ( oTable.fnIsOpen(nTr) )
{
this.src = "${pageContext.request.contextPath}/theme/v_1_0/app-images/details_open.png";
oTable.fnClose( nTr );
}
else
{
this.src = "${pageContext.request.contextPath}/theme/v_1_0/app-images/details_close.png";
var html = '<div> Placeholder here.. </div>';
oTable.fnOpen(nTr, html, 'details');
}
} );
ajaxComplete
中绑定将绑定到所有行,这是无效的,因为DataTables很聪明,直到需要才在浏览器中呈现所有行。因此,Kon的live()
答案是正确的。 - Matt Mitchell