el-cascader层级控件使用详细,clearable删除旧项(el-cascader使用clearable,完全清空,删除旧项)

1、el-cascader标签属性:

<el-cascader
    v-model="subjectValue"
    :options="searchOptions"
    :show-all-levels="false"
    :props="{ expandTrigger: 'hover' }"
    @change="choseTheme"
    placeholder="检索类型"
    clearable
>
              <template slot-scope="{ node, data }">//层级内容数量
                <span>{{ data.label }}</span>
                <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
              </template>
 </el-cascader>

2、相关参数属性值:

data() {
    return {
      //地区地址层级数据选择
      searchOptions: [  
        {
          value: "area",
          label: "地区",
          children: [
           //地区层级参数......
             children: [ 地区层级参数...... ],
          ],
        },
        {
          value: "title",
          label: "题目",
        },
      ], 
      subjectValue: "", //主题参数
      searchCon: "", // 搜索框输入的搜索内容
      preTheme: "title", // 上一个主题
      curTheme: "title", // 当前主题
  },

3、层级控件的选择事件:

  choseTheme(subjectValue) {
      //subjectValue是层级控件选中的数据
      let Area = subjectValue[0];
      if (subjectValue.length === 3) {
        //地区>中国**市,三层控件的选择事件
        subjectValue = subjectValue[2];
        this.searchContent[Area] = subjectValue;
        this.choseSearchResult();

        //也可以使用this.$set来实现
        // this.$set(this.searchContent, Area, subjectValue);
        // console.log(subjectValue); 
      } else if (subjectValue.length === 2) {
        //地区>**国,二层控件的选择事件
        console.log(this.searchContent);
        subjectValue = subjectValue[1];
        this.searchContent[Area] = subjectValue;
        this.choseSearchResult();

        //也可以使用this.$set来实现
        // this.$set(this.searchContent, Area, subjectValue);
        // console.log(subjectValue);
      } else if (subjectValue.length === 1) {
        // 删除旧键
        let oldKey = this.preTheme;
        delete this.searchContent[oldKey];

        //检索类型一层 ,删除地区内容
        delete this.searchContent.area;
        //然后赋值,搜索
        this.curTheme = subjectValue;
        this.choseSearchResult();
      } else if (subjectValue.length === 0) {
        //clearable的点击×删除事件
        let oldKey = this.preTheme;
        // console.log("oldKey", oldKey);
        delete this.searchContent[oldKey];
      }
        //赋值旧值
      this.preTheme = subjectValue;
    },

4、搜索事件:

choseSearchResult() {
      // console.log(this.searchContent);
      if (this.searchContent.area) {//如果有地区参数默认title
        this.curTheme = "title";
      } else {
        delete this.searchContent.title;
      }
      let key = this.curTheme;//赋值搜索
      this.searchContent[key] = this.searchCon;

      searchKeyword(this.searchContent).then((res) => {//搜索接口
         ......
      });
    },

el-select和el-cascader:

祝你开心、快乐、幸福、健康、点赞、收藏、关注😀

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北城笑笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值