How to generate dynamic columns based on the dataset

How to generate dynamic columns based on the dataset

althafalthaf Posts: 5Questions: 3Answers: 0
edited April 2018 in Free community support

I need to generate dynamic columns based on the dataset. Some times my dataset has 3 columns and sometimes it has 5 columns based on the dropdown selection.
In that case how we can render the data table.
Below is my code for the dataset with 3 columns. How to use the same code for the dataset with 5 columns.

 function RenderDataTable(dataset) {

     var table = $('#dsexample').DataTable({
         destroy: true,
         data: dataset,
         "orderClasses": false,
         //fixedColumns: true,
         columns: [
             {  
                 title: "Unique ID",
                 "width": "100"
             },
             {
                 title: "Level1",
                 "width": "200"
             },
             {
                 title: "Level2",
                 "width": "200"
             }
         ]
     })
 }

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @althaf ,

    Take a look at this example - here it's defining the columns in a variable which is then referenced in the table initialisation.

    You could do the same - just create the array first with the number of columns that you want, whether that be 3 or 5 as you used in your example, then pass that into the table initialisation.

    That should do it for you,

    Cheers,

    Colin

  • althafalthaf Posts: 5Questions: 3Answers: 0

    Great Helo Colin. Thanks. You save my day.

This discussion has been closed.