Are there any examples of creating columns dynamically?

Are there any examples of creating columns dynamically?

ritcher1ritcher1 Posts: 1Questions: 1Answers: 0

I'm new to datatables, and have the need to create columns when I don't know the column info ahead of time - it will be passed to the javascript. We will have a small number of static columns (first name & last name for example), and an array with zero-to-many additional values that need to display in the table. A simplified example of the data coming in would be

1: First Name: Bob; Last Name: Clark; [{Name: Expense 1; Value: 100.50}, {Name: Expense 2; Value: 300.75}]
2: First Name: Sue; Last Name: Richards; [{Name: Expense 1; Value: 0.0}, {Name: Expense 2; Value: 540.00}]

Are there any examples someone could point me to of how to dynamically add the columns in the array using "Name" as the column header and "Value" as the value. So it looks something like

First Name | Last Name | Expense 1 | Expense 2
Bob | Clark | 100.50 | 300.75
Sue | Richards | 0.0 | 540.00

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    I put this simple example together that pulls the column names from the field names returned in the AJAX response.
    http://live.datatables.net/fafuyeyu/1/edit

    I've seen where the column names are returned in a different object, parsed and applied to the columns. My example is just to show one way it could be done. You will want to look at the following docs for the example:

    columns.data
    columns.title
    data

    You will also want to read the Datatables Data documentation.

    You can have nested objects or nested arrays. You will need to adjust your nested portion ([{Name: Expense 1; Value: 100.50}, {Name: Expense 2; Value: 300.75}]) to meet the requirements of Datatables.

    Let us know if you have further questions.

    Kevin

This discussion has been closed.