簡體   English   中英

通過列渲染將jquery數據表的行ID傳遞給另一個函數

[英]Pass row ID of jquery datatable through column render to another function

伙計們,請幫我解決這個問題,我試圖在jquery數據表中添加一個“編輯”按鈕,並在每次用戶單擊時傳遞行ID,事實是,每次單擊它時,它都說“未定義”,已經嘗試了所有方法,但仍然缺少某些功能!

這是我的代碼:

function LoadDtPrimaryBottom(JsonData) {

debugger;

var table = $('.dtPrimaryBottom').DataTable({
    // dom: "Bfrtip",
    "lengthMenu": [[6], [7]],
    paging: true,

    columns:[
       { title: 'Student ID', data: 'stu_ID'},

        { title: 'Registration No', data: 'Registration No' , 'searchable':true},
        { title: 'Name', data: 'Name' },
        { title: 'FathersName', data: 'FathersName' },
        { title: 'Class', data: 'Class' },
        { title: 'Section', data: 'Section' },
        {
            //"title": "Actions",
            //"mdata": null,
            //"render": function (data, type, row) {
            //    return '<button class="btnID">Edit</button>';

            //"mData": null,
            //"bSortable": false,
            //"mRender": function (stu_ID) { return '<input id="btnDispose" type="button" onclick="myfunction(' + stu_ID +')" value="Edit" />'; }

            'data': 'stu_ID',
            'render': function (data, type, row) {
                var id = $(this).data('stu_ID');
                return '<input id="btnEdit" type="button" onclick="myfunction(' + id + ')" value="Edit" />';
            }                  
        }               
    ],    
    data: JsonData
});


function myfunction(stu_ID){
debugger;
alert(stu_ID);

}

您的數據將直接顯示在該函數的“數據”參數中。

        'render': function (data, type, row) {
          console.log(data)

            return '<input id="btnEdit" type="button" onclick="myfunction(' + data + ')" value="Edit" />';
        } 

您可以使用它來獲取所需的ID。

如果需要該對象的其他屬性,則可以使用row。 它給出了整個行對象

嘗試以下代碼:

'render': function (data, type, row) {
 var id =data;    // You can also write this line like var id =row[0];
 return '<input id="btnEdit" type="button" onclick="myfunction(' + id + ')" value="Edit" />';
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM