el-cascader 自动展开第一项或者某一项的下一级

想要实现的效果 当选择项只有一个的时候 自动展开下一级 最后一级和第一级的时候 用户自己点击 其他时候自动展开
看到源码是这样操作的 大家有更好的可以交流啊

请添加图片描述

<template>
  <el-cascader
    ref="cascader"
    v-model="path"
    placeholder="请选择关联项目"
    :props="props"
    :disabled="disabled"
    @visible-change="visibleChange"
  />
</template>

<script lang="ts">
import Vue from 'vue';
import { Component, Prop, Ref, Watch } from 'vue-property-decorator';
import FormBase from '@/components/form/FormBase';
import Fetch from '@/utils/fetch';
import type { Cascader } from 'element-ui';

@Component
export default class RelationItemPath extends FormBase<string> {
  @Prop({
    type: String,
    required: false,
    default: ''
  })
  private requestKey !: string;

  @Prop({
    type: Boolean,
    required: false,
    default: false
  })
  private showText !: boolean;

  @Prop({
    type: String,
    required: false,
    default: ''
  })
  private inputText !: string;

  @Prop({
    type: Boolean,
    required: false,
    default: false
  })
  private disabled !: boolean;

  @Ref()
  private readonly cascader !: Cascader

  private recordText=false; // 【修改的时候】用来判断路径改没改变  没改变传入的inputText,改变了新的选择的value

  private get path (): string {
    if (!this.value) {
      return '';
    }
    this.$refs.cascader && this.$emit('change', (this.$refs.cascader as any).inputValue);
    return this.value;
  }

  private set path (val) {
    const value = val;
    this.$emit('update:value', value);
    this.$emit('input', value);
  }

  private selectPath: string[] = [];
  private props = {
    lazy: true,
    value: 'value',
    label: 'label',
    emitPath: false, // 只返回最后一个节点
    lazyLoad: (node: any, resolve: any) => {
      const { level, pathNodes } = node;
      this.selectPath = [];
      // if (level === 0) {
      //   resolve([]);
      //   return;
      // }
      // if (level === 1) {
      //   this.dataOptions = this.dataPathOptions;
      // }
      if (pathNodes && pathNodes.length) {
        pathNodes.forEach((item: object, index: number) => {
          const value = pathNodes[index] ? pathNodes[index].value : '';
          this.selectPath.push(value);
        });
      }
      Fetch.get(`/api/project_info?application=${this.requestKey}&query=${this.selectPath.join('/')}`, {}, false).then(async response => {
        const nodes = response.data.map((item: any) => ({
          label: item.company_name || item.scan_type || item.name || item.train_type || item.parse_name,
          value: item.company_name || item.ID || item.name || item.train_type || item.parse_name,
          leaf: node.level >= 3
        }));
        resolve(Array.from(new Set(nodes)));
        if(response.data.length === 1 && level !== 3 && level !== 0 ){ // 当只有一级 自动展开下一级 后台固定关联项目只有四级 最后一级 用户自己选择
          await this.$nextTick()
          const nextMenu = (this.$refs.cascader as any).panel.$refs.menu[level]
          if (nextMenu) {
            const firstNode = nextMenu.$el.querySelector('.el-cascader-node[tabindex="-1"]');
            firstNode.focus();
            firstNode.click();
          }
        }
      });
    }
  };

  private visibleChange (visible: boolean) {
    console.log("visibleChange",this.path)
    const cascader = this.$refs.cascader as any;
    if (this.showText && !this.recordText) {
      cascader.inputValue = this.inputText;
      cascader.presentText = this.inputText;
    }
  }

  @Watch('showText', { immediate: true })
  private async onTextChange () {
    await this.$nextTick();
    const cascader = this.$refs.cascader as any;
    if (cascader && this.showText) {
      cascader.inputValue = this.inputText;
    }
  }

  @Watch('path')
  private async onPathChange () {
    await this.$nextTick();
    if (this.showText && (this.$refs.cascader as any).inputValue !== this.inputText) {
      this.recordText = true;
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/components/input/input.scss';
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值