簡體   English   中英

如何在Jquery Datatable Render中獲取行值?

[英]How to get row values in Jquery Datatable Render?

大家好,我被困在必須獲取數據表整行值的東西上,就我獲得ID卻未獲取整行對象

這就是我

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

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

        { 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" />'; }
            title:'Actions',
            'data': 'stu_ID',
            'render': function (data, type, row) {
                debugger;
                var id = $(this).data('stu_ID');

               // console.log(data);
                return '<input id="btnEdit" type="button" class="btn btn-warning" onclick="myfunction(' + data + ')" value="Edit" />  <input id="btnDelete" type="button" class="btn btn-danger" onclick="myfunction(' + data + ')" value="Delete" />';
            }                  
        }               
    ],    
    data: JsonData
});

在我的onclick函數上,當我寫數據時,我得到了id,但是當我嘗試將整行傳遞給我的函數時,它沒有被命中

function myfunction(data) {
    debugger;
    var stid = row.stu_ID;
    var regNo = row



    alert(stu_ID);
}

單擊編輯按鈕時如何傳遞整行值?

您正在明確指定僅在數據對象中傳遞一個值:

'data': 'stu_ID'

因此,如果要將完整的對象傳遞給render函數,則需要刪除此屬性。

將您的代碼更改為:

'data' : null

或只是簡單地刪除此屬性,默認情況下它將通過完整的對象。

title:'Actions',
'render': function (data, type, row) {
            debugger;
            console.log(data); // you should in console  object now
            return '<input id="btnEdit" type="button" class="btn btn-warning" onclick="myfunction(' + data + ')" value="Edit" />  <input id="btnDelete" type="button" class="btn btn-danger" onclick="myfunction(' + data + ')" value="Delete" />';
        }  

現在您可以在功能向下訪問它:

function myfunction(data) {
    debugger;
    var stid = data.stu_ID;
}

您可以在此處詳細了解如何使用render功能:

https://datatables.net/reference/option/columns.render

您可以使用以下方式呈現數據。 我一直在服務器端處理中以以下方式呈現數據:

var table= $('.dtPrimaryBottom').DataTable( {

        "serverSide": true,
        "destroy" :true,
         "lengthMenu": [[6], [7]],
        "ajax": {
            "url": '/reports/getTopPerformerReport',
        },

        "columns": [


            { "data": "stu_ID" },
            { "data": "Registration No", },
            { "data": "Name" },
            { "data": "FathersName" },
            { "data": "Class" },
            { "data": "Section" },
            { "data": "stu_ID",
              "render": function ( data, type, full, meta ) {
                  return "<img src=\"http://test.com/"+data+"\" style=\"max-width:150px;\">";

              }
            },
        ]

});

希望能幫助到你。

暫無
暫無

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

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