Convert whole to monetary value in jQuery datatables

1

I found this function in DataTables :

render: $.fn.dataTable.render.number( ',', '.', 0, '$' )

for which I have tried several ways to inject that bit of code, but I have not found the form.

HTML:

<table id="tabla_salario" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
       <th>salario</th>
    </tr>
  </thead>
  <tbody>                                            
  </tbody>    
</table>

JQUERY / AJAX:

var tabla = $('#tabla_salario').DataTable({
    "columnDefs": [
             {
                 "targets": [ 0 ],
                 "visible": true
             }, 
         ],
     }); 



listar_datos();

function listar_datos()
{   
    $.ajax({
              url: 'listar_salario',
              type: 'POST'
          })
          .done(function(data)
          {
              var dato = $.parseJSON(data);

              for (var i = dato.length - 1; i >= 0; i--) 
              {
                  var rowNode = tabla
                  .row.add([ dato[i].salario, ])
                  .draw()
                  .node();
              }
          })
          .fail(function() {
              console.log("error");
          });  
      }  

This would be the example I need:

How could I incorporate it into my code?

    
asked by JDavid 19.01.2017 в 13:44
source

1 answer

1

Try to attach it to your code, but I decided it best to leave an example.

note: for your money to have cents, your number should be float

example:

12.34

12 represents $ 12 dollars

.34 represents 34 cents

var numero = 1250.223;
var dinero = '$' + numero.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

console.log(dinero);
    
answered by 19.01.2017 / 14:19
source