columns generated dynamically

columns generated dynamically

gmstephanegmstephane Posts: 4Questions: 3Answers: 0

Hello
I have this code
```function initDataTable(annee_inv_sel) {

$('#table_data').DataTable({
    "paging": true,
    "scrollX": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "info": true,
    "responsive": false,
    "autoWidth": false,
    "pageLength": 10,
    "ajax": {
        "url": "data_activ_year_sel.php",
        "type": "GET",
        "data": {
            "annee_inv_sel": annee_inv_sel
        }
    },
    "columns": [
        { "data": "rank" },
        { "data": "annee_tempo" },
        { "data": "gaz_naturel" },
        { "data": "gasoil_diesel" },
        { "data": "fiouls_residuel" },
        { "data": "bois_dechet" }
    ],
    // fonction pour retourner un message personnalisé en cas de données absentes
    "drawCallback": function(settings) {
        var api = this.api();
        var rows = api.rows().count();
        if (rows === 0) {
            // Aucune donnée retournée, effectuez votre traitement ici
            console.log("Aucune donnée retournée");
            // Par exemple, vous pouvez afficher un message d'erreur ou masquer la table
            //$("#output2").html("Aucune donnée n'a été trouvée.");
        }
    },
    "language": {
        "emptyTable": "Aucune donnée disponible dans le tableau"
    },
    dom: 'Bfrtip',
    lengthMenu: [
        [1, 5, 10, 25, 50, -1],
        ['1 entrée/page', '5 entrées/page', '10 entrées/page', '25 entrées/page', '50 entrées/page', 'Montrer tout']
    ],
    buttons: [
        'pageLength',
        'excel'
    ],
    "fixedHeader": {
        "header": true,
        "footer": true
    },
    "language": {
        "url": "http://cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
    }
});

}

and the JSON return by data_activ_year_sel.php is like:
```{"data":[{"rank":1,"annee_tempo":"1990","gaz_naturel":"0","gasoil_diesel":"155","fiouls_residuel":"55.7228","bois_dechet":"0"},{"rank":2,"annee_tempo":"1991","gaz_naturel":"0","gasoil_diesel":"158","fiouls_residuel":"61.6931","bois_dechet":"0"},{"rank":3,"annee_tempo":"1992","gaz_naturel":"0","gasoil_diesel":"162","fiouls_residuel":"56.7177","bois_dechet":"0"},{"rank":4,"annee_tempo":"1993","gaz_naturel":"0","gasoil_diesel":"174","fiouls_residuel":"53.733","bois_dechet":"0"}]}

My question is ho to generate columns dynamically from the response of the server (JSON )

Answers

Sign In or Register to comment.