2013-04-10 82 views
4

使用jQuery DataTables插件,我使用mRender將按鈕添加到動態添加的行中。這部分工作很好,但我怎樣才能獲得按鈕添加到當前行的rowID?我需要這個來爲按鈕創建唯一的ID。JQuery DataTables mRender - 如何獲取行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); 
    }); 

回答

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函數中獲取當前行索引?那麼如何做到這一點?

+0

+1自答案是良好的SO – msw 2013-04-24 12:01:50

+0

啊。謝謝@msw! – elizabethmeyer 2013-04-24 15:17:39

2

This page正好有你所需要的,如果你正在使用服務器端DT_RowId

代碼:

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