【问题标题】:Pass row ID of jquery datatable through column render to another function通过列渲染将jquery数据表的行ID传递给另一个函数
【发布时间】:2018-08-05 13:28:54
【问题描述】:

伙计们请帮助我陷入困境,我正在尝试在 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);

}

【问题讨论】:

    标签: javascript c# jquery ajax datatable


    【解决方案1】:

    您的数据将直接出现在该函数的“数据”参数中。

            'render': function (data, type, row) {
              console.log(data)
    
                return '<input id="btnEdit" type="button" onclick="myfunction(' + data + ')" value="Edit" />';
            } 
    

    您可以使用它来获取所需的 ID。

    如果您需要该对象的另一个属性,您可以使用 row.它给出了整个行对象

    【讨论】:

      【解决方案2】:

      尝试以下代码:

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

      【讨论】:

        猜你喜欢
        • 2016-03-09
        • 2012-02-19
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2022-06-22
        • 2017-08-08
        • 1970-01-01
        相关资源
        最近更新 更多