iview獲取全選和半選資料(方法一:基於樹型資料)
阿新 • • 發佈:2018-12-21
在用iview的時候發現iview的樹中獲取半選和全選的函式getCheckedAndIndeterminateNodes在我使用的iview版本里面是沒有提供的,
於是自己寫了一下獲取全選和半選節點的資料
this.halfCheckData = [] checkData = this.$refs.menuTree.getCheckedNodes() this.getHalfCheckData(checkData) let allCheckData = Array.from(new Set(checkData.concat(this.halfCheckData))) console.error('allCheckData', allCheckData.map((item) => item.name)) getHalfCheckData (checkData) { if (!checkData) { return } let halfData = [] let checkfidData = checkData.map((item) => { return item.fid }) checkfidData = Array.from(new Set(checkfidData)) checkfidData.forEach((item)=> { if (item) { this.halfCheckNode = {} this.findParent(item, this.menuTreeData) halfData.push(this.halfCheckNode) } }) if (halfData && halfData.length > 0) { this.getHalfCheckData(halfData) } },
findParent (id, tree) { for (let item of tree) {if (item.id === id) { this.halfCheckNode = item this.halfCheckData.push(item) return } if (item.children && item.children.length > 0) { this.findParent(id, item.children) } }
}