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 ?
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()
;– hard123
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
– Duque
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 ?– hard123
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(...
– Duque
thanks! , it worked.
– hard123
Dispose friend!!
– Duque