当前位置: 首页 > Web前端 > vue.js

vueelement-ui中el-cascader的异步使用(含echo、filter)

时间:2023-04-01 01:26:04 vue.js

>>>.loading-cascader{line-height:32px;.el-loading-spinner{高度:32px;行高:32px;顶部:0;保证金顶部:0;文本对齐:右;向右填充:10px;}}.data-dict{位置:相对;.el-cascader,.el-select{显示:块;宽度:100%;}}>>>.loading-cascader{line-height:32px;.el-loading-spinner{高度:32px;行高:32px;顶部:0;保证金顶部:0;文本对齐:右;向右填充:10px;}}.data-dict{位置:相对;.el-cascader,.el-select{显示:块;宽度:100%;}}import{arrayValueEquals}from'@/utils/util.js'exportdefault{components:{},model:{prop:'value',event:'input'},props:{值:{//对应父组件v-model中的值type:[Array],default:()=>null},dictKey:{//type:String,required:true,default:()=>''},checkStrictly:{类型:布尔值},needReturnCheckedNodes:{类型:布尔值,},禁用:{类型:布尔值},多个:{类型:布尔值},可过滤:{类型:布尔值},可清除:{类型:布尔值},size:{type:String},},data(){return{filterKey:'',filterOptions:[],orginList:[],currentVal:[],isFinishedGetOptions:false,dictTreeProps:null}},计算:{_current:{get(){returnthis.currentVal},set(val){this.currentVal=valthis.$emit('input',val)}}},watch:{value:{handler(newVal,oldVal){如果(newVal&&Array.isArray(newVal)&&newVal.length){this.currentVal=newVal}else{this.currentVal=[]}如果(this.isFinishedGetOption年代||arrayValueEquals(newVal,oldVal)){return}if(this.currentVal&&this.currentVal.length){//如果当前有价值,则获取数据用于返回this.getAllTreeByCurrent()}else{this.getTreeInfo()}},immediate:true}},created(){constths=thisconsole.log('created')ths.dictTreeProps={lazy:true,children:'children',label:'name',value:'key}',multiple:ths.multiple,checkStrictly:ths.checkStrictly,lazyLoad(node,resolve){if(node.level===0){ths.getInitTree(resolve)}else{ths.getChildrenInfo(node,resolve)}}}}if(!ths.dictKey){ths.isFinishedGetOptions=truereturnfalse}},方法:{beforeFilter(val){constths=thisths.filterKey=valconstpromise=newPromise((resolve,reject)=>{constparams={key:val,source_uri:ths.dictKey}ths.$store.dispatch('xxxxxxx/getAllTreeByKeyword',params).then(res=>{consttempList=Array.isArray(res.data)?res.data:(res.data?[res.data]:[])if(tempList&&tempList.length){ths.filterOptions=tempListresolve(tempList)}else{reject(newError())}}).catch(res=>{拒绝(新的呃ror())})})returnpromise},blurFn(){this.filterKey=''},//值改变后的回调changeFn(value){letnodes=[]if(this.needReturnCheckedNodes){nodes=this.$refs.dictCascaderRef.getCheckedNodes()||[]}this.$emit('change',value,nodes)},//获取子列表getChildrenInfo(current,resolve){if(!current||!current.data||!current.data.id){returnfalse}//如果已经有子节点数据返回if(current.data.leaf||(resolve&¤t.data.children&¤t.data.children.length)){resolve([])}else{constparams={node_id:current.data.id}constths=thisths.$store.dispatch('dictdata/getTreeChildByKey',params).then(res=>{lettempList=[]if(res.data&&res.data.length&&res.data[0].children&&res.data[0].children.length){tempList=this.removeEmptyFn(res.data[0].children||[])}if(resolve){resolve(tempList)}}).catch(res=>{resolve([])})}},//移去子列表表示为空removeEmptyFn(list=[]){constths=thisconsttempList=listtempList.forEach(item=>{if(!item.儿童||!item.children.length){删除item.children}else{item.children=ths.removeEmptyFn(item.children)}})returntempList},/***根据当前值获取树(forecho*eg[[11,111,1111],[13,131]]thenfrom后台返回第一层的所有数据[11;12;13...],[11]children的所有数据,[111]children的所有数据,[13]children的所有数据,)*/getAllTreeByCurrent(){constths=这个constparams={路径:ths.currentVal||[],source_uri:ths.dictKey}ths.isFinishedGetOptions=falseths.$store.dispatch('xxxx/getAllTreeByCurrent',params).then(res=>{consttempList=Array.isArray(res.data)?res.数据:(res.data?[res.data]:[])if(tempList&&tempList.length){ths.isFinishedGetOptions=trueths.orginList=tempList}else{ths.getTreeInfo()}}).catch(res=>{ths.isFinishedGetOptions=true})},//初始化信息getTreeInfo(){constths=thisconstparams={key:ths.dictKey,view_type:'tree'}ths.isFinishedGetOptions=falseths.$store.dispatch('xxx/getTreeInfo',params).then(res=>{consttempList=res.data||[]ths.orginList=tempList}).catch(res=>{}).finally(()=>{ths.isFinishedGetOptions=true})},//初始化一级信息getInitTree(resolve){constths=thisif(ths.isFinishedGetOptions){if(解决){resolve(ths.orginList)}}else{setTimeout(()=>{ths.getInitTree(resolve)},300)}}}}