Salary DataTables jQuery

0

I do not handle much the DataTables for which I tried several ways to convert a result of ajax that are numbers and convert them into salary, but since I am handling it in a different way than the DataTables it suddenly becomes I'm going with the idea.

I would appreciate if you could solve my problem.

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");
        });  
    }
    
asked by JDavid 18.01.2017 в 23:22
source

1 answer

1

Try this, I hope it works for you

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

function listar_datos()
{   
	$.get('lista.php', function(data){
		if( data.success ){
			for( var $item in data.salarios ){
				console.log( $item );
				$('#tabla_salario > tbody:last-child').append('<tr><td>'+formatCurrency(data.salarios[$item])+'</td></tr>');
			}
			var tabla = $('#tabla_salario').DataTable({

				"columnDefs": [
					   {
						  "targets": [ 0 ],
						  "visible": true
					   },   
					]
			 });
		}
	},'json');
	
	
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	return ((sign) ? '' : '-') + '$' + num + '.' + cents;
}
});
<script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" type="text/css">

<table width="100%" class="table table-striped table-bordered table-hover" id="tabla_salario"> 
	<thead>
		<tr>
			<th>salario</th>
		</tr>
	</thead>
	<tbody>
		
	</tbody> 
</table>

lista.php - >

<?php
    $salarios = [];
    for($i = 0; $i < 1000; $i++){
        $salarios[] = mt_rand();
    }
    echo json_encode([ 'success' => true, 'salarios' => $salarios]);
    
answered by 19.01.2017 / 00:07
source