Function error in jquery function .keypress [duplicate]

0

Then the problem is that when pressing the enter key inside the cell, some error occurs when the whole table disappears from the page.

function criaTabela() {
    $("#tbl-datas-prazo-convocacao tbody tr").remove();
    $("#tbl-datas-prazo-convocacao thead tr").remove();

    var semestre = document.getElementById('combobox-semestre').value;

    $.ajax({
        type: "POST",
        url: "../plugins/ctz/asp/ServerFunctionsCtz.aspx/dsc_vestibular_ctzap",
        crossDomain: false,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({
            'semestre': parseInt(semestre)
        }),
        dataType: "json",
        success: function(data) {
            var corpo = document.getElementById('corpotabela');
            if (data.d != null && corpo.rows.length == 0) {
                var cabecalho = "<tr><th>Descrição Vestibular</th><th>dat 1</th><th>dat 2</th><th>dat 3</th><th>dat 4</th></tr>";
                $('#headtabela').append(cabecalho);
                for (var i = 0; i < data.d.length; i++) {
                    var linha = "<tr>";
                    linha += "<td >" + data.d[i][0] + "</td>";
                    if (data.d[i][1] != "") linha += "<td>" + data.d[i][1] + "</td>";
                    else {
                        linha += "<td contenteditable>Célula vazia</td>";
                    }
                    if (data.d[i][2] != "") linha += "<td>" + data.d[i][2] + "</td>";
                    else {
                        linha += "<td contenteditable>Célula vazia</td>";
                    }
                    if (data.d[i][3] != "") linha += "<td>" + data.d[i][3] + "</td>";
                    else {
                        linha += "<td contenteditable>Célula vazia</td>";
                    }
                    if (data.d[i][4] != "") linha += "<td>" + data.d[i][4] + "</td>";
                    else {
                        linha += "<td>Célula vazia</td>";
                    }
                    //else {linha += "<td><input type='text' value='célula vazia'></td>"; }

                    //linha += "<td>"+exibecaixa.replace("idelemento", "id = 'combobox-vinculo" + i + "'")+"</td>";                         

                    // add nota: salve antes de mudar o semestre
                    linha += "</tr>";
                    $('#corpotabela').append(linha);
                    $('#combobox-vinculo' + i).val(data.d[i][2]);
                }
                quantidadeLinhas = document.getElementById('tbl-datas-prazo-convocacao').rows.length;
                quantidadeLinhas = quantidadeLinhas - 1;

                // Função
                $(function() {

                    //Função click na celula
                    $('td').click(function() {
                        //Salva o conteudo da celula                            
                        var conteudoOriginal = $(this).text();

                        //$(this).html("<input type='text' value='" + conteudoOriginal + "' />");

                        //Função toggleClass Troca as classes                               
                        $(this).toggleClass("Comfoco");
                        // Salva a edição ao apertar enter                          
                        $(this).keypress(function(event) {
                            if (event.which == 13) {
                                var novoConteudo = $(this).val();
                                $(this).text(novoConteudo);
                                $(this).toggleClass("teste2");
                            }
                        });
                        // Ao perde o foco retira a classe vermelha                         
                        $(this).blur(function() {
                            $(this).toggleClass("teste2");
                            $(this).text(conteudoOriginal);
                        });
                        //
                    });
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError);
        }
    });
}
    
asked by anonymous 12.03.2018 / 16:23

1 answer

0

The contenteditable makes keypress possible in td itself. You would not need to create input as your friend commented on the question.

But the enter key will skip line, so the table should be disappearing or something. I suggest putting a command to bar the normal operation of the enter key at the end of the function:

$(this).keypress(function(event) {
    if (event.which == 13) {
        var novoConteudo = $(this).val();
        $(this).text(novoConteudo);
        $(this).toggleClass("teste2");
    }
    event.stopPropagation();
});

The val() will probably have to be html() .

For more details and a more correct answer, I suggest you put a functional example with the HTML code.

    
12.03.2018 / 19:36