将列数据设置为超链接(dataTable JQUERY)

2024-10-03 00:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用datatable将列作为超链接,但没有成功

函数successCallback(responseObj){

  $(document).ready(function() {
         $('#example').dataTable( {
        "data":responseObj ,
        "bDestroy": true,
        "deferRender": true ,
        "columns": [
                    { "data": "infomation" },
                    { "data": "weblink" },
                ]
  } );

  } );

}

我需要weblink来显示链接并成为该列中的超链接,以便用户可以单击并重定向到其他页面。我查看了render,但由于链接上的信息较少,我无法成功地做到这一点

我也研究了这个{a2},但没有什么帮助


Tags: 函数truedata链接examplefunctiondocumentready
3条回答

使用^{}API方法动态生成单元格的内容

$('#example').dataTable({
   "data": responseObj,
   "columns": [
      { "data": "information" }, 
      { 
         "data": "weblink",
         "render": function(data, type, row, meta){
            if(type === 'display'){
                data = '<a href="' + data + '">' + data + '</a>';
            }

            return data;
         }
      } 
   ]
});

有关代码和演示,请参见this example

如果您希望基于其他列数据添加链接,则可以使用以下方法

$('#example').dataTable({
   "data": responseObj,
   "columns": [
      { "data": "information" }, 
      { 
         "data": "weblink",
         "render": function(data, type, row, meta){
            if(type === 'display'){
                data = '<a href="' + row.myid + '">' + data + '</a>';
            }
            return data;
         }
      } 
   ]
});

我刚刚更改了渲染函数data仅指当前列数据,而row对象指整行数据。因此,我们可以使用它来获取该行的任何其他数据

    $('#example').dataTable( {
  "columnDefs": [ {
    "targets": 0,
    "data": "download_link",
    "render": function ( data, type, full, meta ) {
      return '<a href="'+data+'">Download</a>';
    }
  } ]
} );

documentation开始。这对我来说非常清楚和直截了当,你不明白的具体是什么?你看到了什么错误

有关更完整的示例,请参见here

相关问题 更多 >