Search code examples
jsonloopsangular7primengprimeng-datatable

Pass json string having dynamic data as a source to primeNG datatable


I'll be getting json dynamically from service as follows:

{"items":[[{"key": "SerialID","value": "P1.M1.T1"},{  "key": "Description",  "value": "Dummy Desc 1"},{  "key": "Label",  "value": "A123"}],[{"key": "SerialID","value": "P1.M1.T2"},{  "key": "Description",  "value": "Dummy Desc 2"},{  "key": "Label",  "value": "B123"}]]}

Here, sample 2 rows in items array are given. I would get multiple such rows. Also, columns in every row may very (e.g.: Here, I am receiving 3 columns per row. I might receive n number. However, it'll be same for all rows in input).

I want to pass this json as a data source to primeNG table and render it in tabular format. So both, columns and rows, need to be generated dynamically. I tried parsing the json but somehow not able to form appropriate inputs for primeNG table.

Any help is highly appreciated.


Solution

  • I finally iterated my JSON, formed column and row arrays in the format which PrimeNG expects and passed it to component. It worked!