【问题标题】:Hide/Show Columns in an HTML Table (with multiple column in the header)在 HTML 表格中隐藏/显示列(标题中有多个列)
【发布时间】:2020-12-07 17:53:26
【问题描述】:

谁能帮忙,我正在尝试隐藏/显示我的 html 表格列,我的表格在标题中有三行,需要隐藏/显示第二行的列以及包含的小行,我使用了一个示例在 Stackoverflow 中找到但仍然无法成功完成这项工作,我的代码如下:

        $(function() {
  // on init
  $(".table-hideable .hide-col").each(HideColumnIndex);

  // on click
  $('.hide-column').click(HideColumnIndex)

  function HideColumnIndex() {
    var $el = $(this);
    var $cell = $el.closest('th,td')
    var $table = $cell.closest('table')

   
    var colIndex = $cell[0].cellIndex + 1;

    // find and hide col index
    $table.find("tbody tr, thead tr")
      .children(":nth-child(" + colIndex + ")")
      .addClass('hide-col');
      
    // show restore footer
    $table.find(".footer-restore-columns").show()
  }

  // restore columns footer
  $(".restore-columns").click(function(e) {
    var $table = $(this).closest('table')
    $table.find(".footer-restore-columns").hide()
    $table.find("th, td")
      .removeClass('hide-col');

  })

  $('[data-toggle="tooltip"]').tooltip({
    trigger: 'hover'
  })

})
        body {
  padding: 15px;
}

.table-hideable td,
.table-hideable th {
  width: auto;
  transition: width .5s, margin .5s;
}

.btn-condensed.btn-condensed {
  padding: 0 5px;
  box-shadow: none;
}


/* use class to have a little animation */
.hide-col {
  width: 0px !important;
  height: 0px !important;
  display: block !important;
  overflow: hidden !important;
  margin: 0 !important;
  padding: 0 !important;
  border: none !important;
}
<!DOCTYPE html>
<html>
<head>

    <script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/paper/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<table class="table table-condensed table-hover table-bordered table-striped table-hideable">

  <thead>
      <tr>
      <th colspan="4" > 
        
        TOP </th>
        </tr>
        <tr>
      <th colspan="2" > <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">
          <i class="fa fa-eye-slash"></i>  
        </button>
        
        A </th>
      <th  colspan="2" > <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">
          <i class="fa fa-eye-slash"></i>  
        </button>
       
        B
      </th>
 
      </tr>
       <tr>
       <th>
         
         AA </th>
       <th class="hide-col"> 
        
         AB   </th>
       <th>  
        
         BB </th>
      <th > 
        
        BC </th>

       </tr>
  </thead>
  <tbody>

    <tr>
      <td>Home</td>
      <td>Index</td>
      <td>ActionResult</td>
      <td>Authorize</td>
      
    </tr>

    <tr>
      <td>Client</td>
      <td>Index</td>
      <td>ActionResult</td>
      <td>Authorize</td>
     
    </tr>

    <tr>
      <td>Client</td>
      <td>Edit</td>
      <td>ActionResult</td>
      <td>Authorize</td>
     
    </tr>

  </tbody>
  <tfoot class="footer-restore-columns">
    <tr>
      <th colspan="4"><a class="restore-columns" href="#">Some columns hidden - click to show all</a></th>
    </tr>
  </tfoot>
</table>

非常感谢

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    试试这样:

    $(function() {
      // on click
      $('.hide-column').click(HideColumns)
      
      // on init
      $('.hide-column').eq(0).trigger( "click" );
    
      function HideColumns() {
        var $el = $(this);
        var $cell = $el.closest('th,td')
        var $table = $cell.closest('table')
    
        var colIndex = 0;
        var colSpan = 0;
    
        $cell.parent().children().each(function(index) {
          if ($(this).is($cell)) {
            colSpan = parseInt($cell.attr("colspan") || 1);
            return false;
          } else {
            colIndex += parseInt($(this).attr("colspan") || 1);
          };
        });
    
        // find and hide col index
        for (var i = colIndex; i < (colIndex + colSpan); i++) {
          $table.find("tbody tr, thead :nth-child(3)")
            .children(":nth-child(" + (i + 1) + ")")
            .addClass('hide-col');
        };
        
        //always show first header
        $table.find("thead :nth-child(1)").children().removeClass('hide-col');
        
        //hide clicked cell
        $cell.addClass('hide-col');
    
        // show restore footer
        $table.find(".footer-restore-columns").show()
      }
    
      // restore columns footer
      $(".restore-columns").click(function(e) {
        var $table = $(this).closest('table')
        $table.find(".footer-restore-columns").hide()
        $table.find("th, td")
          .removeClass('hide-col');
    
      })
    
      $('[data-toggle="tooltip"]').tooltip({
        trigger: 'hover'
      })
    
    })
    body {
      padding: 15px;
    }
    
    .table-hideable td,
    .table-hideable th {
      width: auto;
      transition: width .5s, margin .5s;
    }
    
    .btn-condensed.btn-condensed {
      padding: 0 5px;
      box-shadow: none;
    }
    
    
    /* use class to have a little animation */
    
    .hide-col {
      width: 0px !important;
      height: 0px !important;
      display: block !important;
      overflow: hidden !important;
      margin: 0 !important;
      padding: 0 !important;
      border: none !important;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
    
      <script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
    </head>
    
    <body>
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/paper/bootstrap.min.css">
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    
      <table class="table table-condensed table-hover table-bordered table-striped table-hideable">
    
        <thead>
          <tr>
            <th colspan="4">
    
              TOP </th>
          </tr>
          <tr>
            <th colspan="2"> <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column" data>
              <i class="fa fa-eye-slash"></i>  
            </button> A </th>
            <th colspan="2"> <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">
              <i class="fa fa-eye-slash"></i>  
            </button> B
            </th>
    
          </tr>
          <tr>
            <th>
    
              AA </th>
            <th class="hide-col">
    
              AB </th>
            <th>
    
              BB </th>
            <th>
    
              BC </th>
    
          </tr>
        </thead>
        <tbody>
    
          <tr>
            <td>Home</td>
            <td>Index</td>
            <td>ActionResult</td>
            <td>Authorize</td>
    
          </tr>
    
          <tr>
            <td>Client</td>
            <td>Index</td>
            <td>ActionResult</td>
            <td>Authorize</td>
    
          </tr>
    
          <tr>
            <td>Client</td>
            <td>Edit</td>
            <td>ActionResult</td>
            <td>Authorize</td>
    
          </tr>
    
        </tbody>
        <tfoot class="footer-restore-columns">
          <tr>
            <th colspan="4"><a class="restore-columns" href="#">Some columns hidden - click to show all</a></th>
          </tr>
        </tfoot>
      </table>

    由于正在使用带有colspanth 元素,因此需要一种不同的方法。上面的解决方案不是只考虑当前列索引,而是根据点击按钮的th$cell)在tbody内找到td单元格的索引范围。

    为了隐藏列的跨度,它首先循环遍历表的第二行,将每个 colspan 添加到 colIndex 直到它到达 $cell。然后,要隐藏在每一行上的 td 索引是从 colIndexcolIndex + $cell (1-based in jQuery) 的 colspan

    【讨论】:

    • 嗨,sbgib,在实施此解决方案时仍然存在问题,当我开始从右侧隐藏列时它工作正常,但是当我从左侧开始时,我可以看到问题;有些单元格即使不需要也被隐藏,我可以把代码发给你吗?
    • 它与工作演示相比有何变化?如果你能把它上传到某个地方,我可以快速浏览一下。
    • 当然,这是它的链接:stackoverflow.com/questions/65288649/…
    猜你喜欢
    • 2010-10-02
    • 2021-03-25
    • 2011-02-20
    • 2016-01-28
    • 1970-01-01
    • 2015-05-16
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多