Angel丶守护&雪之源 2022-03-15 14:31 采纳率: 24.3%
浏览 108
已结题

element cascader中的before-filter钩子输入框的搜索关键词为空时不触发,还有什么方法监测输入关键词的变化

问题遇到的现象和发生背景

我想监测输入关键词的变化,当有值时进行数据请求,没值时默认请求初始数据

问题相关代码,请勿粘贴截图
<el-cascader
        v-model="cascaderValue"
        :options="Options"
        :props="Props"
        ref="myCascader"
        :show-all-levels='showAllLevels'
        :filterable="Filterable"
        :clearable='clearable'
        separator
        :debounce='debounce'
        @change="handleChange"
        @expand-change="expandChange"
        :before-filter="searchFilter">
 </el-cascader>

searchFilter(searchKey){
            this.searchVal=searchKey
            let query={
                with:1,
                paged:1,
                per_page:100,
                search:this.searchVal
            }
            this.getOptions(query)
            return false
},

getOptions(query){
            const req = new ARequest({
                method:'get',
                url:`/api/vc/term?query[term_group]=term-product`,
                nonce:true,
                lang:true,
                catch:true,
                query
            })
            req.send().then(res=>{
                // 判断是否是展开选项还是搜索或者默认加载数据
                if(this.soureVal!=''){
                    this.getMenuBtnList(this.Options,res,this.soureVal)
                }else if(this.searchVal!=''){
                    let Options=[]
                    res.forEach(item=>{
                        let obj = {
                            label:item[this.nameKey],
                            value:item[this.idKey]
                        }
                        Options.push(obj)
                    })
                    this.Options = Options
                    this.searchVal=''
                }else if(query.search==''||this.soureVal==''){
                    let Options=[]
                    res.forEach(item=>{
                        let obj = {
                            label:item[this.nameKey],
                            value:item[this.idKey],
                            children:[]
                        }
                        Options.push(obj)
                    })
                    this.Options = Options
                }
            })
            
        },

getMenuBtnList (menuTreeList, menuList,val) {
            let list =[]
            for (let item of menuTreeList) {
                if(menuList.length==0&&item.value==val){
                    let {children,...data}=item
                    item=data
                }else{
                    for(let item1 of menuList){
                        if (item.value === val) {
                            list.push({
                                label: item1[this.nameKey],
                                value: item1[this.idKey],
                                children: []
                            });
                            item.children=list
                        } else if (item.children.length > 0 ) {
                            this.getMenuBtnList(item.children, menuList,val);
                        }
                    }
                }
            }
            this.soureVal=''
        },

created(){
        let query = {
            with:1,
            paged:1,
            per_page:-1,
            'query[parent]':0
        } 
        this.getOptions(query)
 }

  • 写回答

1条回答 默认 最新

  • 崽崽的谷雨 2022-03-15 14:53
    关注

    cascaderValue 判断这个的值 。给 不同的参数 。

    或者 监听 这个 watch也可以做到 。

    filter-method 这个可以自定义逻辑

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月18日
  • 已采纳回答 5月18日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测