Getting column names and data from ajax source and json.

Getting column names and data from ajax source and json.

infocontrollerinfocontroller Posts: 1Questions: 0Answers: 0
edited April 2019 in Free community support

After much work and several hours lost, I was finally able to load columns and data dynamically. I'll post here for discussion. Sorry for my English ...

Basically what I did was create two requests, one json for the columns and another ajax for the data.

The great advantage is that the table.ajax.reload(); function works perfectly.

function r(r){
    
    

    table(r);

        
    $.post('controller/Controller.php',
        {
            action: r,              
        },
        function(data, textStatus) {
            
    var my_columns = [];

    $.each(data[0], function(key, value) {
    
    
    var my_item = {};
    my_item.data = key;
    my_item.title = key;
    
    if (my_item.title=='FUNCAO'){my_item.title='FUNÇÃO'};

    my_columns.push(my_item);
  });           
    
$('#r_table').DataTable( {

        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": false,
        "responsive": true,
        "autoWidth": false,
        "pageLength": 10,
        "columnDefs": [
            { "visible": false, "searchable": false, "targets": 0 },
            { "className": "dt-center", "targets": 1 },
        ],
        
          "ajax": {
          "data": {"action": "ajax"},
          "url": "controller/Controller.php",
          "type": "POST",
            },
      
        "columns": my_columns,
      
        "language": {
    "sEmptyTable": "Nenhum registro encontrado",
    "sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
    "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
    "sInfoFiltered": "(Filtrados de _MAX_ registros)",
    "sInfoPostFix": "",
    "sInfoThousands": ".",
    "sLengthMenu":     "Mostrar _MENU_ resultados por página",
    "sLoadingRecords": "Carregando...",
    "sProcessing": "Processando...",
    "sZeroRecords": "Nenhum registro encontrado",
    "sSearch": "Pesquisar",
    "oPaginate": {
        "sNext": "Próximo",
        "sPrevious": "Anterior",
        "sFirst": "Primeiro",
        "sLast": "Último"
    },
    "oAria": {
        "sSortAscending": ": Ordenar colunas de forma ascendente",
        "sSortDescending": ": Ordenar colunas de forma descendente"
    }
        },  
    });     

        }, 
        "json"      
    );
            
}

This discussion has been closed.