简体   繁体   English

在 jQuery 数据表中添加指向 Ajax 源数据的链接

[英]Add link to Ajax sourced data in jQuery Datatables

I want to add a link/href to the 2nd column (the one with "C" data in it).我想向第二列(其中包含“C”数据的列)添加一个链接/href。 I have tried the columns render function but it only has data of the current column.我尝试了列渲染功能,但它只有当前列的数据。

https://datatables.net/reference/option/columns.render https://datatables.net/reference/option/columns.render

I want to use data from different columns onto anchor tag of column 2我想使用来自不同列的数据到第 2 列的锚标记

this guy here这家伙在这里

https://stackoverflow.com/a/47696609/11575565 https://stackoverflow.com/a/47696609/11575565

explains using rowID which would've solved my problem but this isnt working.解释使用 rowID 可以解决我的问题,但这不起作用。

I tried using $.getJSON and append() over ajax function but it doesnt work as well.我尝试在 ajax 函数上使用 $.getJSON 和 append() 但它不起作用。

    $(document).ready(function() {
  var table = $('#bla').DataTable({
    "ajax" : "blist.json",
    "columns" : [
        { "data" : null,defaultContent: "-" },
        { "data" : "C" },
        { "data" : "B" },
        { "data" : "D" },
        { "data" : null,defaultContent: "-" },
        { "data" : null,defaultContent: "-" },
    ],

[the array in blist.json has data "A","B","C","D","E"] [blist.json 中的数组有数据 "A","B","C","D","E"]

Say, your Link value is in key E then, you would be able to use render as shown below.假设您的 Link 值在键E那么您将能够使用如下所示的渲染。

"columns" : [
  { "data" : null,defaultContent: "-" },
  { "data" : "C", 
    "render": function(data, type, row, meta){
      if(type === 'display'){
          data = '<a href="' + row.E + '">' + data + '</a>';
      }
      return data;
    }
  },
  { "data" : "B" },
  { "data" : "D" },
  { "data" : null,defaultContent: "-" },
  { "data" : null,defaultContent: "-" },
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM