dongyongmin5711 2018-10-18 19:15 采纳率: 100%
浏览 57

使用PHP ECHO填充时,DataTables子行无法正确显示

I have looked at all the examples I can find but haven't found an answer to this.

I have a DataTables table which is loaded by PHP echos, after manipulating data from MySQL queries.

I now want to add child rows showing extra data, basically the same format type as the DataTables examples (http://datatables.net/examples/api/row_details.html)

I have modified the table by adding an extra <tr>, and have added the details-control class (Simplified code below)

<tbody>
  <?php 
    while($fetch = $query->fetch_array()) {
      /*
      processing to build 15 variables used as data in main row <td>s
      and 8 variables used in the "hidden" rows
      */
      echo '<tr id = "parent">';
        echo '<td class="details-control"></td>';  /* taken from the DT example */
        echo '<td>'.$MainVariable.'</td>'; 
        /* repeated for all 15 of the main row variables */
      echo '</tr>';
      /* Now this should be the child row */
      echo '<tr id="child">';   
        echo '<td colspan="5">';
           echo 'Text: <b>'.$ChildVariable.'</b><br />';
           /* repeated for 6 of the child row variables */
        echo '</td>';

        echo '<td colspan="5">';
           echo 'Text: <b>'.$ChildVariable.'</b><br />';
           /* repeated for remaining 2 of the child row variables */
        echo '</td>';

        echo '<td colspan="5">';
           /* reserved for possibly more data */
        echo '</td>';
     echo '</tr>'; 
          }  
  ?>
</tbody>

The Javascript I have tried is below

$(document).ready(function() {

// Add event listener for opening and closing details
  $('#attendtbl').on('click', 'td.details-control', function () {
      var tr = $(this).closest('tr');
      var row = table.row(tr);

      if (row.child.isShown()) {
          // This row is already open - close it
          row.child.hide();
          tr.removeClass('shown');
      } else {
          // Open this row
          row.child(format(tr.data('child-value'))).show();
          tr.addClass('shown');
      }
  });
});

This shows all the data correctly, but all the child are shown displayed when the table loads, and the on('click' doesn't work.

Looking at all the DataTables examples I can find all the data seems to be loaded by Javascript/AJAX. which is something I don't really want to do.

I assume there must be a Javasript fault, but this is a new area for me.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大