Javascript dataTable - 添加表格链接

时间:2016-11-25 10:07:58

标签: javascript datatable

我正在使用javascript dataTable,我通过api链接获取数据来填充表格:

jQuery.get(api_url_here", function(dataSet){

    jQuery('#myTable').DataTable( {
        data: dataSet,
        columns: [
            { "data": "id", "title": "theId" },
            { "data": "name", "title": "theName" }
        ]
    });


});

<table id="myTable" class="display"></table>

这一切都按要求工作但我需要其中一列创建一个链接,这样当用户点击id时,它将转到分配了id的URL ...

例如:<a href="someurl+{id}">theId</a>

如何使用dataTable执行此操作?

3 个答案:

答案 0 :(得分:1)

"ajax": "./pasien/look/",
aoColumns: [
  { "mData": null }
],
columnDefs: [{
  "targets": 0,
  "data": null,
  "mRender": function (data, type, row) {
  return '<a href="#">'+ row.mr +'</a>';
 }
}]

试试这个,我在数据表中使用ajax作为数据。

答案 1 :(得分:0)

尝试这样:

            {
                "data": "id",
                "title": "theId",
                "aTargets": [0],
                "sType": "numeric"
            },
            {
                "className":        '',
                "orderable":        false,
                "data":             null,
                "defaultContent":   '<a href="your link"</a>'
            }];

答案 2 :(得分:0)

jQuery有一个在创建单元格时触发的事件,然后你可以编写正常的javascript来设置它的id为内容:

jQuery.get("api_url_here", function(dataSet){
    jQuery('#myTable').DataTable( {
        data: dataSet,
        columns: [
            { "data": "id", "title": "theId",
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html("<a class='display' href='" + oData.id + "'>" + oData.id + "</a>");
                }
            },
            { "data": "name", "title": "theName" }
        ]
    });
});

我希望它可以帮到你。

我从https://www.datatables.net/forums/discussion/25111/hyperlink-in-td得到答案。