awlibs
awlibs copied to clipboard
请教一个问题
我正在做一个treeView结构在react中,使用rc-tree插件。 我从Database中提取的数据格式是这样的 { id: 1, name: 'L1', pId: 0 }, { id: 2, pId: 1, name: 'L2_1' }, { id: 3, pId: 2, name: 'L3_1' },
{ id: 4, pId: 1, name: 'L2_2' },
{ id: 5, pId: 4, name: 'L3_1' }
rc-tree需要的数据格式: { key: 1 title: 'L1', children: [ { key: 2 title: 'L2_1', children: [ { name: 'L3_1'} ] },
{
key: 3
title: 'L2_2',
children: [ { name: 'L3_1' } ]
},
]
}
]; 即:根节点标识的pId为0,且有多个根节点,请问是否可以使用您的插件得到将我从DB得到的数据转化为rc-tree需要的格式? 另外: 在npm install awlibs后,我从copy你的数据,但无法运行,结果如下: Failed to compile.
./node_modules/awlibs/tree/index.js Module parse failed: Unexpected token (14:8) You may need an appropriate loader to handle this file type. | function Tree(options) { | this.options = { | ...DEFAULT_OPTIONS, | ...options, | }; 请问,这是什么原因?
发布的是源码,有 ES6 的代码,需要转义后才能运行
/**
* 扁平表示的树形数据源
* 只有子 -> 父关系,没有父 -> 子关系
*/
var treeData2 = [
{ id: 1, name: 'L1' },
{ id: 2, pId: 1, name: 'L2_1' },
{ id: 3, pId: 2, name: 'L3_1' },
{ id: 4, pId: 1, name: 'L2_2' },
{ id: 5, pId: 4, name: 'L3_1' },
];
const treeIst2 = new Tree({
type: 'flattened',
idKey: 'id',
pIdKey: 'pId',
childrenKey: 'children',
data: treeData2,
});
const ret2 = treeIst2.getData();
console.log(ret2);
这个就是从扁平结构 -> 嵌套结构的转换