【问题标题】:Expand/Collapse all with Nested Table - jQuery Datatables使用嵌套表展开/折叠全部 - jQuery Datatables
【发布时间】:2020-06-25 12:45:18
【问题描述】:

我正在使用数据表插件。我想问一下,有没有办法展开/折叠嵌套表的所有行。我试图在下面实现这一点,但它不起作用。我想像这个例子一样展开/折叠行 https://www.gyrocode.com/articles/jquery-datatables-how-to-expand-collapse-all-child-rows/#regular。 请帮忙谢谢

  function fnFormatDetails(table_id, html) {

      var sOut = "<table id=\"exampleTable_" + table_id + "\">";

      sOut += html;
      sOut += "</table>";
      return sOut;
  }
  var iTableCounter = 1;
  var oTable;
  var oInnerTable;
  var TableHtml;

  //Run On HTML Build
  $(document).ready(function () {

      TableHtml = $('#exampleTable_2').html();

      //Insert a 'details' column to the table
      var nCloneTh = document.createElement('th');
      var nCloneTd = document.createElement('td');

      nCloneTd.innerHTML = '<img src="http://i.imgur.com/SD7Dz.png">';
      nCloneTd.className = "center";
      $('#exampleTable thead tr').each(function () {
          this.insertBefore(nCloneTh, this.childNodes[0]);
      });

      $('#exampleTable tbody tr').each(function () {
          this.insertBefore(nCloneTd.cloneNode(true), this.childNodes[0]);
      });

      //Initialse DataTables, with no sorting on the 'details' column
      var oTable = $('#exampleTable').dataTable({
              'bJQueryUI': true,

              'sPaginationType': 'full_numbers',
              'aoColumnDefs': [{
                      'bSortable': false,
                      'class': 'details-control',
                      'aTargets': [0]
                  }
              ],
              'aaSorting': [[1, 'asc']]
          });

      /* Add event listener for opening and closing details
       * Note that the indicator for showing which row is open is not controlled by DataTables,
       * rather it is done here
       */
      $('#exampleTable tbody tr img').on('click', function () {
          var nTr = $(this).closest('tr');

          if (oTable.fnIsOpen(nTr)) {

              /* This row is already open - close it */
              this.src = "http://i.imgur.com/SD7Dz.png";
              oTable.fnClose(nTr);
          } else {
              /* Open this row */
              this.src = "http://i.imgur.com/d4ICC.png";
              oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, TableHtml), 'details-control');
              oInnerTable = $('#exampleTable_' + iTableCounter).dataTable({
                      'bJQueryUI': true,
                      'sPaginationType': 'full_numbers'
                  });
              iTableCounter = iTableCounter + 1;

          }

          $('#btn-show-all-children').on('click', function () {
              // Enumerate all rows
              oTable.rows().every(function () {
                  // If row has details collapsed
                  if (!this.oTable.fnIsOpen(nTr)) {
                      /* Open this row */
                      this.src = "http://i.imgur.com/d4ICC.png";
                      this.oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, TableHtml), 'details-control');
                      this.oInnerTable = $("#exampleTable_" + iTableCounter).dataTable({
                              'bJQueryUI': true,
                              'sPaginationType': 'full_numbers'
                          });
                      iTableCounter = iTableCounter + 1;
                  }
              });
          });

          // Handle click on "Collapse All" button
          $('#btn-hide-all-children').on('click', function () {
              // Enumerate all rows
              oTable.rows().every(function () {
                  // If row has details expanded
                  if (oTable.fnIsOpen(nTr)) {

                      /* This row is already open - close it */
                      this.src = "http://i.imgur.com/SD7Dz.png";
                      oTable.fnClose(nTr);
                  }
              });
          });
          $('#btn-show-all-children').on('click', function () {
              // Enumerate all rows
              oTable.rows().every(function () {
                  // If row has details collapsed
                  if (!this.oTable.fnIsOpen(nTr)) {
                      /* Open this row */
                      this.src = "http://i.imgur.com/d4ICC.png";
                      this.oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, TableHtml), 'details-control');
                      this.oInnerTable = $("#exampleTable_" + iTableCounter).dataTable({
                              'bJQueryUI': true,
                              'sPaginationType': 'full_numbers'
                          });
                      iTableCounter = iTableCounter + 1;
                  }
              });
          });

          // Handle click on "Collapse All" button
          $('#btn-hide-all-children').on('click', function () {
              // Enumerate all rows
              oTable.rows().every(function () {
                  // If row has details expanded
                  if (oTable.fnIsOpen(nTr)) {

                      /* This row is already open - close it */
                      this.src = "http://i.imgur.com/SD7Dz.png";
                      oTable.fnClose(nTr);
                  }
              });
          });
      });
  });
td.details-control {
        background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_open.png') no-repeat center center;
        cursor: pointer;
    }
    tr.shown td.details-control {
        background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_close.png') no-repeat center center;
 <html>
     <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables_themeroller.css">
    <link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script>
    <script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <!-- Col reorder with resize-->  
        <script src="colreorderwithresize.js"></script> 
        <script src="https://code.jquery.com/jquery-3.5.1.js"></script>     
        <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>  
     </head>

    <body>
    <button id="btn-show-all-children" type="button">Expand All</button>
     <button id="btn-show-all-children" type="button">Collapse All</button>
    <table id="exampleTable">
        <thead> 
            <tr>
                <th>Year</th>
                <th>Month</th>
                <th>Savings</th>
                
            </tr>
        </thead>
        <tbody>
             <tr>
          <td>2012</td>
          <td>January</td>
          <td>$100</td>
        </tr>
        <tr>
          <td>2012</td>
          <td>February</td>
          <td>$80</td>
        </tr>
        </tbody> 
    </table>  
         <div style="display:none">
    <table id="exampleTable_2" class="display select" width="100%">
      <thead>
        <tr>
          <th>First name</th> 
          <th>Last name</th>
          <th>Position</th>
          <th>Office</th>
          <th>Age</th>
          <th>Start date</th> 
          <th>Salary</th>
          <th>Extn.</th>
          <th>E-mail</th>
        </tr>
      </thead>
      <tbody >
        <tr>
          <td>Tiger</td>
          <td>Nixon</td>
          <td>System Architect</td>
          <td>Edinburgh</td>
          <td>61</td>
          <td>2011/04/25</td>
          <td>$320,800</td>
          <td>5421</td>
          <td>t.nixon@datatables.net</td>
        </tr>
        <tr>
          <td>Garrett</td>
          <td>Winters</td>
          <td>Accountant</td>
          <td>Tokyo</td>
          <td>63</td>
          <td>2011/07/25</td>
          <td>$170,750</td>
          <td>8422</td>
          <td>g.winters@datatables.net</td>
        </tr>
        </tfoot>  
    </table>
     </div>
     </body>
     </html>

【问题讨论】:

    标签: javascript html css datatables


    【解决方案1】:

    问题

    代码存在太多问题,无法一一列举。例如:

    • 包含多个版本的 jQuery DataTables - 1.9 和 1.10
    • 包括多个版本的 jQuery:1.11 和 3.5
    • DataTables 1.10 API 方法(例如 rows())在 DataTables 1.9 实例上被调用,有关详细信息,请参阅 API
    • 事件处理程序被多次分配到不正确的位置。

    解决方案

    请查看下面更正后的代码并根据您正在使用的库进行调整。

    function fnFormatDetails(table_id, html) {
    
          var sOut = "<table id=\"exampleTable_" + table_id + "\">";
    
          sOut += html;
          sOut += "</table>";
          return sOut;
      }
      var iTableCounter = 1;
      var oTable;
      var oInnerTable;
      var TableHtml;
    
      //Run On HTML Build
      $(document).ready(function () {
    
          TableHtml = $('#exampleTable_2').html();
    
          //Insert a 'details' column to the table
          var nCloneTh = document.createElement('th');
          var nCloneTd = document.createElement('td');
    
          $('#exampleTable thead tr').each(function () {
              this.insertBefore(nCloneTh, this.childNodes[0]);
          });
    
          //Initialse DataTables, with no sorting on the 'details' column
          var oTable = $('#exampleTable').dataTable({
                  'bJQueryUI': true,
    
                  'sPaginationType': 'full_numbers',
                  'aoColumnDefs': [{
                          'bSortable': false,
                          'class': 'details-control',
                          'aTargets': [0]
                      }
                  ],
                  'aaSorting': [[1, 'asc']]
              });
    
          /* Add event listener for opening and closing details
           * Note that the indicator for showing which row is open is not controlled by DataTables,
           * rather it is done here
           */
          $('#exampleTable tbody tr td.details-control').on('click', function () {
              var nTr = $(this).closest('tr');
    
              if (oTable.fnIsOpen(nTr)) {
    
                  oTable.fnClose(nTr);
              } else {
                  oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, TableHtml), 'details-control');
                  oInnerTable = $('#exampleTable_' + iTableCounter).dataTable({
                          'bJQueryUI': true,
                          'sPaginationType': 'full_numbers'
                      });
                  iTableCounter = iTableCounter + 1;
    
              }
          });
          
    
          // Handle click on "Collapse All" button
          $('#btn-hide-all-children').on('click', function () {
              // Enumerate all rows
              oTable.$('tr').each(function(index, nTr){              
                  // If row has details expanded
                  if (oTable.fnIsOpen(nTr)) {
                      oTable.fnClose(nTr);
                      $(nTr).removeClass('shown');
                  }
              });
          });
    
          $('#btn-show-all-children').on('click', function () {
              // Enumerate all rows              
              oTable.$('tr').each(function(index, nTr){
                  // If row has details collapsed
                  if (!oTable.fnIsOpen(nTr)) {
                      /* Open this row */
                      oTable.fnOpen(nTr, fnFormatDetails(iTableCounter, TableHtml), 'details-control');
                      $(nTr).addClass('shown');
                  }
              });
          });      
      });
    td.details-control {
            background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_open.png') no-repeat center center;
            cursor: pointer;
        }
        tr.shown td.details-control {
            background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_close.png') no-repeat center center;
    <html>
         <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.21/datatables.min.css">
        <script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
            <!-- Col reorder with resize-->  
            <script src="colreorderwithresize.js"></script> 
            <script src="https://cdn.datatables.net/v/dt/dt-1.10.21/datatables.min.js"></script>  
         </head>
    
        <body>
        <button id="btn-show-all-children" type="button">Expand All</button>
         <button id="btn-hide-all-children" type="button">Collapse All</button>
        <table id="exampleTable" class="display">
            <thead> 
                <tr>
                    <th>Year</th>
                    <th>Month</th>
                    <th>Savings</th>
                    
                </tr>
            </thead>
            <tbody>
                 <tr>
              <td></td>
              <td>2012</td>
              <td>January</td>
              <td>$100</td>
            </tr>
            <tr>
              <td></td>
              <td>2012</td>
              <td>February</td>
              <td>$80</td>
            </tr>
            </tbody> 
        </table>  
             <div style="display:none">
        <table id="exampleTable_2" class="display select" width="100%">
          <thead>
            <tr>
              <th>First name</th> 
              <th>Last name</th>
              <th>Position</th>
              <th>Office</th>
              <th>Age</th>
              <th>Start date</th> 
              <th>Salary</th>
              <th>Extn.</th>
              <th>E-mail</th>
            </tr>
          </thead>
          <tbody >
            <tr>
              <td>Tiger</td>
              <td>Nixon</td>
              <td>System Architect</td>
              <td>Edinburgh</td>
              <td>61</td>
              <td>2011/04/25</td>
              <td>$320,800</td>
              <td>5421</td>
              <td>t.nixon@datatables.net</td>
            </tr>
            <tr>
              <td>Garrett</td>
              <td>Winters</td>
              <td>Accountant</td>
              <td>Tokyo</td>
              <td>63</td>
              <td>2011/07/25</td>
              <td>$170,750</td>
              <td>8422</td>
              <td>g.winters@datatables.net</td>
            </tr>
            </tfoot>  
        </table>
         </div>
         </body>
         </html>

    链接

    有关更多信息和示例,请参阅jQuery DataTables: How to expand/collapse all child rows

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 2011-09-12
      • 2012-04-05
      • 1970-01-01
      • 2014-03-30
      • 2019-04-13
      • 1970-01-01
      • 2011-09-18
      相关资源
      最近更新 更多