el-cascader 设置可以手动输入也可以下拉选择

el-cascader 设置可以手动输入也可以下拉选择

稍微修改一下就可食用

js 复制代码
   <template slot="stationId" slot-scope="">
        <div style="position: relative;">
          <!-- 可输入也可显示选项 -->
          <el-input
            :value="stationNameInput"
            @input="onStationNameInput"
            @blur="onStationNameBlur"
            placeholder="请选择或输入"
            clearable
          >
            <template slot="suffix">
              <i class="el-icon-arrow-down" @click="toggleCascader" style="cursor: pointer;" />
            </template>
          </el-input>

          <!-- 隐藏的 Cascader,仅触发选项选择 -->
          <el-cascader
            ref="cascaderAddr"
            v-model="stationids"
            :disabled="type === 'view'"
            :check-strictly="true"
            :options="unitTreeData"
            :props="defaultProps"
            placeholder="请选择所属部门"
            @change="handleChange"
            style="position: absolute; top: 0; left: 0; opacity: 0; z-index: -1; pointer-events: none;"
          />
        </div>
      </template>


//data
   stationNameInput: '', // 展示输入框内容
   isManualInput: false, // 判断用户是否在输入


//methods
   // 输入框打开 Cascader
    toggleCascader() {
      const inputEl = this.$refs.cascaderAddr.$el.querySelector('input')
      if (inputEl) inputEl.click()
    },

    // 用户手动输入
    onStationNameInput(val) {
      this.stationNameInput = val
      this.data.stationName = val
      this.isManualInput = true
    },

    onStationNameBlur() {
      // 失去焦点后关闭输入标记
      this.isManualInput = false
    },

    // 选择之后,仅当不是手动输入才填充
    handleChange(val) {
      this.stationids = val
      this.data.stationId = val[val.length - 1]
      this.data.stationIds = val.join(',')

      if (!this.isManualInput) {
        const node = this.$refs.cascaderAddr.getCheckedNodes()?.[0]
        const label = node ? node.pathLabels.join('/') : ''
        this.stationNameInput = label
        this.data.stationName = label
      }
    },
相关推荐
Cacciatore->4 小时前
Electron 快速上手
javascript·arcgis·electron
vvilkim5 小时前
Electron 进程间通信(IPC)深度优化指南
前端·javascript·electron
某公司摸鱼前端6 小时前
ES13(ES2022)新特性整理
javascript·ecmascript·es13
清幽竹客7 小时前
vue-30(理解 Nuxt.js 目录结构)
前端·javascript·vue.js
知性的小mahua7 小时前
echarts+vue实现中国地图板块渲染+省市区跳转渲染
vue.js
weiweiweb8887 小时前
cesium加载Draco几何压缩数据
前端·javascript·vue.js
我不吃饼干9 天前
鸽了六年的某大厂面试题:你会手写一个模板引擎吗?
前端·javascript·面试
我不吃饼干9 天前
鸽了六年的某大厂面试题:手写 Vue 模板编译(解析篇)
前端·javascript·面试
前端fighter9 天前
为什么需要dependencies 与 devDependencies
前端·javascript·面试