Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't it remove the table?

I have this piece of 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;
});

It triggers when I want to delete a table row through the delete button (as shows the image) image http://aviary.com/viewfull?fguid=433f68f6-d18d-102d-a9f3-0030488e168c&nowatermark=true

It may happen that the table becomes empty of table rows. I want to delete the whole table when this occurs, but the table isn't being removed. The line code $(this).remove(); works and this seems to refer to the tr element in that scope 'cause the whole row is being removed but the next 2 lines doesn't work. The table isn't being removed.

EDIT

I changed the if($(this).closest('table').find('tbody').is(':empty')) to if(!$(this).closest('table').find('tbody').is(':empty')) (exclamation mark) to see if it removes and it removed the whole table, but I inspected the table element before and after deleting the last row and got this

image http://rookery9.aviary.com.s3.amazonaws.com/4344000/4344383_4fbd.png

JS says that tbody is not empty, google chrome says otherwise. I don't know how to fix it

like image 254
Rodrigo Souza Avatar asked Jun 25 '10 10:06

Rodrigo Souza


People also ask

How do you remove a table in Word?

Click Layout > Delete Table.

How a table can be deleted?

Click the “Layout” tab under “Table Tools”. Click “Delete” in the “Rows & Columns” section and select “Delete Table” to delete the table. You can also use the “Delete Columns” and “Delete Rows” options to delete the entire table as long as the entire table is selected.


2 Answers

The problem is that once you remove it, relative selectors like .closest() won't do anything, since that element doesn't have a parent anymore. Also :empty isn't working because it includes text nodes, from the docs:

Select all elements that have no children (including text nodes).

So :empty won't match if there's any white-space left (alert or log $("latest tbody").html()) to see what I mean), which is almost certainly the case, the same goes for my example below. Instead you can use :has(tr) to check for table rows, and :not() to negate that, like this:

$('.delete').live('click', function() {
    $(this).closest('tr').fadeOut('slow', function() {
        $(this).remove();
        $("#latest tbody:not(:has(tr))").parent().remove();
    });
    return false;
});

You can view a quick demo here, if the :not(:has(tr)) selector doesn't match anything...and it won't if there's not a row-less <tbody>, it just won't remove anything.

like image 140
Nick Craver Avatar answered Oct 03 '22 12:10

Nick Craver


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

update

Like Nick Craver mentioned, this behavior is caused by the HTML markup itself.

<tbody><tr><td>Something</td><td>Anything?</td><td><button class="delete">delete</button></td></tr></tbody>

for instance, WILL work with :empty aswell.

like image 37
jAndy Avatar answered Oct 03 '22 12:10

jAndy