views:

57

answers:

5

I have this table structure

<table id="latest" class="debt">
    <thead>
    <tr>
        <th width="50">Credor</th>
        <th width="50">Devedor</th>
        <th>Motivo</th>
        <th width="80">Valor</th>
        <th width="10"></th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($this->latest as $latest) { ?>
        <tr <?php if ($latest->reg_value < 0) { echo "class='error'"; } ?>>
        <td><?php echo $latest->findParentRow('Application_Model_DbTable_Names','Creditor')->nam_name; ?></td>
        <td><?php echo $latest->findParentRow('Application_Model_DbTable_Names','Debtor')->nam_name; ?></td>
        <td><?php echo $latest->reg_reason; ?></td>
        <td>R$ <?php echo number_format(abs($latest->reg_value), 2, ',', ' ')?></td>
        <td><a href="#" id="<?php echo $latest->reg_id; ?>" class="delete"><img src="http://192.168.0.102/libraries/css/blueprint/plugins/buttons/icons/cross.png" alt=""/></a></td>
        </tr>
    <?php } ?>
    </tbody>
</table>

each row has a delete button which uses AJAX and removes the tr element itself. What happens is: it may happen the tbody become empty when all rows are deleted, when this happens I'd like to delete all the table.

I know there is a pseudo-selector $('#latest tbody:empty')but this selects only the tbody. I want to select the whole table when the tbody is empty or something like it.

EDIT after answers

I inspected the element after deleting all rows: there's only and the thead tag inside the tag. Still, it didn't removed the table element. Look at the code

// TR Fading when deleted
$('.delete').live('click', function() {
    $.ajax({
    type: 'POST',
    url: 'history/delete/id/'+$(this).attr('id')
    });
    $(this).closest('tr').fadeOut('slow', function() {
    $(this).remove();
    if($(this).closest('table').find('tbody').is(':empty'))
        $('#latest').remove();
    });
    return false;
});
+2  A: 
if($('#latest tbody').is(':empty'))
   $('#latest').remove();

your might compress this to

$('#latest tbody:empty').parent().remove();

update

This won't work. It looks like remove() and detach() do leave some kind of crud behind after they remove a tr element. It's like a whitespace or a linebreak, so since the :empty selector also checks for textnodes, it's not empty at all.

so

if($('#latest tbody').children().length() < 1)
   $('#latest').remove();

should do the trick.

Reference: http://jsbin.com/esaye3/2/edit

jAndy
I inspected the element after deleting all rows: there's only <tbody></tbody> and the thead tag inside the <table> tag. Still, it didn't removed the table element. Look my code http://snipt.org/Qou thx
Rodrigo Alves
@rasouza: From your code, you should maybe use `$(this).closest('table').find('tbody').is(':empty')` for the `if-statement`
jAndy
why doesn't the no if-statement code work for this case? And why do I have to use find()? Thanks
Rodrigo Alves
Sorry the ignorance but it's still not working. Look how it is now: http://snipt.org/Qou
Rodrigo Alves
wow.. right I tested around a bit. It seems like `.remove()` and `detach()` remove the element, but they leave some kind of whitespace or breakline, so since `:empty` also checks for textnodes its not empty. Use `if($('#latest tbody').children().length() < 1)` as statement!
jAndy
First you give the wrong solution, which people upvoted. Then you add a solution that is pretty much like mine... Wow... nice going folks.
Gert G
A: 

How about:

$('#latest tbody:empty').parent(); 
S..
A: 
$("#latest:has('tbody:empty')").remove();
na43251
A: 
if ($('#latest tbody').children().length==0) {
  $('#latest').remove();
};
Gert G
A: 

I can't check if the element is empty or not 'cause it will never be empty for text nodes still remains inside its markup. I have to check if the <table> has <tr> inside it.

Rodrigo Alves