views:

92

answers:

3

What I have to work with:
An html table 5X7. On many queries, there are less that 35 items filling the complete table.

How can I "hide" the empty cells dynamically in this case, using jQuery (or any other efficient way)?

Thank you.

+1  A: 

Obviously you'll want to adjust the selector to fit your specific needs:

$('td').each(function(){
  if ($(this).html() == '') {
    $(this).hide();
  }
});
David Fells
I've had situations where $('td:empty').hide(); did not work in Safari so I don't use it. I wonder if it's more reliable in newer versions of jquery.
David Fells
+1  A: 

Edit - Improved Version

// Grab every row in your table
$('table#yourTable tr').each(function(){
  if($(this).children('td:empty').length === $(this).children('td').length){
    $(this).remove(); // or $(this).hide();
  }
});

Not tested but seems logically sound.

// Grab every row in your table
$('table#yourTable tr').each(function(){
  var isEmpty = true;
  // Process every column
  $(this).children('td').each(function(){
    // If data is present inside of a given column let the row know
    if($.trim($(this).html()) !== '') {
      isEmpty = false;
      // We stop after proving that at least one column in a row has data
      return false;
    }
  });
  // If the whole row is empty remove it from the dom
  if(isEmpty) $(this).remove();
});
Ballsacian1
+1  A: 
$('td:empty').hide();
inkedmn