Fill datatables with dynamic columns

Fill datatables with dynamic columns

willeramwilleram Posts: 1Questions: 1Answers: 0

I'm working with datatables and I want to fill a table with dynamic columns.
The columns will be display according to the "Order" property.
The rows are mapped to the columns by the property "Id_Col" and the "RowIdx" corresponds to the index of the row.
I want to populate the datatable options :"columns" and "data"
My json object is as follows :

  {
"Col": [
    {
        "Id_Col": 1,
        "Col_Name": "Col 1",
        "Order": 1
    },
    {
        "Id_Col": 2,
        "Col_Name": "Col 2",
        "Order": 2
    },
    {
        "Id_Col": 3,
        "Col_Name": "Col 3",
        "Order": 3
    },
    {
        "Id_Col": 4,
        "Col_Name": "Col 4",
        "Order": 4
    }
],
"Row": [
    {
        "Id_Col": 1,
        "RowIdex": 1,
        "Value": "Row 1 col 1"
    },
    {
        "Id_Col": 2,
        "RowIdex": 1,
        "Value": "Row 1 col 2"
    },
    {
        "Id_Col": 3,
        "RowIdex": 1,
        "Value": "Row 1 col 3"
    },
    {
        "Id_Col": 4,
        "RowIdex": 1,
        "Value": "Row 1 col 4"
    },
    {
        "Id_Col": 1,
        "RowIdex": 2,
        "Value": "Row 2 col 1"
    },
    {
        "Id_Col": 2,
        "RowIdex": 2,
        "Value": "Row 2 col 2"
    },
    {
        "Id_Col": 3,
        "RowIdex": 2,
        "Value": "Row 2 col 3"
    },
    {
        "Id_Col": 4,
        "RowIdex": 2,
        "Value": "Row 3 col 4"
    },
    {
        "Id_Col": 1,
        "RowIdex": 3,
        "Value": "Row 3 col 1"
    },
    {
        "Id_Col": 2,
        "RowIdex": 3,
        "Value": "Row 3 col 2"
    },
    {
        "Id_Col": 3,
        "RowIdex": 3,
        "Value": "Row 3 col 3"
    },
    {
        "Id_Col": 4,
        "RowIdex": 3,
        "Value": "Row 3 col 4"
    }
    ]
 }

I created manually an array such as shown bellow and it's work for data, but I have some problems to create dynamically this object :
var data = [
["Row 1 - Col 1", "Row 1 - Col 2", "Row 1 - Col 3", "Row 1 - Col 4"],
["Row 2 - Col 1", "Row 2 - Col 2", "Row 2 - Col 3", "Row 2 - Col 4"],
["Row 3 - Col 1", "Row 3 - Col 2", "Row 3 - Col 3", "Row 3 - Col 4"]
];

I hope to have your help
Thank you in advance!

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Hi @willeram ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.