Vue el-tree組件,得到后台數據渲染時,選擇父級會全選所有的子級 解決辦法


效果:

解決辦法:

check-strictly:在顯示復選框的情況下,是否嚴格的遵循父子不互相關聯的做法,默認為 false。

  1、設置check-strictly 是一個變量checkStrictly,默認值是false    

   <el-tree 
                :data="rolesTree"
                show-checkbox
                default-expand-all
                ref="rolestree"
                node-key="Id"
                :default-checked-keys="defaultCheckedNode"
                 :check-strictly="checkStrictly"
                :expand-on-click-node="false"
                highlight-current
                :props="defaultProps"
                @check="handleNodeCheck">
        </el-tree>
     checkStrictly:false,

  2、獲取數據后,

        getRolesTree (params) { 
            role.getRolesTree(params).then(res => {
                this.rolesTree = res.data.Data;  
                this.checkStrictly = true,  //給樹節點賦值之前,先設置為true
                this.$nextTick(() => {
                    setTimeout(()=>{                     
                        //渲染
                        this.defaultCheckedNode = this.getArray(this.rolesTree, true, []);                           
                        this.$refs.rolestree.setCheckedNodes(this.defaultCheckedNode);   //給樹節點賦值
                        this.checkStrictly = false;    //賦值完成后設置為false
                    },0)
                })
            })
        },
        getArray (data, name, lst) {
            var tempArr = lst, _this = this;
            data.map(item => {             
                if (item.Disabled == name) {                
                    tempArr.push(item.Id);
                    if (!!item.Children && (typeof item.Children) == "object") {
                        _this.getArray(item.Children, name, tempArr);
                    }

                } else {
                    if (!!item.Children && (typeof item.Children) == "object") {
                        _this.getArray(item.Children, name, tempArr);
                    }

                }
            })
            return tempArr
        },
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM