van-tree-select实现全选功能
- 建立一级分类和二级分类id对照表,方便后续操作各个id
data=[{id:101,text:一级分类,children:[
{id:101,text:'全部'},
{id:101001,text:'二级分类1'},
{id:101002,text:'二级分类2'}]
data.forEach((item)=>{
const subIdArr=[]
item.children.forEach((subItem)=>{
subIdArr.push(subItem.id)
})
this.sortIdMap[item.id]=subIdArr
})
- 全选功能实现函数
changeCatCodeArray(item) {
let activeIds=this.currentParams.catCodeArray;
if((''+item.id).length==3){
if(activeIds.includes(item.id)){
let _arr=activeIds.concat(this.sortIdMap[item.id])
activeIds = Array.from(new Set(_arr))
this.currentParams.catCodeArray=activeIds
}else{
this.sortIdMap[item.id].forEach((deleteItem)=>{
activeIds=activeIds.filter(useItem=>useItem!==deleteItem)
})
this.currentParams.catCodeArray=activeIds
}
}else{
let parentId=+(''+item.id).substring(0,3);
if(activeIds.includes(item.id)){
this.sortIdMap[parentId]=this.sortIdMap[parentId].filter(item=>item!==parentId)
if(this.subsetTo(this.sortIdMap[parentId],activeIds)){
activeIds.unshift(parentId)
this.currentParams.catCodeArray=activeIds
}
}else{
activeIds=activeIds.filter(item=>item!==parentId)
this.currentParams.catCodeArray=activeIds
}
}
},
subsetTo(arr1,arr2){
return arr1.every(v=>arr2.includes(v))
}