如何在DataTable的单个单元格中创建多个超链接

时间:2019-05-09 20:42:16

标签: javascript jquery ajax datatable

我使用数据表来构造表。 我的目标如下表所示;使用诸如ContractDetails列之类的单元格。

https://i.stack.imgur.com/fp5nC.png

这是我到目前为止所能做的...

javascript:

{"data": "contract_details",
              "render": function(data, type, row){
                    if (row.announced === 'Completed') {
                        return '<a href="./contracts/' + data + '">' + data + '</a>' 
                    }
                    else { 
                        return data;
                    }
                }
              }

Ajax文件(.txt):

 {
      "id": "5",
      "announced": "Completed",
      "division": "Food Dept",
      "title": "Food Depts",
      "title_summary": "",
      "area": "Food Dept",
      "contract_details": "os-05-16-00040.asp; os-05-16-00040.asp"
    }

1 个答案:

答案 0 :(得分:0)

您可以将ajax数据本身格式化为html格式

Ajax

{
    "id": "5",
    "announced": "Completed",
    "division": "Food Dept",
    "title": "Food Depts",
    "title_summary": "",
    "area": "Food Dept",
    "contract_details": "<a href='./contracts/os-05-16-00040.asp'>os-05-16-00040.asp</a> <a href='./contracts/os-05-16-00040.asp'>os-05-16-00040.asp</a>"
}

或者您需要爆炸contract_details的内容

{"data": "contract_details",
              "render": function(data, type, row){
                    if (row.announced === 'Completed') {
                        var contractdetails = row.contract_details;
                        var contractdetailsarray=  contractdetails.split("; ");

                        var ReturnString = "";
                        for(var i=0; i<contractdetailsarray.length; i++){
                            ReturnString += '<a href="./contracts/' + contractdetailsarray[i] + '">' + contractdetailsarray[i]+ '</a> ';
                        }
                        return ReturnString;
                    }
                    else { 
                        return data;
                    }
                }
              }