我使用此代码是为了通过Datatables的fnCreatedRow回调在每个原始行下添加额外的行。原始行已正确更新,但回调后会删除其他行。。。
fnCreatedRow: function(nRow, aData, iDataIndex) {
var $nRow = $(nRow),
$content = $(),
numIndex = 3;
$nRow.find('td').eq(3).html('');
// We add one or multiple sub rows under each row in order to show the accounts details of the client
$.each(aData[3][0], function(e, i) {
if (e === 'profileId') {
return true;
}
$content = $content.add($nRow.clone().find('td').eq(numIndex).html(i).end().end());
++numIndex;
});
$nRow.after($('<tr>').append($content).html());
}
是否有更合适的回调?我认为如果我在整个表创建后做这些事情是可行的,但如果插件做了一个循环。。。为什么不使用它?
我不是唯一一个有这个问题的人:https://datatables.net/forums/discussion/21354/using-fncreatedrow-to-add-detail-rows-working-but-not
我想补充一点,我检索的json包含第四列的数组。正是这个数组,我使用它来构造这些子例程。我认为它不起作用,因为Datatables需要一个数组或与字符串不同的东西。
我建议像本例中那样,考虑用row.child()附加子行:
https://datatables.net/examples/api/row_details.html
然而,这个例子确实有所不同,因为它们是通过点击事件来呈现细节行的——然而,它们的呈现方式可能是解决问题的关键,而不是使用$content.add.