How to add and remove rows and columns in html table with jquery

Asked

Viewed 2,076 times

1

I have a table where I allow the user to insert/remove rows when they find it necessary, but what I need and am not able to adapt is to insert a block of Columns and Rows, but without success, I will try to show you what I have. I have this table:

inserir a descrição da imagem aqui

I’m trying to add this whole block when the user clicks on Adicionar Linha, I have already made some searches and found nothing similar and my knowledge in Jquery is little, almost nothing.

The skeleton of the table is like this:

<table width="60%" border="1" id="tabela-herdeiro" class="table">
<tbody>
    <tr>
        <td width="8%">Nome</td>
        <td width="23%">&nbsp;</td>
        <td width="10%">Nacionalidade</td>
        <td width="27%">&nbsp;</td>
        <td width="10%">Estado Civil</td>
        <td width="22%">&nbsp;</td>         
    </tr>
    <tr>
        <td>Residência</td>
        <td>&nbsp;</td>
        <td>RG</td>
        <td>&nbsp;</td>
        <td>CPF</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>Cônjuge</td>
        <td>&nbsp;</td>
        <td>RG</td>
        <td>&nbsp;</td>
        <td>CPF</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="6" align="right"><button class="btn btn-large btn-danger btn-xs" onclick="RemoveTableRowPessoa(this)" type="button">Remover</button></td>
    </tr>       
</tbody>

  Add Line

The Jquery that inserts lines is this:

   (function($) {

    RemoveTableRowPessoa = function(handler) {
      var tr = $(handler).closest('tr');

      tr.fadeOut(400, function(){ 
        tr.remove(); 
      }); 

      return false;
    };



    AddTableRowPessoa = function() {

        var newRow = $("");
        var cols = "";

        cols += 'Nome  Nacionalidade  Estado Civil  RG  CPF  Residênvia  Cônjuge  RG Cônjuge  CPF Cônjuge ';

        cols += '';
        cols += 'Remover';
        cols += '';

        newRow.append(cols);

        $("#tabela-herdeiro").append(newRow);

        return false;
    };          



   })(jQuery);  

But I couldn’t adapt, like I said

  • tries to make a clone and then an append.

  • maybe this question can help you. https://answall.com/questions/37731/como-remover-tabela-generatedstring

1 answer

1


Do not use onclick on the button, because it will not work because the function called by the event is outside the scope, inside (function($){.

What you can do is create an Event Handler by pointing to the button and in the function of that event remove the respective block:

$(document).on("click", "#tabela-herdeiro button", function(){
   // REMOVER O BLOCO
});

Clicking the "remove" button will remove the TBODY where the button is.

To add a new block, on page loading, create a copy (not clone) of the HTML of TBODY and save to a variable:

var copia = document.querySelector("#tabela-herdeiro tbody").outerHTML;

Whenever you want to add a new TR’s block, just make a .append of copia on the table.

For the "Add" button, also create an Event Handler:

$("#adicionar").on("click", function(){
    $("#tabela-herdeiro").append(copia);
});          

Let’s see it working:

$(function(){

   // já cria logo uma cópia do TBODY original
   var copia = document.querySelector("#tabela-herdeiro tbody").outerHTML;

    $(document).on("click", "#tabela-herdeiro button", function(){

      var tr = $(this).closest("tbody");
      
      tr.fadeOut(400, function(){
        this.remove(); 
      }); 

    });

    $("#adicionar").on("click", function(){
        $("#tabela-herdeiro").append(copia);
    });          

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table width="60%" border="1" id="tabela-herdeiro" class="table">
<tbody>
    <tr>
        <td width="8%">Nome</td>
        <td width="23%">&nbsp;</td>
        <td width="10%">Nacionalidade</td>
        <td width="27%">&nbsp;</td>
        <td width="10%">Estado Civil</td>
        <td width="22%">&nbsp;</td>         
    </tr>
    <tr>
        <td>Residência</td>
        <td>&nbsp;</td>
        <td>RG</td>
        <td>&nbsp;</td>
        <td>CPF</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>Cônjuge</td>
        <td>&nbsp;</td>
        <td>RG</td>
        <td>&nbsp;</td>
        <td>CPF</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="6" align="right"><button class="btn btn-large btn-danger btn-xs" type="button">Remover</button></td>
    </tr>       
   
</tbody>
</table>
<br>
<button id="adicionar">Adicionar linha</button>

  • Great, thanks for the great code, just one more question, how can I copy the table style? Clicking the Add button on the copied table is without the class="table formatting".

  • Ixi rs.. was to apply normally... if you could send a print of how it is getting would make it easier for me to analyze

  • Relax, what you did has already helped me a lot, as the print goes?

  • https://i.stack.Imgur.com/uZxWA.png

  • 1

    I changed all the code. Copy again to make a test.

  • Perfect, thank you so much for your help.

  • 1

    Blz... no longer needs the "lines" class I had suggested. Abs!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.