我正试图用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=$1
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: '[]' }
]
这是可以的,但我发现,孩子没有渲染,因为有引号周围的方括号在孩子的路径-我怎么能解决这个问题?
如果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)