Doubt to make a Json

1

Hello, I have the following Json.

AndI'dliketoshowthetotalvalueina'p',I'musingthedatatablestoshowthevaluesofthe'date'.I'musingthe link as an example. And to mount the table I use (just an example, mine is equal only with the data I want):

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": "../ajax/data/objects.txt",
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );

// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function () {
    var tr = $(this).closest('tr');
    var row = table.row( tr );

    if ( row.child.isShown() ) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child( format(row.data()) ).show();
        tr.addClass('shown');
    }
} );

The html of the table looks like this:

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Salary</th>
        </tr>
    </thead></table>

I would like to add a 'p' at the end of the table with this total value. But I do not know how to create this. Thanks in advance.

    
asked by anonymous 08.03.2017 / 12:36

1 answer

0

Friend,

You can capture the data returned in ajax and handle this outside the dataTable.

So I understand you want to create a paragraph after the table containing the value, right?

Try this:

table.on( 'xhr', function () {
    var json = table.ajax.json();
    console.log(json);
} );

For your JSON example it would look something like this:

table.on( 'xhr', function () {
    var json = table.ajax.json();
    document.getElementById('meuParagrafo').innerText = json.valorTotal;
} );
    
09.03.2017 / 05:56