How can I hide empty html table cells with jQuery?

后端 未结 5 1277
别跟我提以往
别跟我提以往 2021-01-16 03:45

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 dynam

5条回答
  •  时光取名叫无心
    2021-01-16 04:02

    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();
    });
    

提交回复
热议问题