How to send data from a dynamic HTML table via json

Asked

Viewed 962 times

0

I need to do something similar to the code below:

Here I create the htmla table dynamically:

$('#btnIncluirContato').on('click', function () {

    $('#tblContato tbody').append('<tr id=' + $('#ContatoID').val() + '>' +
    '<td class="NomeContato" id="NomeContato">' + $('#NomeContato').val() + '</td>' +
    '<td class="TelefoneContato" id="TelefoneContato">' + $('#TelefoneContato').val() + '</td>' +
     '<td class="remover" id="remover">' + '<a href="#"><img src="/Content/Images/excluirFlatRed.png" class="remover" /></a>' + '</td>' +
    '</tr>');

});

Here sending to the database via ajax:

 $("#btnFinalizar").click(function () {

    //Percorrer a tabela 
    var table = $('#tblContato');
    table.find('tr').each(function (indice) {

        $(this).find('td').each(function (indice) {

        });

    });

    //Setar campos inputs da Tela
    var _TBCliente = {
        'ID': $('#campoID').val(),
        'Nome': $('#campoNome').val(),
        'Idade': $('#campoIdade').val(),
        'TBContato':[]
    };

    //Adicionar os itens da Tabela de Contato
    _TBCliente.TBContato.push({

    });

    //Com o objeto populado eu vou enviar para o banco via $.ajax
});

The main question is how to take the data from the HTML table and popular the object that will be sent to a Jsonresult method ?

1 answer

1


you can try like this:

var dados  =  [];
var i     =  0;
var table = $('#tblContato');
table.find('tr').each(function () {
   dados[i]['id']  =  $( this ).find( 'td:nth-child(1)' ).text();
   dados[i]['nome']  =  $( this ).find( 'td:nth-child(2)' ).text();
   dados[i]['idade']  =  $( this ).find( 'td:nth-child(3)' ).text();
   dados[i]['contato']  =  $( this ).find( 'td:nth-child(4)' ).text();
   i++;
});
$.ajax({
   type: "POST",
   data: {meusDados:dados},
   url: "http://seusite.com.br/processarPost",
   success: function( data ){
       alert( 'Enviou' )
   },
   error: function (request, status, error) {
      alert(request.responseText);
   }
});

Switch the Nth-Child pseudo-class to your case.

I hope it helps.

  • The pseudo-class you refer to in the table row class <td class="ModeloEquipamentoID" correct ? Then it would look that way ? $(this).find('td:ModeloEquipamentoID(1)').text();

  • No friend, the pseudo class refers to an order, so its first TD will be Nth-Child(1), the second TD Nth-Child(2) and so on. Remember that the pseudo class is referencing the daughter Tds of the TR that is in the looping iteration

  • Very good! I added the code to create the object dados[i] = new Object(), but there is a problem: the code is bringing an empty line more than the header, as I do not add in the data array[] the first row (header) of the table ?

  • If you are separating the table with <thead> and <tbody>, you can use it like this: $('#tblContact > tbody');. If you are not using thead and tbody, you can filter the Trs by discarding the first line, thus: table.find('tr'). not( ':first' ).each(...

  • thanks! , it worked.

  • Dispose friend!!

Show 1 more comment

Browser other questions tagged

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