Jayce
Jayce

Reputation: 595

How to add columns dynamically to DataTables?

I would like add columns dynamically in DataTables.

I retrieve an array with the values for the title of my DataTables. For the first column, I want nothing and then I want to put my array with the values.

I use Ajax to retrieve the values for titles of DataTables in allyearstat11 .

Here is my javascript code :

function getStatistic11() {

var response;
var allstat11 = [];
var allyearstat11 = [];
var nbY = 20;

$.ajax({
    type: 'GET',
    url: 'http://localhost:52251/Service1.asmx/Statistic_11',
    data: "nbYear='" + nbY + "'",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
        response = msg.d;
        for (var i = 0; i < response.Items.length; i++) {
            allstat11[i] = new Array(nbY);
            var j = 0;
            allstat11[i][j] = response.Items[i].Interventie;
            var t = 1;
            while (j <= nbY) {

                allstat11[i][t] = response.Items[i].Sum[j];
                t++;
                j++;
            }                
        }
        for (var k = 0; k <= nbY; k++) {
            allyearstat11[k] = response.Items[0].YearStart + k;
        }
        fillDataTable11(allstat11, allyearstat11);

    },
    error: function (e) {
        alert("error loading statistic 11");
    }
});
}

Here is my javascript code that fills the DataTables, it works well but manually

function fillDataTable11(data, allyearstat11) {

if ($("#table_statistic_11").css("visibility") == "hidden")
    $("#table_statistic_11").css("visibility", "visible");

$('#table_statistic_11').dataTable({

    'aaData': data,
    'aoColumns': [
        { "sTitle": "", "sCellType": "th", "fnCreatedCell": function (cell) { cell.scope = 'row'; } },
        { "sTitle": allyearstat11[0] },
        { "sTitle": allyearstat11[1] },
        { "sTitle": allyearstat11[2] },
        { "sTitle": allyearstat11[3] },
        { "sTitle": allyearstat11[4] },
        { "sTitle": allyearstat11[5] },
        { "sTitle": allyearstat11[6] },
        { "sTitle": allyearstat11[7] },
        { "sTitle": allyearstat11[8] },
        { "sTitle": allyearstat11[9] },
        { "sTitle": allyearstat11[10] },
        { "sTitle": allyearstat11[11] },
        { "sTitle": allyearstat11[12] },
        { "sTitle": allyearstat11[13] },
        { "sTitle": allyearstat11[14] },
        { "sTitle": allyearstat11[15] },
        { "sTitle": allyearstat11[16] },
        { "sTitle": allyearstat11[17] },
        { "sTitle": allyearstat11[18] },
        { "sTitle": allyearstat11[19] },
        { "sTitle": allyearstat11[20] }
    ],

    "iDisplayLength": 12,
    "bJQueryUI": true,
    "bDestroy": true,
    "bPaginate": false,
    "bLengthChange": false,
    "bFilter": false,
    "bSort": false,
    "bInfo": false,
    "bAutoWidth": false
});
}

Can I use for loop in "aoColumns"? How do I do ?

Upvotes: 0

Views: 522

Answers (1)

Jayce
Jayce

Reputation: 595

I solved the problem :

var tabTitleColumn = [];

for (var i = 0; i < allyearstat11.length; i++) {
    tabTitleColumn.push({
        "sTitle": allyearstat11[i],
    });
};

And after :

'aoColumns': tabTitleColumn 

Upvotes: 1

Related Questions