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
      }
    },
相关推荐
炫饭第一名2 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
Forever7_3 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
不会敲代码13 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Angelial3 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
进击的尘埃4 小时前
Vue3 响应式原理:从 Proxy 到依赖收集,手撸一个迷你 reactivity
javascript
willow4 小时前
JavaScript数据类型整理1
javascript
LeeYaMaster4 小时前
20个例子掌握RxJS——第十一章实现 WebSocket 消息节流
javascript·angular.js
UIUV5 小时前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
SuperEugene5 小时前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
阿懂在掘金5 小时前
defineModel 是进步还是边界陷阱?双数据源组件的选择逻辑
vue.js·源码阅读