简体   繁体   English

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

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

Hello guys I am stuck on something I have to get the values of the whole row of data table, as far I am getting id but not getting whole row object 大家好,我被困在必须获取数据表整行值的东西上,就我获得ID却未获取整行对象

this is where I am 这就是我

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

on my onclick function when I write data I get the id but when I try to pass the whole row to my function it does not get hit 在我的onclick函数上,当我写数据时,我得到了id,但是当我尝试将整行传递给我的函数时,它没有被命中

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



    alert(stu_ID);
}

how to pass whole row values when clicking on edit button? 单击编辑按钮时如何传递整行值?

You are specifying explicitly to pass only one value in data object : 您正在明确指定仅在数据对象中传递一个值:

'data': 'stu_ID'

So, you need to remove this property if you want to pass complete object to render function. 因此,如果要将完整的对象传递给render函数,则需要删除此属性。

Change your code to : 将您的代码更改为:

'data' : null

or just simply remove this property, as default it would pass complete object then. 或只是简单地删除此属性,默认情况下它将通过完整的对象。

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

Now you can access it in function down: 现在您可以在功能向下访问它:

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

You can read more in detail about how to use render function here: 您可以在此处详细了解如何使用render功能:

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

You can use the following way to render data. 您可以使用以下方式呈现数据。 I have always rendered data in serverside processing the following way : 我一直在服务器端处理中以以下方式呈现数据:

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;\">";

              }
            },
        ]

});

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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