Skip to content
Advertisement

Need help by populating a PrimeReact Tree

I’m trying to populate a PrimeReact Tree with data from sqlite3 database, but it doesnt work.

Here is my SQL-Query:

select 'prj:'||p.id as key,p.name as label,(
select json_group_array(json_object('key',key,'label',label))
from(
    select 'tpr:'||tpr.id as key,tpr.name as label from tpr
    where tpr.prjid=p.id
    and tpr.active="true"
    order by tpr.id
    )
    ) as children
from prj p,usr_right r
where p.id=r.prjid
and p.active="true"
and r.usrid=$1
order by p.id

I get following JSON-Code:

[
{
key: 'prj:1',
label: 'Projekt 1',
children: '[{"key":"tpr:1","label":"Teilprojekt 1"},{"key":"tpr:2","label":"Teilprojekt 2"}]'
},
{ key: 'prj:2', label: 'Projekt 3', children: '[]' },
{ key: 'prj:3', label: 'Projekt 2', children: '[]' }
]

This is OK but i figured out, that the children are not rendered, because there are quotes around the square brackets at the children path – how can i fix this?

Advertisement

Answer

If initial is your json to be mapped to Tree, then you need to make a slight modification to convert string to array

let initial = [
{
key: 'prj:1',
label: 'Projekt 1',
children: '[{"key":"tpr:1","label":"Teilprojekt 1"},{"key":"tpr:2","label":"Teilprojekt 2"}]'
},
{ key: 'prj:2', label: 'Projekt 3', children: '[]' },
{ key: 'prj:3', label: 'Projekt 2', children: '[]' }
]

let converted = initial.map(obj => ({ ...obj, children: JSON.parse(obj.children)}))

console.log(converted)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement