我正在使用数据表插件,目前,当单击 td 中的图像时,我的代码会展开/折叠,但我希望能够单击该行来展开,请问有人可以帮忙吗?这是代码:
$(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' );
}
} );
} );
其次,我希望展开和折叠成为一个平滑的动画,有人可以建议我该怎么做吗?
谢谢
如果您的新行代码中有一个 div,您可以执行以下操作。在我的新行中,我有一个带有 insideDetails 类的 div。这就是我所做的:
jquery.dataTables.js第5648行是:
$(nNewRow).insertAfter(nTr);
更改为:
var innerDetails = $(nNewRow).find('div.innerDetails');
innerDetails.css('display', 'none');
$(nNewRow).insertAfter(nTr);
innerDetails.slideDown();
在我看来,这就是它应该开箱即用的方式。
对于您问题的第一部分:
$('#example tbody tr').live('click', function () {...}
对于第二部分,在 jquery.dataTables.js 中找到
this.fnOpen = function( nTr, sHtml, sClass )
,然后在 fnOpen 方法中找到这一行:
$(nNewRow).insertAfter(nTr);
改成这样:
$(nNewRow).css('display','none').insertAfter(nTr).slideDown();
或者无论您选择的动画是什么。
这尚未经过测试,但类似的东西肯定会起作用。
我的解决方案:
JS
$(document).ready(function () {
......
$('.datatable-header-footer').on('draw.dt', function () {
bindRowToggle();
});
$('.datatable-header-footer').on('processing.dt', function () {
bindRowToggle();
});
$('.datatable-header-footer').on('search.dt', function () {
bindRowToggle();
});
bindRowToggle();
});
function bindRowToggle() {
$('tr.parentOrder').click(function () {
$(this).nextAll(':lt(2)').toggle();
});
}
CSS
.parentOrder {
}
.childOrder {
display: none;
}
HTML
<table class="table datatable-header-footer text-nowrap">
...
<tr class="parentOrder"></tr>
<tr class="childOrder"></tr>
<tr class="childOrder"></tr>
//捕获点击表格详情图片打开详情 $('#tblPolingLog tbody').on('click', ".btn.glyphicon", function () {
var nTr = this.parentNode.parentNode;
var aPos = oTable.fnGetPosition(this.parentNode.parentNode);
// cRound = $('#tblPolingLog').dataTable().fnGetData(aPos);
cRound = $('#tblPolingLog').dataTable().fnGetData(this);
if ($(this).hasClass("glyphicon-eye-close")) {
// This row is already open - close it
$(this).removeClass("glyphicon-eye-close").addClass("glyphicon-eye-open");
$(this).attr('title', 'Open record');
oTable.fnClose(nTr);
}
else if ($(this).hasClass("glyphicon-eye-open")) {
//Open this row
DisplayLoadingMessage('on');
$(this).removeClass("glyphicon-eye-open").addClass("glyphicon-eye-close");
$(this).attr('title', 'Close record');
var ID = $(this).attr("rel");
//get partial Page
$.get("Poling/GetPolingLogByIDDetailsPartial", function (htmldata) {
oTable.fnOpen(nTr, htmldata, 'details' + ID);
//populate details
});
$.getJSON("/api/PolingAPI/GetPolingLogByID?ID=" + ID, function (result) {
DisplayLoadingMessage('off');
SetDetailsView(result, "courseDetails", ID, nTr, aPos);
});
}
});