Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a table column if the containing cells are empty with jQuery

Tags:

jquery

I have a table of the following kind:

<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>

What I need to do is - hide all the columns of this table where the <span> element contained by the table cell is empty. I will need to hide the cell fully, with the <th> element on the top. In my example above it's the middle column but there may be a lot of them, not only one.

Could anybody advise over this?

Thanks in advance.

like image 820
cycero Avatar asked Jan 25 '12 13:01

cycero


2 Answers

This should work:

$(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>
        }
    }
}

Or (simpler):

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>
        }
    }
}
like image 121
Matt MacLean Avatar answered Sep 23 '22 07:09

Matt MacLean


I created a version that maybe perform a little better than using a lot of CSS3 selectors in jQuery.

$(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();
        }
    }
});
like image 39
Eliseu Monar dos Santos Avatar answered Sep 25 '22 07:09

Eliseu Monar dos Santos