Use function in datatables

Asked

Viewed 95 times

-1

I have this function of form when placing the price, automatically replaces the zeros for the values placed and keeps the fixed point:

$(function() {
  $('.Preco2').maskMoney({ decimal: '.', thousands: ' ', precision: 2 });
})

In the html do it this way:

<input type="text" class="form-control Preco2" name="Preco2[]" value="0.00">

But I use the function datatables where it shows 5 lines per page:

(function ($) {$(document).ready(function () {

    $('#employee_ta22').DataTable();
    responsive: true
} );
$('#employee_ta22').dataTable({                           
 "pagingType": "full_numbers",
        "iDisplayLength": 5,
        "oLanguage": {
    "sProcessing": "Aguarde enquanto os dados são carregados ...",
    "sLengthMenu": "Mostrar _MENU_ registos por página",
    "sZeroRecords": "Nenhum registo correspondente ao criterio encontrado",
    "sInfoEmtpy": "Exibindo 0 a 0 de 0 registos",
    "sInfo": "Exibindo de _START_ a _END_ de _TOTAL_ registos",
    "sInfoFiltered": "",
    "sSearch": "<span class='glyphicon glyphicon-search'></span>",
    "oPaginate": {
       "sFirst":    "<span class='glyphicon glyphicon-fast-backward'></span>",
       "sPrevious": "<span class='glyphicon glyphicon-backward'></span>",
       "sNext":     "<span class='glyphicon glyphicon-forward'></span>",
       "sLast":     "<span class='glyphicon glyphicon-fast-forward'></span>"
     }
    }
});
})(jQuery); 

The problem is that the class preco2 only works on the first page (in the first 5 lines) when it returns the table, on the second page and on the others it stops working.

1 answer

1


Its function searches for all elements .Preco2 and apply the mask to these elements.

Obviously this mask is only applied to existing elements in your document, if eventually a new .Preco2 for rendered on page, you will have to apply the mask again.

The DataTables offers you some events to handle this, you can create a Handler in the event draw, which is invoked when the DataTables render the page, and then apply the mask again to those new elements that have been rendered.

Example:

$('#employee_ta22').DataTable().on('draw', () => {
    $('.Preco2').maskMoney({
        decimal: '.',
        thousands: ' ',
        precision: 2
    });
});

Browser other questions tagged

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