How to display a hyperlink in a cell with jQuery D

2020-02-11 02:58发布

I have some Ajax data read into jQuery DataTables. Problem is I need to make the data in the first column into a hyperlink. As in <td><a href = "5555.html">5555</a></td>.

My JSON data:

{
   "data": [
      ["5555","07/17/2010","RCC Saturday Open","E10","Harris, Fred","1900","Nikolayev, Igor (FM)","2367","1-0"],
      ["5554","07/17/2010","RCC Saturday Open","B01","Nikolayev, Igor (FM)","2367","Motroni, Richard","1728","1-0"]
   ]
}

JavaScript:

$(document).ready(function() {
   $('#cccr').DataTable( {
      "render": function ( data, type, row ) {
         return '<a href="basic.php?game=' + data + '></a>'; //doesn't work
      },
      "ajax": 'games.json',
      "deferRender": true
   } );
} );

I'm not too knowledgeable about JavaScript. I was unable to figure it out after hours of googling the datatables.net website.

Can anyone please help ?

标签: datatables
2条回答
Summer. ? 凉城
2楼-- · 2020-02-11 03:05

CAUSE

Option render should be sub-property of either columns or columnDefs.

SOLUTION

Use columnDefs.render option to display hyperlink in a cell dynamically.

For example:

var table = $('#cccr').DataTable({
    /* ... skipepd other options ... */
    columnDefs: [
        {
            targets: 0,
            render: function ( data, type, row, meta ) {
                if(type === 'display'){
                    data = '<a href="basic.php?game=' + encodeURIComponent(data) + '">' + data + '</a>';
                }

                return data;
            }
        }
    ]      
});

DEMO

See this jsFiddle for code and demonstration.

查看更多
【Aperson】
3楼-- · 2020-02-11 03:19

configure the column definitions via render option:

"render": function ( data, type, row ) {
  return '<a href="#">' + data + '</a>';
}
查看更多
登录 后发表回答