Editable table with jquery and php?

3

I'm trying to make a PivotTable for editing the data without refresh, but by double-clicking the line nothing happens, it follows the JQuery code


$(document).ready(function(){
    $('#tblEditavel tbody tr td.editavel').dblclick(function(){
        if($('td > input').length > 0){
            return;
        }
        var conteudoOriginal = $(this).text();
        var novoElemento = $('', {type: 'text', value:conteudoOriginal});
        $(this).html(novoElemento.bind('blur keydown', function(e){
            var keyCode = e.which;
            var conteudoNovo = $(this).val();
            if(keyCode == 13 && conteudoNovo != '' && conteudoNovo != conteudoOriginal){
                var objeto = $(this);
                $.ajax({
                    type:"POST",
                    url:"alterar.php",
                    data:{
                        id:$(this).parents('tr').children().first().text(),
                        campo:$(this).parent().attr('title'),
                        valor:conteudoNovo
                    }
                    success:function(result){
                        objeto.parent().html(conteudoNovo);
                        $('body').append(result);
                    }
                })
            }
            else if( keyCode == 27 || e.type == 'blur')
                $(this).parent().html(conteudoOriginal);
        }));
        $(this).children().select();
    }

})
})

Just below the html code

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scripttype="text/javascript" src="atualiza.js"></script>
    <style>
        table{
            border-collapse: collapse;
        }
        table, td, th{
            border: 1px solid black;
            padding: 5px;

        }
    </style>
</head>
<body>
    <table id="tblEditavel">
        <thead>
            <tr>
                <th>Id</th>
                <th>Nome</th>
                <th>Valor</th>
                <th>Vencimento</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>74</td>
                <td title="Nome" class="editavel">Fatura 45 Jhovini</td>
                <td title="valor" class="editavel">2.350,00</td>
                <td title="vencimento" class="editavel">10/02/2017</td>
            </tr>
            <tr>
                <td>75</td>
                <td title="Nome" class="editavel">Fatura 45 Jhovini</td>
                <td title="valor" class="editavel">2.350,00</td>
                <td title="vencimento" class="editavel">10/03/2017</td>
            </tr>
            <tr>
                <td>76</td>
                <td title="Nome" class="editavel">Fatura 45 Jhovini</td>
                <td title="valor" class="editavel">2.350,00</td>
                <td title="vencimento" class="editavel">10/04/2017</td>
            </tr>
            <tr>
                <td>77</td>
                <td title="Nome" class="editavel">Fatura 45 Jhovini</td>
                <td title="valor" class="editavel">2.350,00</td>
                <td title="vencimento" class="editavel">10/05/2017</td>
            </tr>
            <tr>
                <td>78</td>
                <td title="Nome" class="editavel">Fatura 45 Jhovini</td>
                <td title="valor" class="editavel">2.350,00</td>
                <td title="vencimento" class="editavel">10/06/2017</td>
            </tr>
        </tbody>
    </table>

</body>

The php only sends to the server, where I'm wrong, it was working before but after I inserted ajax nothing works and I did not know how to go back to the previous file.

    
asked by anonymous 31.01.2017 / 20:26

1 answer

3

I was able to solve with the help of SOUS @Ionut and I decided to share if anyone needs it in the future, there was an extra closing and a comma was missing before success, I hope I have helped somebody.

$(document).ready(function() {
  $('#tblEditavel tbody tr td.editavel').dblclick(function() {
      if ($('td > input').length > 0) {
        return;
      }
      var conteudoOriginal = $(this).text();
      var novoElemento = $('<input/>', {
        type: 'text',
        value: conteudoOriginal
      });
      $(this).html(novoElemento.bind('blur keydown', function(e) {
        var keyCode = e.which;
        var conteudoNovo = $(this).val();
        if (keyCode == 13 && conteudoNovo != '' && conteudoNovo != conteudoOriginal) {
          var objeto = $(this);
          $.ajax({
            type: "POST",
            url: "#",
            data: {
              id: $(this).parents('tr').children().first().text(),
              campo: $(this).parent().attr('title'),
              valor: conteudoNovo
            }, //added this comma here
            success: function(result) {
              objeto.parent().html(conteudoNovo);
              $('body').append(result);
            }
          })
        } else if (keyCode == 27 || e.type == 'blur'){
          $(this).parent().html(conteudoOriginal);
          }
      }));
      $(this).children().select();
    
        //} removed the extra } from here.
    });

  })
table {
  border-collapse: collapse;
}
table,
td,
th {
  border: 1px solid black;
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tableid="tblEditavel">
  <thead>
    <tr>
      <th>Id</th>
      <th>Nome</th>
      <th>Valor</th>
      <th>Vencimento</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>74</td>
      <td title="Nome" class="editavel">Conta de Telefone</td>
      <td title="valor" class="editavel">2.350,00</td>
      <td title="vencimento" class="editavel">10/02/2017</td>
    </tr>
    <tr>
      <td>74</td>
      <td title="Nome" class="editavel">Conta de água</td>
      <td title="valor" class="editavel">235,00</td>
      <td title="vencimento" class="editavel">18/02/2017</td>
    </tr>

  </tbody>
</table>
    
01.02.2017 / 14:45