How to get an hidden column value in mRender() function from ajax data source in jQuery datatable

不想你离开。 提交于 2019-12-06 11:04:12

First, I would try using aoColumnDefs rather than aoColumns.
As per the datatables documentation:

http://datatables.net/usage/columns

aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7). This allows great flexibility when creating tables, as the aoColumnDefs arrays can be of any length, targeting the columns you specifically want.

Next, I cannot quite tell how you intend to use the Id in the Edit and Delete links, but here the Id is appended to the url:

  "aoColumnDefs": [
        { "mData": "Code ", "aTargets": [ 0 ] },
        { "mData": "Name", "aTargets": [ 1 ] },
        { "mData": "Template", "aTargets": [ 2 ] },
        { "mData": "SPViews", "aTargets": [ 3 ] },               
        { "mData": "Id", "aTargets": [ 4 ], 
            "mRender": function ( data, type, full ) {
                return '<a class="glyphicon glyphicon-pencil" href="/Country/Edit/' + data + '">Edit</a><a class="glyphicon glyphicon-remove" href="/Country/Delete/' + data + '">Delete</a>';
            }
        },
        { "bSortable": false, "aTargets": [ 4 ] }
    ],

...and here the Id appears as a data- attribute whose value you could access, for instance, with jquery:

  "aoColumnDefs": [
        { "mData": "Code ", "aTargets": [ 0 ] },
        { "mData": "Name", "aTargets": [ 1 ] },
        { "mData": "Template", "aTargets": [ 2 ] },
        { "mData": "SPViews", "aTargets": [ 3 ] },               
        { "mData": "Id", "aTargets": [ 4 ], 
            "mRender": function ( data, type, full ) {
                return '<a class="glyphicon glyphicon-pencil" data-countryid="' + data +'" href="/Country/Edit/">Edit</a><a class="glyphicon glyphicon-remove" data-countryid="' + data +'" href="/Country/Delete/">Delete</a>';
            }
        },
        { "bSortable": false, "aTargets": [ 4 ] }
    ],  

@mg1075 thank you for your reply. fnRender function seems to be deprecated. I did not try your solution, but I fixed it another way using mRender function. So here is my solution:

      countryTable = $("#countryListTable").dataTable({
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "/Country/GetAll",
            "aoColumns": [
                 { "bVisible": false },
                  null,
                  null,
                  null,
                  null,
                  {
                      mData: 0,//The Id column
                      "bSearchable": false,
                      "bSortable": false,
                      mRender: function (data, type, row) {

                          return '<a class="glyphicon glyphicon-pencil" onclick="editCountry(\'/Country/Edit/' + data + '\');return false;">Edit</a><a class="glyphicon glyphicon-remove" onclick="deleteCountry(\'/Country/Delete/' + data + '\');return false;">Delete</a>';
                      }
                  }],

        });

I think the both approach should be perfect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!