格式化傳給iview的級聯選擇器的數據格式

####在 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;
        },
發佈了23 篇原創文章 · 獲贊 13 · 訪問量 2006
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章