element-ui樹結構懶加載

在實際項目中,每每樹結構數據量較大,這時樹節點必須懶加載node

element-ui樹的懶加載:element-ui

<div style="width:100%;height:420px;overflow: auto;">
                        <el-tree lazy :load="getOrgList" :props="defaultProps" highlight-current @node-click="handleNodeClick">
                        </el-tree>
</div>

js代碼以下:ui

defaultProps: {
                    children: "children",
                    label: "name"
                },

樹節點形式爲:this

 [
        {
            "id": "1",
            "name": "國家電網",
            "status": null,
            "isCheck": null,
            "children": []
        }
    ]

 

getOrgList方法以下:spa

/**
             * 懶加載樹獲取組織機構子節點
             * element-tree使用方法
             * @param node:當前點擊節點信息
             * @param resolve:傳遞參數方法
             * */
            getOrgList(node,resolve) {
                /*this.$http.get(`${process.env.NODE_ENV}/admin/org/selectall`)
                    .then((response) => {
                        this.data2 = response.data.data;
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
*/
                let self = this;
                console.log(node);
                if(node.level == 0){
                    self.$http.get(`${process.env.NODE_ENV}/admin/org/selectall`).then(res => {
                        let treeData = []
                        res.data.data.forEach(e => {
                            treeData.push(e)
                        })
                        resolve(treeData);
                    }).catch(res => {
                        resolve([]);
                    })
                }else{
                    console.log("當前節點id值爲:"+node.data.id)
                    self.$http.get(`${process.env.NODE_ENV}/admin/org/selectall/${node.data.id}`).then(res => {
                        let myList = [];
                        res.data.data.forEach(e => {
                            myList.push(e)
                        })
                        resolve(myList);
                    }).catch(res => {
                        resolve([]);
                    })

                }

            },

 

handleNodeClick方法以下:code

                        // 點擊樹
            handleNodeClick(data) {
                console.log("點擊樹節點");
                console.log(data);
                this.clickTree = data;
            },                    
相關文章
相關標籤/搜索