【问题标题】:Table show/hide columns表格显示/隐藏列
【发布时间】:2011-04-25 21:49:53
【问题描述】:

我有一张典型的桌子,

7 列,(每次分页到 100 行)。

我想要做的是有一个<select multiple="multiple"> 并让它根据名称列出所有列是各自的<th></th>,然后我想挂钩到 select onchange 事件,当发生更改时隐藏所有列被选中,对于隐藏的每一列,将 <tfoot> 的 colspan 减少 1。

表结构为:

<table>
 <thead>
  <tr>
    <th>first</th>
    <th>second</th>
    <th>third</th>
    <th>fourth</th>
    <th>fifth</th>
    <th>sixth</th>
    <th>seventh</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
  </tr>
 </tbody>
 <tfoot>
  <tr>
   <td colspan="7"></td>
  </tr>
 </tfoot>
</table>

我怀疑结尾选择会是这样的:

<select multiple="multiple">
 <option value="1" selected="selected">first</option>
 <option value="2" selected="selected">second</option>
 <option value="3" selected="selected">third</option>
 <option value="4" selected="selected">fourth</option>
 <option value="5" selected="selected">fifth</option>
 <option value="6" selected="selected">sixth</option>
 <option value="7" selected="selected">seventh</option>
</select>

【问题讨论】:

标签: jquery html-table


【解决方案1】:

这是一个工作示例:JSFiddle link

这里是关于如何做到这一点的代码摘要:

<table class="editable_table">
 <thead>
  <tr>
    <th>first</th>
    <th>second</th>
    <th>third</th>
    <th>fourth</th>
    <th>fifth</th>
    <th>sixth</th>
    <th>seventh</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>1</td>
   <td>2</td>
   <td>3</td>
   <td>4</td>
   <td>5</td>
   <td>6</td>
   <td>7</td>
  </tr>
 </tbody>
 <tfoot>
  <tr>
   <td class="footer" colspan="7">This is the footer</td>
  </tr>
 </tfoot>
</table>
<select multiple="multiple">
 <option value="1" selected="selected">first</option>
 <option value="2" selected="selected">second</option>
 <option value="3" selected="selected">third</option>
 <option value="4" selected="selected">fourth</option>
 <option value="5" selected="selected">fifth</option>
 <option value="6" selected="selected">sixth</option>
 <option value="7" selected="selected">seventh</option>
</select>

还有 javascript:

function hideCol($table, myIndex){
    $table.find("tr").each(function(){
        $(this).find("th:eq("+myIndex+"), td:eq("+myIndex+")").not('.footer').hide();
    });
}

function showCol($table, myIndex){
    $table.find("tr").each(function(){
        $(this).find("th:eq("+myIndex+"), td:eq("+myIndex+")").not('.footer').show();
    });
}

$("select").change(function(){
    var $table = $(".editable_table"),
        cols = $(this).val();
    for (var i = 1; i <= $table.find("th").length; i++){
        if (cols.indexOf(i+'') === -1) {
            hideCol($table, i-1);
        } else {
            showCol($table, i-1);
        }
    }
    $table.find("tfoot td").attr('colspan', cols.length);
});

由于您的问题相当模糊,我认为这就是您想要的。无论如何,它应该可以解决问题!

PS - 可能不是最有效的方式 - 但正如他们所说,考虑提高效率作为读者的练习

【讨论】:

    【解决方案2】:

    http://jsfiddle.net/96G7N/

    注意边缘条件(例如取消选择所有选项)

    $(document).ready(function(){
    
        $(".table-column-selector").change(function(){
            var selectedCols = $(this).val();
            var cols = $("table.tableClass thead th").length;
    
            var footer = $("table.tableClass tfoot tr td");
    
            footer.attr("colspan", selectedCols.length);
    
            for (col = cols - 1 ; col >= 0 ; col--) {
                var columns = $("table.tableClass tbody tr td:nth-child(" + (col + 1) + ")");
                var headers = $("table.tableClass thead tr th:nth-child(" + (col + 1) + ")");
    
                if ($.inArray( col + 1 + "", selectedCols ) != -1) {
                   columns.show();
                   headers.show();
                }
                else {
                    columns.hide();
                    headers.hide();
                }
            }
        })
    });
    

    【讨论】:

      【解决方案3】:

      这就是我的做法,http://jsfiddle.net/pxfunc/mFmjb/2/

      jQuery:

      // setup re-usable variables
      var $myGrid = $('#myGrid'),
          $colFilter = $('#columnFilter'),
          optionElementTemplate = "<option value=\"{0}\">{0}</option>";    
      
      // iterate the <th> tags to build the multi-select box
      $('th', $myGrid).each(function() {
          // create a jquery object to append from the optionElementTemplate and the <th> text
          $colFilter.append($(optionElementTemplate.replace(/\{0\}/g, $(this).text())));
      });
      
      $colFilter.change(function() {
          // store count for footer
          var count = 0;
      
          // show all columns
          resetTable();
      
          // iterate over the select options for selected values
          $('option', this).each(function() {
              if(!$(this).attr("selected")) {
                  count++;
                  hideColumn($(this).index());
              }
          });
      
          // adjust the colspan
          adjustFooter($('option', this).length - count);
      });
      
      function resetTable() {
          $('#myGrid td, #myGrid th').show();
          $('#myGrid tfoot td').attr("colspan", $('#columnFilter option').length);
      }
      
      function hideColumn(index) {
          $('#myGrid thead th:eq(' + index + ')').hide();
          $('#myGrid tbody td:eq(' + index + ')').hide();
      }
      
      function adjustFooter(count) {
          $('#myGrid tfoot td').attr("colspan", count);
      }
      

      在将其投入生产之前可能会进行一些微调,但功能就在那里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-25
        • 2010-10-28
        • 2010-10-02
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 2016-12-13
        • 1970-01-01
        相关资源
        最近更新 更多