'Update a dynamic created JS html table after a row is deleted

I have an HTML table dynamically created via JavaScript. After filling in some fields, clicking the "Add" button adds a new row to the table. After inserting a few rows in the table, the form is sent to an asp.net core controller.

To create the table dynamically, I use:

$("#btn-add").click(function () {
  var index = $('#tblItensPedido tr').length - 1
            var grife = $("#addGrife").val();
        var modelo = $("#addModelo").val();
        var cor = $("#addCor").val();
        var calibre = $("#addCalibre").val();
        var shape = $("#addShape").val();
        var quantidade = $("#addQuantidade").val();
        var valorTabela = $("#valorTabela").val();
        var valorTotal = $("#valorTotal").val();

  var markup = '<tr>\
                      <td>' + index + '</td>\
                      <td><input value="'+ grife + '" name="Itens[' + index + '].Grife" style="width:50px; border:none;"" readonly=readonly></input></td>\
                      <td><input value="'+ modelo + '" name="Itens[' + index + '].Modelo" style="width:55px; border:none;" readonly=readonly></input></td>\
                      <td><input value="'+ cor + '" name="Itens[' + index + '].Cor" style="width:50px; border:none;" readonly=readonly></input></td>\
                      <td><input value="'+ calibre + '" name="Itens[' + index + '].Tamanho" style="width:50px; border:none;" readonly=readonly></input></td>\
                      <td><input value="'+ shape + '" name="Itens[' + index + '].Shape" style="width:50px; border:none;" readonly=readonly></input></td>\
                      <td><input value="'+ quantidade + '" name="Itens[' + index + '].Quantidade" style="width:50px; border:none;" readonly=readonly></input></td>\
                      <td><input value="'+ valorTabela + '" name="Itens[' + index + '].ValorUnitario" style="width:100px; border:none;" id="currency" readonly=readonly></input></td>\
                      <td><input value="'+ valorTotal + '" style="width:100px; border:none;" readonly=readonly></input></td>\
                      <td><a class="btn btn-danger btn-sm btn-delete remover-linha" id="btn-deleta"><span class="fa fa-trash"></span></a></td>\
                      </tr>\ ';
        $("table tbody").append(markup);

)};

And to remove a row on the table I use:

    $('#tblItensPedido').on('click', '.remover-linha', function () {
        var indexDoRow = $(this).closest('tr').index();
        console.log("index removido " + indexDoRow)

        $(this).closest('tr').remove();
        console.log('o index é ' + indexDoRow)
    });

The "index" variable is used to create an array index on the table which will send to the server Itens[0].Grife, and on another row Index[1].Grife and so on...

It is working ok, but when I remove a row on the table, I need the index to be updated, because if I remove the index[0] (first row), the others row begin on the [1] index and my server crash.

Any way to update the entire index of the rows when a row is deleted?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source