0

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 ?

1 Answer 1

1

I solved the problem :

var tabTitleColumn = [];

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

And after :

'aoColumns': tabTitleColumn 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.