doubt sore o each JQUERY

1

I'm breaking my head and I can not seem to find the problem to have a table that is created dynamically when I scan it it appears position 0 as null and after that it starts to appear normal

HTML screen

linecreationintable

varid=1;functioncriar_linhaGrid(){vardados={Codigo:$("#txt_codigo").val(),
        Descricao: $("#txt_descricao").val()

    };

    // verificar se o códigoInterno da conversao foi digitado
    if (dados.Codigo != "") {


        $("#grid_cadastro > tbody").append(
         "<tr class = 'dadosConversao'>" +
        "    <td class='id'>" + dados.id + "</td>" +
        "    <td class='codigo'>" + dados.Codigo + "</td>" +
        "    <td class='descricao'>" + dados.Descricao + "</td>" +
        "    <td <a class='btn btn-danger btn-xs btn-Excluir' role='button'><i class='glyphicon glyphicon-trash'></i>  Excluir</a>" + "</td>" +
        "</tr>"
        )

        $("#modal_incluir").parents('.bootbox').modal('hide');

        $("#txt_codigo").val("");
        $("#txt_descricao").val("");

       id = id + 1;

    } 

};

HTML

                                        <div id="cabecalho_grid" class="row">
                                        <div class="col-md-12">
                                            <table id="grid_cadastro" class="table table-bordered table-striped table-hover">
                                                <thead>
                                                    <tr>
                                                        <th>ID</th>
                                                        <th>Código</th>
                                                        <th>Descrição</th>
                                                        <th>Ação</th>
                                                    </tr>
                                                </thead>
                                              <tbody>

                                                </tbody>
                                            </table>
                                        </div>
                                    </div>


                                </div>
                            </div>

JQUERY to pass through ajax my view lists

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


    var arrayConversao = $('.id');
    var listaConversao = new Array();
    $('#grid_cadastro tr').each(function (index) {

        listaConversao.push({
                   IDConversao: index,
                    Codigo: $(this).find('.codigo').text(),
                    Descricao: $(this).find('.descricao').text()

                });

                return listaConversao;
    });


});

on my controller

        [HttpPost]
      public ActionResult SalvarProdutos(DTO.Produtos todas_listas)
    {


        // Falta Incrementar
        return View();


    }

controller image inserted 2 records by modal

    
asked by anonymous 28.04.2018 / 01:53

1 answer

1

This is because your each selector is catching all tr s, including the first that is just the table header, where it does not contain the classes you capture, so the result of the 0 index will be null or undefined.

To avoid the first row of the table, use the method .not(":first") that the first row of the table is ignored:

$('#grid_cadastro tr').not(":first").each(function (index) {...
    
28.04.2018 / 02:33