简体   繁体   English

将 href 超链接添加到数据表中的行或字段

[英]Add href hyperlink to row or field in datatable

I've seen a lot of questions about this, however I cannot seem to get it working.我已经看到很多关于这个的问题,但是我似乎无法让它工作。

I have a datatable but I cannot get it to work.我有一个数据表,但我无法让它工作。 I use python-flask with bootstrap and I change a pandas dataframe to a html table with to_html().我将 python-flask 与 bootstrap 一起使用,并使用 to_html() 将 Pandas 数据帧更改为 html 表。

<table width="100%" class="table table-striped table-bordered table-hover dataTable" id="dataTables-example"><thead>
<tr style="text-align: right;">
  <th>id</th>
  <th>user</th>
  <th>status</th>
</tr>
</thead>
<tbody>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>1</td>
</tr>
<tr>
  <td>2</td>
  <td>1</td>
  <td>1</td>
</tr>
</tbody>
</table>

and at the bottom of the body I have:在身体的底部,我有:

<script>
$(document).ready(function() {
$('#dataTables-example').DataTable({

        "bDestroy": true,
       "deferRender": true,
       "columns": [
          { "data": "id" }, 
          { 
             "data": "weblink",
             "render" : function(data, type, row, meta){
                if(type === 'display'){
                   return $('<a>')
                      .attr('href', data)
                      .text(data)
                      .wrap('<div></div>')
                      .parent()
                      .html();

                } else {
                   return data;
                }
             }
          } 
       ]
    });
});
</script>

I've looked at a lot of awnsers however they all contain the data as json in the javascript while my data is already in the html.我查看了很多 awnsers,但是它们都包含 javascript 中的 json 数据,而我的数据已经在 html 中。

Use columnDefs when you have a DOM <table> and only need to target one or few columns :当您有一个 DOM <table>并且只需要定位一列或几列时,请使用columnDefs

$('#dataTables-example').DataTable({
  destroy: true,
  deferRender: true,
  columnDefs: [{ 
    targets: 0, //<-- index of column that should be rendered as link
    render : function(data, type, row, meta){
      if (type === 'display'){
         return $('<a>')
           .attr('href', data)
           .text(data)
           .wrap('<div></div>')
           .parent()
           .html();
      } else {
         return data;
      }
     }
  }] 
})

It works here -> http://jsfiddle.net/j9ez0sbj/它在这里工作-> http://jsfiddle.net/j9ez0sbj/

You have 3 columns in your html table but only define 2 columns in your initialization.您的 html 表中有 3 列,但在初始化时只定义了 2 列。

From datatables documentation for the columns initialization option :列初始化选项的数据文档中:

Note that if you use columns to define your columns, you must have an entry in the array for every single column that you have in your table (these can be null if you don't wish to specify any options).请注意,如果您使用列来定义您的列,则您必须在数组中为表中的每一列都有一个条目(如果您不想指定任何选项,这些条目可以为空)。

Depending on what your intent is, at the very least you need to add a definition for the third column, so something like this:根据您的意图,至少您需要为第三列添加一个定义,如下所示:

"columns": [
      { "data": "id" }, 
      { 
         "data": "weblink",
         "render" : function(data, type, row, meta){
            if(type === 'display'){
               return $('<a>')
                  .attr('href', data)
                  .text(data)
                  .wrap('<div></div>')
                  .parent()
                  .html();

            } else {
               return data;
            }             
         }
      },
      { "data": "status" } // Third column definition added 
   ]

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

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