<template>
<el-cascader
v-model="checkData"
:options="options"
ref="cascader"
:props="cascaderProps"
:show-all-levels="showAllLevels"
@change="handleChange"
filterable
clearable/>
</template>
<script>
import * as api from './api'
export default {
name: "mz-aera-cascader",
props: {
multiple: {
type: Boolean,
required: false,
default: false
},
checkStrictly: {
type: Boolean,
required: false,
default: false
},
showAllLevels: {
type: Boolean,
required: false,
default: true
},
filterable: {
type: Boolean,
required: false,
default: false
},
checkOption: {
type: Array,
required: false,
default: []
}
},
watch: {
checkOption(newData) {
this.checkData = newData
}
},
data() {
return {
pid: 0,
checkData: this.checkOption,
options: [],
cascaderProps: {
lazy: true,
multiple: this.multiple,
checkStrictly: this.checkStrictly,
value: 'id',
label: 'name',
childrenData: 'children',
lazyLoad: (node, resolve) => {
this.getData(node, resolve)
}
},
}
},
methods: {
getData(node, resolve) {
if (node.level != 0) {
this.pid = node.data.id
} else {
this.pid = 0
}
let childDataList = []
api.getDataByPid(this.pid).then(res => {
childDataList = res resolve(childDataList)
})
},
handleChange() {
if (!this.multiple) {
const selectNode = this.$refs.cascader.getCheckedNodes()[0]
if (selectNode && !selectNode.loaded) this.$refs.cascader.$refs['panel'].lazyLoad(selectNode)
}
this.$emit('handleChange', this.checkData)
}
}
}
</script>
<style scoped>
</style>