通过填充 PrimeReact 树需要帮助

Need help by populating a PrimeReact Tree

我正在尝试使用 sqlite3 数据库中的数据填充 PrimeReact 树,但它不起作用。

这是我的 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=
order by p.id

我关注了 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: '[]' }
]

没关系,但我发现 children 没有呈现,因为 children 路径的方括号周围有引号 - 我该如何解决这个问题?

如果initial是你要映射到Tree的json,那么你需要稍微修改一下,将字符串转换成数组

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)