feat: 网元类型记录数据
This commit is contained in:
@@ -186,3 +186,38 @@ export function parseTreeNodeKeys(
|
||||
}
|
||||
return treeIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析数据层级转级联options树结构
|
||||
*
|
||||
* @param data 数组数据
|
||||
* @param fieldType 读取节点label字段 默认 'type'
|
||||
* @param fieldLabel 读取节点label字段 默认 'name'
|
||||
* @param fieldValue 读取节点value字段 默认 'value'
|
||||
* @param fieldChildren 设置子节点字段 默认 'children'
|
||||
* @returns 层级数组
|
||||
*/
|
||||
export function parseDataToOptions(
|
||||
data: Record<string, any>[],
|
||||
fieldType: string = 'type',
|
||||
fieldLabel: string = 'label',
|
||||
fieldValue: string = 'value',
|
||||
fieldChildren: string = 'children'
|
||||
) {
|
||||
let options: Record<string, any>[] = [];
|
||||
|
||||
for (const v of data) {
|
||||
let vType = v[fieldType];
|
||||
let vLabel = v[fieldLabel];
|
||||
let vValue = v[fieldValue];
|
||||
const vData = { label: vLabel, value: vValue, ...v };
|
||||
const item = options.find(i => i.value === vType);
|
||||
if (item) {
|
||||
item[fieldChildren].push(vData);
|
||||
} else {
|
||||
options.push({ label: vType, value: vType, [fieldChildren]: [vData] });
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user