teikuei
teikuei

Reputation: 311

Make variable number of columns in datatables

I'm trying to make a table and need to do something like this in the configuration of the datatable:

    $(document).ready(function() {
            var conta_col = document.getElementById("input_x").value;
            var table = $('#dataart').DataTable( {
              ....
              ....  
                "columns": [ 
                    {"width": "10%"},
                    {"width": "35%"},
                    {"width": "10%"},
                    {"width": "6%"},
                    {"width": "5%"},
                    while (conta_col >= 1) {
                        {"width": "5%"};
                        conta_col--;
                    },
                    {"width": "14%"}
                ],
...

Is it posible??? thank's.

Upvotes: 2

Views: 505

Answers (1)

Tibrogargan
Tibrogargan

Reputation: 4603

The value you're giving as the value of the "columns" key in your data table is just an array.

var columns = 
     [ {"width": "10%"}
     , {"width": "35%"}
     , {"width": "10%"}
     , {"width": "6%"}
     , {"width": "5%"}
     ];
while (conta_col >= 1) {
    columns.push( { "width" : "5%" }; )
    conta_col--;
};
columns.push( {"width": "14%"} );
...
var table = ${'#dataart').DataTable( {
   ...
   "columns" : columns,
   ...
} );
...

Upvotes: 3

Related Questions