####在 iview-ui
中,級聯選擇器的數據格式是他們限制死的,但是後臺夥伴傳給我們的數據格式跟要求的格式不一致,只能自己動手去遍歷重構數據了,方法如下
// 定義的map爲後臺返給自己的數據key值,res.data爲後臺返回的數據,按照下面格式,調用格式化數據的方法
const map = {
label: "newsCategoryName",
children: "list",
value: "newsCategoryCode"
};
this.tableData= this.convertTree(res.data, map);
convertTree(tree, map) {
const result = [];
tree.forEach(item => {
// 讀取 map 的鍵值映射
item[map.children] = item[map.children]
? item[map.children]
: [];
let expand = false;
let label = item[map.label];
let value = item[map.value];
let children = item[map.children] ? item[map.children] : null;
// 如果有子節點,遞歸
if (children) {
children = this.convertTree(children, map);
}
result.push({ expand, label, value, children });
});
return result;
},