JQuery DataTables mRender - 如何获取行 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15919723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery DataTables mRender - how do I get the row ID?
提问by elizabethmeyer
With the JQuery DataTables plugin I am using mRender to add buttons to my dynamically added rows. This part works great, but how can I get the rowID of the current row the buttons are added to? I need this to create unique IDs for the buttons.
使用 JQuery DataTables 插件,我使用 mRender 将按钮添加到我动态添加的行。这部分效果很好,但是如何获取按钮添加到的当前行的 rowID?我需要这个来为按钮创建唯一的 ID。
What do I need to use in place of ???? in the code below?
我需要用什么来代替???在下面的代码中?
JavaScript:
JavaScript:
$('#example').dataTable({
"aoColumns": [
{ "sTitle": "Person", "mData": "Person" },
{
"sTitle": "Buttons", "mData": "Buttons", "mRender": function () {
var rowID = ?????;
btnD = '<button id="btnDepth' + rowID + '" data-keyindex="' + rowID + '" data-type="Depth" data-action="Show" class="addDepthGraph" title="Show Depth">D</button>';
btnG = '<button id="btnGraph' + rowID + '" data-keyindex="' + rowID + '" data-type="Graph" data-action="Show" class="addDepthGraph" title="Show Graph">G</button>';
var returnButton = btnD + btnG;
return returnButton;
}
}
],
"bPaginate": false
});
$("#addRowOptions").click(function () {
rowindex = $('#example').dataTable().fnGetData().length;
obj = [{Person:'PersonA', Buttons: ''}];
$('#example').dataTable().fnAddData(obj);
});
采纳答案by elizabethmeyer
Okay, I've found a work around. Although this is not the perfect solution, it does work. I am now passing the total number of rows in the grid through to the mRender function as the rowID.
好的,我找到了解决办法。虽然这不是完美的解决方案,但它确实有效。我现在将网格中的总行数作为 rowID 传递给 mRender 函数。
$('#example').dataTable({
"aoColumns": [
{ "sTitle": "Person", "mData": "Person" },
{
"sTitle": "Buttons", "mData": "Buttons", "mRender": function (rowIndex) {
alert(rowindex);
btnD = '<button id="btnDepth' + rowindex + '" data-keyindex="' + rowindex + '" data-type="Depth" data-action="Show" class="addDepthGraph" title="Show Depth">D</button>';
btnG = '<button id="btnGraph' + rowindex + '" data-keyindex="' + rowindex + '" data-type="Graph" data-action="Show" class="addDepthGraph" title="Show Graph">G</button>';
var returnButton = btnD + btnG;
return returnButton;
}
}
],
"bPaginate": false
});
$("#addRowOptions").click(function () {
rowindex = $('#example').dataTable().fnGetData().length;
obj = [{Person:'PersonA', Buttons: rowindex}];
$('#example').dataTable().fnAddData(obj);
});
I would still like to know: Is it possible to get the current row index from within the mRender function? And how would one do that?
我仍然想知道:是否可以从 mRender 函数中获取当前行索引?怎么做呢?