** 如图,一级和二级是后端接口返回的,只需要把对应的key名改成value、label和children即可,第三级的内容是根据一级二级去请求接口返回的,所以需要注意以下问题**
- 一级和二级确定都是由内容的,三级不确定,先要判断到了二级是否停止,用到组件中的leaf(true:代表停止,即没有下一级,没有右边的箭头,false代表下一级还有内容)
- 第三级的选项
<el-cascader ref="cascader" :props="props" clearable @change="handleChange" checkStrictly: true />
- 首先
:props="props"
checkStrictly: true
:代表可以在任意一级停止,不用选到最后一级handleChange
:代表选择结束,到第二级或者第三级,默认参数是一个数组[选中的一级,二级, 三级]
- 最后就是懒加载
首先在data中声明props: { lazy: true, lazyLoad: this.lazyLoad},
然后在methods
中定义lazyLoad
方法,,代码如下
lazyLoad(node, resolve) {
setTimeout(() => {
this.getList(node, resolve)
}, 200)
},
async getList(node, resolve) {
let nodes = []
const optionsList = []
if (node.level === 0) {
const res = await ResourceAdminAPI.queryListByDictClass({
dictClass: 'resource'
})
let contentList = res.content || []
this.typeOptionList = res.content || []
contentList = contentList.map((item) => {
return {
value: item.dictType,
label: item.dictName,
leaf: false
}
})
nodes = contentList
resolve(nodes)
} else if (node.level === 1) {
nodes = this.typeOptionList.find((item) => item.dictName === node.label) .dictValueList || []
nodes.map((item) => {
this.getResourceNameList(node.value, item.dictValCode).then((res) => {
item.value = item.dictValCode
item.label = item.dictValName
item.leaf = !res.length
item.children = res.map((rc) => {
return {
value: rc.value,
label: rc.label,
leaf: true
}
})
})
})
setTimeout(() => {
resolve(nodes)
}, 100)
} else if (node.level === 2) {
resolve(nodes)
}
},