jQuery remove HTML table column

后端 未结 11 1353
粉色の甜心
粉色の甜心 2020-12-13 07:13

I have a HTML table like this:

DE
相关标签:
11条回答
  • 2020-12-13 08:08

    A generic way (not tested):

    $("a.delete").click(function() {
       var colnum = $(this).closest("td").prevAll("td").length;
    
       $(this).closest("table").find("tr").find("td:eq(" + colnum + ")").remove();
    }
    

    No need to change markup.

    0 讨论(0)
  • 2020-12-13 08:11

    jQuery:

       $('.delete').click(function() {
           var colNumber = $(this).parents().find('td').attr('col');
           $('td[col='+colNumber+']').remove();
           return false;
        });
    

    HTML:

    <table border="1">
        <tbody>
        <tr>
                    <td col='1'><a href="#" class="delete">DELETE COL</a>COL 1</td>
                    <td col='2'><a href="#" class="delete">DELETE COL</a>COL 2</td>
                <td col='3'><a href="#" class="delete">DELETE COL</a>COL 3</td>
                <td col='4'><a href="#" class="delete">DELETE COL</a>COL 4</td>
                <td col='5'><a href="#" class="delete">DELETE COL</a>COL 5</td>
                <td col='6'><a href="#" class="delete">DELETE COL</a>COL 6</td>
            </tr>
            <tr>
                    <td col='1'>ROW 1</td>
                    <td col='2'>ROW 1</td>
                <td col='3'>ROW 1</td>
                <td col='4'>ROW 1</td>
                <td col='5'>ROW 1</td>
                <td col='6'>ROW 1</td>
            </tr>
            <tr>
                    <td col='1'>ROW 2</td>
                    <td col='2'>ROW 2</td>
                <td col='3'>ROW 2</td>
                <td col='4'>ROW 2</td>
                <td col='5'>ROW 2</td>
                <td col='6'>ROW 2</td>
            </tr>
        </tbody>
    </table>
    
    0 讨论(0)
  • 2020-12-13 08:15

    I didn't really like any of the solutions from this post, so I came up with my own. Idealy what needed is :nth-of-type selector which would make things way easier. But unfortunately JQuery does not support it "due to their lack of real-world usefulness". Ehh..

    So here's my solution which does the trick using :nth-child expression:

    $("a.delete").click(function(event) {
       event.preventDefault();
    
       var current_cell = $(this).closest("td");
       var nb_columns = current_cell.closest('table').find('tr:eq(1) td').length+1;
       var column_to_delete = current_cell.prevAll("td").length+1;
    
       $('table tr td:nth-child('+(nb_columns+'n-'+(nb_columns-column_to_delete))+')').remove();
    });
    
    0 讨论(0)
  • 2020-12-13 08:17

    After a few years, it's probably time to update the answer on this question.

    // Listen for clicks on table originating from .delete element(s)
    $("table").on("click", ".delete", function ( event ) {
        // Get index of parent TD among its siblings (add one for nth-child)
        var ndx = $(this).parent().index() + 1;
        // Find all TD elements with the same index
        $("td", event.delegateTarget).remove(":nth-child(" + ndx + ")");
    });
    
    0 讨论(0)
  • 2020-12-13 08:18

    When I've read this post I tried the first solution using jQuery's remove function. But it seems to have a problem with this function when using it on a table row to delete cell. The problem is bind to a concurrent modification. In the exemple with this reponse if you try to use the index() function it will not work because cell index is changing each time you remove a cell. One solution could be to use the hide() function on the cell you want to delete. But if you really need to delete the column (remove it from the DOM) the way which has worked for me were to use the javascript native to remove the column.

    $(function() {      
        $('table tr').each(function(e, row) {
        var i = 0;
        $(row).find('td, th').each(function(e, cell) {          
            if (i == 1)  { 
               row.removeChild(cell);  
            }   
            i++;                    
        });
    });
    

    In this example you delete the second column of the table : i == 1 ...

    0 讨论(0)
提交回复
热议问题