如何将jQuery DataTable单元格的默认内容设置为可点击的按钮?

时间:2019-03-30 16:41:04

标签: javascript jquery object datatables

在我的数据表输出类型是对象的情况下,我希望整个单元格都将变成一个链接。

直到现在,我只能设法将单元格内的数据转换为链接,但仍无法将整个单元格转换为链接。

"render": function (data, type, row, meta) {
          var link = "{{output.content}}";
          var type = typeof data;
          if(type == "object"){
            if(Array.isArray(data)){
              data.forEach(function(obj) {
                if (obj.hasOwnProperty("category")) {
                  data.sort((a,b) => Number(a.category.id) - Number(b.category.id));
                }
              })
              return (data.map(obj => obj.category ? `<div style="border-color:${obj.category.color}" class="circle"></div> ${obj.name}` : ` <a href="`+content+`">This is my link</a>${obj.name}`)).join('<br>');
            }else {
              if(data){
                return data.name;
              } else {
                return '';
              }
            }
          } else {
            if(String(data).indexOf('#') == 0) {
              return '<div style="border-color:'+ data +'" class="circle"></div>';
            } else {
              return data;
            }
          }
        },
        "targets": "_all"

2 个答案:

答案 0 :(得分:1)

Try this. Just replace the button with a link.

 $(document).ready(function() {
   var table = $('#example').DataTable( {
    "ajax": "data/arrays.txt",
    "columnDefs": [ {
        "targets": -1,
        "data": null,
        "defaultContent": "<button>Click!</button>"
    } ]
    } );

     $('#example tbody').on( 'click', 'button', function () {
      var data = table.row( $(this).parents('tr') ).data();
      alert( data[0] +"'s salary is: "+ data[ 5 ] );
    } );
} );


HTML
=============

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
   </table>

答案 1 :(得分:0)

您可以使用columns.className选项将某些类分配给特定的单元格,而不是使这些单元格显示为带有某些CSS的超链接,最后,将点击处理程序附加到该特定的类上,它将重定向到所需的URL:

const srcData = [
  {id: 1, name: 'stackvoerflow', url: 'https://stackoverflow.com'},
  {id: 2, name: 'wikipedia', url: 'https://wikipedia.org'},
  {id: 3, name: 'github', url: 'https://github.com'}
];

const dataTable = $('#mytable').DataTable({
  dom: 't',
  data: srcData,
  columns: [
    {title: 'id', data: 'id'},
    {title: 'name', data: 'name', className: 'link'}
  ]
});

$('#mytable').on('click', '.link', function(){
  window.location.href = dataTable.row($(this).closest('tr')).data().url;
});
.link {cursor: pointer}
<!doctype html>
<html>
<head>
  <script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script type="application/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
</head>
<body>
<table id="mytable"></table>
</body>
</html>