【问题标题】:DataTables with json data making TR row clickable带有 json 数据的 DataTables 使 TR 行可点击
【发布时间】:2017-02-14 11:31:52
【问题描述】:

我正在使用:jquery.dataTables.js 来自:https://datatables.net

我试图让每个 tr 都有一个链接,但由于某种原因这不起作用,我尝试在我的控制台上运行 chrome 并且工作正常,有人可以解释我为什么不能在我的元素中插入这个链接?

它与json数据有关吗?

html:

<div class=" dashboard">
  <div class="col-md-8 no-padding">
    <div class="form-group col-md-4 no-padding">
      <select class="form-control" id="sel1">
        <option value="Filter by">Filter by country </option>
        <option value="All">All</option>
        <option value="China">China</option>
        <option value="EUA">EUA</option>
        <option value="Spain">Spain</option>
      </select>
    </div>
  </div>

  <br>
  <br>
  <table id="example" class="display" width="100%" cellspacing="0">
    <thead>
      <tr>
        <th>First name</th>
        <th>Place</th>

      </tr>
    </thead>
  </table>

jquery:

$(document).ready(function() {
  var dt = $('#example').dataTable();
  dt.fnDestroy();
});

$(document).ready(function() {
  var url = 'http://www.json-generator.com/api/json/get/crcCiZXZfm?indent=2';
  var table = $('#example').DataTable({
    ajax: url,
    columns: [{
      data: 'name'
    }, {
      data: 'place'
    }]
  });

  $('#sel1').change(function() {
    if (this.value === "All") {
      table
        .columns(1)
        .search('')
        .draw();
    } else {
      table
        .columns(1)
        .search(this.value)
        .draw();
    }
  });
});

$(document).ready(function() {
  $('#example tbody tr').attr('onclick', "document.location = 'edit.html'");
});

jquery 我要插入:

$('#example tbody tr').attr('onclick', "document.location = 'edit.html'");

jsfiddle 但不是上面的 jquery: http://jsfiddle.net/f7debwj2/

【问题讨论】:

    标签: javascript jquery json datatables


    【解决方案1】:

    使用多个 $(document).ready() 函数不是一个好的选择。数据表创建后可以使用回调函数做一些功能。

    updated fiddle is: http://jsfiddle.net/dssoft32/f7debwj2/4/
    

    【讨论】:

      【解决方案2】:

      当您将列传递到调用以实例化表时,请使用列上的render 属性。这是the documentation 的链接和下面的示例:

      $(document).ready(function() {
        var dt = $('#example').dataTable();
        dt.fnDestroy();
      });
      
      $(document).ready(function() {
        var url = 'http://www.json-generator.com/api/json/get/crcCiZXZfm?indent=2';
        var table = $('#example').DataTable({
          ajax: url,
          columns: [{
            data: 'name',
            "render": function(data, type, full, meta) {
              return '<a href="http://stackoverflow.com">' + data + '</a>';
            }
          }, {
            data: 'place',
            "render": function(data, type, full, meta) {
              return '<a href="http://www.bbc.com/sport/football">' + data + '</a>';
            }
          }]
        });
      
        $('#sel1').change(function() {
          if (this.value === "All") {
            table
              .columns(1)
              .search('')
              .draw();
          } else {
            table
              .columns(1)
              .search(this.value)
              .draw();
          }
        });
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      
      <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-2.2.4/dt-1.10.13/datatables.min.css" />
      
      <script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-2.2.4/dt-1.10.13/datatables.min.js"></script>
      
      <div class=" dashboard">
        <div class="col-md-8 no-padding">
          <div class="form-group col-md-4 no-padding">
            <select class="form-control" id="sel1">
              <option value="Filter by">Filter by country</option>
              <option value="All">All</option>
              <option value="China">China</option>
              <option value="EUA">EUA</option>
              <option value="Spain">Spain</option>
            </select>
          </div>
        </div>
      
        <br>
        <br>
        <table id="example" class="display" width="100%" cellspacing="0">
          <thead>
            <tr>
              <th>First name</th>
              <th>Place</th>
      
            </tr>
          </thead>
        </table>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-30
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 2019-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多