简体   繁体   中英

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

Guys please help I'm stuck in this, I'm trying to add an 'Edit' button in jquery datatable and pass the row id everytime user clicks it, thing is that that each time when I click it says 'undefined' and I've tried all methods but still i'm missing something !

This is my code:

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

}

Your data will be present directly in 'data' parameter of that function.

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

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

you can use it to get your required ID.

If in case you need another property from that object, you can use row. It gives the entire row object

try Following Code:

'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" />';
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM