Data Tables Get iDataRow in mRender

2020-07-30 01:22发布

问题:

I am using DataTables and the TableTools

It is possible get row index in mRender? Something like this:

{
"mData": "someData",
"mRender" :  function ( data, type, full ) {
     **// get iDataRow somehow**
     return '<a href="'+data+'">Download '+ **iDataRow** +'</a>';
   }
}
  • and not alter data for return of iDataRow

suggestions?

回答1:

Allan: This feature (columns.render) is available starting from May 2014, DataTable 1.10 release. "...columns.data, columns.render should be able to do anything fnRender could do".

The evolution chain of the datatable render feature:

  1. fnRender (deprecated)
  2. mRender
  3. columns.render (the latest, most powerful)

Example: columns.render - Use as a function to create a link from the data source.

$('#example').dataTable( {
   "columnDefs": [ {
   "targets": 0,
    "data": "download_link",
    "render": function ( data, type, full, meta ) { //meta.row is what you are looking for
      var ix = meta.row;
      return "Row number is " + ix;
    }
  } ]
} );

Here are two related unanswered questions from datatable forum, but the first contains the references which helped me to find out the answer:

  • How to get row index in mData, June 2013
  • Get row index in mRender, December 2012


回答2:

 "mRender": function (data, type, row) {
     return "<a href='@Url.Action("Delete", "Review")?id=" + row.ReviewId + "'" + " Class='label label-sm label-success deleteLink loader' >Active</a>";
  }                       


回答3:

I do not know if you mean you want to get information of certain rows when you mean iDataRow.

{
"mData": "someData",
"mRender" :  function (data, type, full, row) {
        // * *  get iDataRow somehow**
                return '<a href="' + data + '">Download ' + row[0] + '</a>'; // Row[0] = first column first row data
        }
}

Good luck with the implementation