【问题标题】:Hiding a table column if the containing cells are empty with jQuery如果包含单元格为空,则使用 jQuery 隐藏表格列
【发布时间】:2012-02-18 16:10:16
【问题描述】:

我有一张如下表:

<table id="mytable" width="500" border="1" cellspacing="0" cellpadding="0">
  <thead>
  <tr>
    <th><span>1</th><th><span>2</th><th><span>3</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td><span>1/span></td>
    <td><span></span></td>
    <td><span>2/span></td>
  </tr>
  <tr>
    <td><span>1</span></td>
    <td><span></span></td>
    <td><span>2</span></td>
  </tr>
  <tr>
    <td><span>1</span></td>
    <td><span></span></td>
    <td><span>2</span></td>
  </tr>
  </tbody>
</table>

我需要做的是 - 隐藏该表格的所有列,其中表格单元格包含的&lt;span&gt; 元素为空。我需要完全隐藏单元格,顶部有 &lt;th&gt; 元素。在我上面的示例中,它是中间列,但可能有很多,而不仅仅是一个。

有人可以就此提出建议吗?

提前致谢。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    我创建了一个版本,它的性能可能比在 jQuery 中使用大量 CSS3 选择器好一点。

    $(function () {
        var $table = $('#mytable'),
            $thead = $table.find('thead'),
            $tbody = $table.find('tbody');
    
        var isEmpty = {};
        $tbody.find('td').each(function () {
    
            var $this = $(this);
            if ( $this.text() == '' && isEmpty[ $this.index() ] != false ) {
                isEmpty[ $this.index() ] = true;
            } else {
                isEmpty[ $this.index() ] = false;
            }
    
        });
    
        for (var x in isEmpty) {
            if ( isEmpty[x] ) {
                $thead.find('th').eq( x ).remove();
                $tbody.find('td:nth-child(' + (parseInt(x, 10) + 1) + ')').remove();
            }
        }
    });
    

    【讨论】:

    • 这确实只隐藏了 6 列中的 3 列。
    【解决方案2】:

    这应该可行:

    $(document).ready(function() {
        hideEmptyCols($("#mytable"));
    });
    
    function hideEmptyCols(table) {
        //count # of columns
        var numCols = $("th", table).length;
        for ( var i=1; i<=numCols; i++ ) {
            var empty = true;
            //grab all the <td>'s of the column at i
            $("td:nth-child(" + i + ")", table).each(function(index, el) {
                //check if the <span> of this <td> is empty
                if ( $("span", el).text() != "" ) {
                    empty = false;
                    return false; //break out of each() early
                }
            });
            if ( empty ) {
                $("td:nth-child(" + i + ")", table).hide(); //hide <td>'s
                $("th:nth-child(" + i + ")", table).hide(); //hide header <th>
            }
        }
    }
    

    或者(更简单):

    function hideEmptyCols(table) {
        var rows = $("tr", table).length-1;
        var numCols = $("th", table).length;
        for ( var i=1; i<=numCols; i++ ) {
            if ( $("span:empty", $("td:nth-child(" + i + ")", table)).length == rows ) {
                $("td:nth-child(" + i + ")", table).hide(); //hide <td>'s
                $("th:nth-child(" + i + ")", table).hide(); //hide header <th>
            }
        }
    }
    

    【讨论】:

    • 感谢@maclema 的帮助。您能否建议是否可以“跳过”表格中的第一列而不隐藏它?情况是,第一列由 标记组成,内部没有 ,它是一种表格垂直标题,我不想在使用您的函数时隐藏它。
    • 让它工作,刚刚添加了 $("th:nth-child(1)", table).show();到底。如果即使函数隐藏了第一列,我也会在之后显式显示它。
    • 一旦我发现 Bootstrap 类 'visible-lg' 覆盖了 jQuery,LOL,这会导致一些列仍然显示。一旦我删除了课程,实际上所有的空白列都被隐藏了。不错。
    • @cycero - 您可以尝试手动将起始编号更新为 2 而不是 1。因此,循环类似于for ( var i=2; i&lt;=numCols; i++ )。我知道现在回复你为时已晚,但有些人仍然觉得它很有用。
    【解决方案3】:

    我建议为每个 th 和 td 添加一个类(例如“col_1”、“col_2”等)并使用$("td").children("span:empty") 查找应该隐藏的列。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签