/** * 用于动态表结构的 tableProps 生成 * @param {*} nameData * @returns */ export function handleNameData(nameData) { const props = step1(nameData.filter((item) => item.tree == 1)); step2( props, nameData.filter((item) => item.tree == 2) ); // console.log('level 1', JSON.stringify(props, null, 2)); return props; } function step1(tree1) { return Array.from(new Set(tree1.map((item) => item.name))) .sort() .map((item) => ({ prop: item, label: item, align: 'center', children: [], })); } function step2(firstTierProps, tree2) { tree2.map((nd) => { const parent = firstTierProps.find( ({ prop }) => nd.parentId.indexOf(prop) > -1 ); if (notRepeat(parent.children, nd.name)) { parent.children.push({ label: nd.name, prop: `${parent.prop}-${nd.name}`, align: 'center', }); } }); } function notRepeat(propArray, name) { return propArray.every((item) => item.label !== name); } /** * 用于调整服务器返回的动态数据 * @param {*} dynamicData * @returns */ export function handleDynamicData(dynamicData) { return dynamicData.map((dd) => { const initData = { inspectionContent: dd.inspectionDetContent, }; dd.data.forEach((column) => { column.children.forEach((ch) => { initData[`${column.dynamicName}-${ch.dynamicName}`] = ch.dynamicValue; }); }); return initData; }); }