Popular DataTables through a JSON variable

0

I have the following function which returns data from a query in the database and stores it in json format in the msg variable.

$.ajax({
        type: 'POST',
        dataType: 'html',
        url: page,

        data: {idtabela: idtabela},
        success: function (msg) {
            msg = $.parseJSON(msg);
 }
});

In my front-end file, I need this table to be popular. I am using the DataTables plugin

<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th>Cliente</th>
                                        <th style="width: 50px">Carro</th>
                                        <th style="width: 50px">Data</th>
                                        <th style="width: 150px">Ações</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    <tr class="odd gradeX">
                                        <td>> </td>
                                        <td> </td>
                                        <td> </td>
                                        <td style="text-align: center">
                                            <a class="btn btn-primary btn-sm" href="edit.php?id=1" role="button">Editar</a>
                                            <input type="button" value="Excluir" class="btn btn-danger btn-sm""/>
                                        </td>
                                    </tr>

                                </tbody>
     </table>

jquery function for the datatables plugin

$(document).ready(function() {
    $('#dataTables-example').DataTable({
        language:{
            url: '../includes/datatable_ptbr.json'
        },
        responsive: true
    });
});
    
asked by anonymous 17.01.2018 / 19:44

1 answer

0

The most advisable is to refactor to make the ajax request in the datatable even after the manual: link

p>
$(document).ready(function() {
  $('#example').DataTable( {
    "ajax": "data/objects.txt",
      "columns": [
        { "data": "name" },
        { "data": "position" },
        { "data": "office" },
        { "data": "extn" },
        { "data": "start_date" },
        { "data": "salary" }
     ]
   });
 });
    
17.01.2018 / 21:11