JQuery DataTables mRender - 如何获取行ID?

时间:2013-04-10 07:37:11

标签: jquery jquery-datatables

使用JQuery DataTables插件,我使用mRender为动态添加的行添加按钮。这部分工作得很好,但是如何获取按钮添加到当前行的rowID?我需要这个来为按钮创建唯一的ID。

我需要用什么代替????在下面的代码中?

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);
    });

2 个答案:

答案 0 :(得分:3)

好的,我找到了一个解决方法。虽然这不是完美的解决方案,但确实有效。我现在将网格中的总行数传递给mRender函数作为rowID。

$('#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);
});

我仍然想知道:是否有可能从mRender函数中获取当前行索引?怎么会这样做?

答案 1 :(得分:3)

如果您使用This page服务器端

DT_RowId正是您所需要的

代码:

"mRender": function ( data, type, full ) {
                                    return '<a href="?action=student_report&studentUID='+ full.DT_RowId + '">' + data + '</a>';
相关问题