How to Add Mask in ajax input via jquery?

Asked

Viewed 390 times

1

Hello guys I have a function that edits the table dynamically, but the data contained in it has formatting coming from the database, but when I double click it does not remain with the mask. the table coming from the bank is this inserir a descrição da imagem aqui

I can edit the information and give enter it saves the information and goes to the bottom line to edit the next information, but saved without formatting, I wanted to format only the value field and date field, I tried with .mask() and with .unmask but it didn’t work

look at the example below her working

$(document).ready(function () {
    $('#tblEditavel tbody tr').each(function (i) {
        $(this).children('td').each(function (p) {
           
            $(this).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: "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);
                            }
                        })
                        var posicao = p + 1;
                        $(this).parent()
                                .html(conteudoNovo)
                                .parents('tr')
                                .next()
                                .children('td:nth-child(' + posicao + ')')
                                .trigger('dblclick');

                    } else if (keyCode == 27 || e.type == 'blur')
                        $(this).parent().html(conteudoOriginal);

                }));
                $(this).children().select();
            });
        });
    });
})
table{
                border-collapse: collapse;
            }
            table, td, th{
                border: 1px solid black;
                padding: 5px;
                
            }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<table id="tblEditavel">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Nome</th>
                    <th>Valor Parcela</th>
                    <th>Vencimento</th>
                    <th>Parcela</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>74</td>
                    <td title="Nome" class="editavel">Fatura 45 Fornecedor B</td>
                    <td title="valor" class="editavel vlr">2.350,00</td>
                    <td title="vencimento" class="editavel dt">10/02/2017</td>
                    <td>1 / 3</td>
                </tr>
                <tr>
                    <td>75</td>
                    <td title="Nome" class="editavel">Fatura 45 Fornecedor B</td>
                    <td title="valor" class="editavel vlr">2.350,00</td>
                    <td title="vencimento" class="editavel dt">10/03/2017</td>
                    <td>2 / 3</td>
                </tr>
                <tr>
                    <td>76</td>
                    <td title="Nome" class="editavel">Fatura 45 Fornecedor B</td>
                    <td title="valor" class="editavel vlr">2.350,00</td>
                    <td title="vencimento" class="editavel dt">10/04/2017</td>
                    <td>3 / 3</td>
                </tr>
            </tbody>
        </table>

1 answer

2


Utilize jquery-maskmoney and jquery-Mask and ask a question before adding the element by the attribute title that was clicked:

Example

$(document).ready(function() {
  $('#tblEditavel tbody tr').each(function(i) {
    $(this).children('td').each(function(p) {

      $(this).dblclick(function() {
        if ($('td > input').length > 0) {
          return;
        }
        var conteudoOriginal = $(this).text();
        var novoElemento = $('<input/>', {
          type: 'text',
          value: conteudoOriginal
        });
        if ($(this).attr('title')=='valor')
        { 
           $(novoElemento)
              .maskMoney({decimal:',',thousands:'.'});
        }
        if ($(this).attr('title')=='vencimento')
        { 
           $(novoElemento)
              .mask("99/99/9999");
        }
        $(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);
              }
            })
            var posicao = p + 1;
            $(this).parent()
              .html(conteudoNovo)
              .parents('tr')
              .next()
              .children('td:nth-child(' + posicao + ')')
              .trigger('dblclick');

          } else if (keyCode == 27 || e.type == 'blur')
            $(this).parent().html(conteudoOriginal);

        }));
        $(this).children().select();
      });
    });
  });
})
table {
  border-collapse: collapse;
}
table,
td,
th {
  border: 1px solid black;
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>

<table id="tblEditavel">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Nome</th>
                    <th>Valor Parcela</th>
                    <th>Vencimento</th>
                    <th>Parcela</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>74</td>
                    <td title="Nome" class="editavel">Fatura 45 Fornecedor B</td>
                    <td title="valor" class="editavel vlr">2.350,00</td>
                    <td title="vencimento" class="editavel dt">10/02/2017</td>
                    <td>1 / 3</td>
                </tr>
                <tr>
                    <td>75</td>
                    <td title="Nome" class="editavel">Fatura 45 Fornecedor B</td>
                    <td title="valor" class="editavel vlr">2.350,00</td>
                    <td title="vencimento" class="editavel dt">10/03/2017</td>
                    <td>2 / 3</td>
                </tr>
                <tr>
                    <td>76</td>
                    <td title="Nome" class="editavel">Fatura 45 Fornecedor B</td>
                    <td title="valor" class="editavel vlr">2.350,00</td>
                    <td title="vencimento" class="editavel dt">10/04/2017</td>
                    <td>3 / 3</td>
                </tr>
            </tbody>
        </table>

  • 1

    perfect was exactly what I was needing, another thing that now started and get me the vein was the fact that all the columns of the table are opening for editing, before I was using #tblEditabel tr tr.editavel edit only the columns of the class . editable, but since I needed to interact to use the enter function I couldn’t make it work anymore, have any idea? but what you did was spectacular already this to a day trying.

  • I didn’t quite understand the new question, who knows a new question relating to this. @Wagnerfernandomomesso

Browser other questions tagged

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