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
      }
    },
相关推荐
用户2986985301426 分钟前
在 React 中实现 Excel 与 Markdown 格式互转的前端实践
javascript·excel·markdown
徐小夕38 分钟前
万字分享:CRDT 算法在 Word 文档中的工程化落地
前端·vue.js·github
map_3d_vis1 小时前
百度地图添加标注 Marker 和自定义图标(JSAPI 4.0)
前端·javascript·地图·百度地图
夕水2 小时前
夕水文学网502 故障 —— 爬虫把 Node 堆内存吃爆
javascript·后端·node.js
honkun62 小时前
vue 表格组件 vxe-table 行拖拽排序时,为“拖拽为子级”增加操作提示
前端·javascript·vue.js·vxe-table
斯内普吖2 小时前
(开源)智慧物业管理系统实战指南 基于 Python + Django + uni-app + Vue + MySQL
vue.js·python·django·uni-app·开源
总要学点什么2 小时前
第127篇:河道流场水面——把整条河的流向烤进一张贴图
开发语言·javascript·microsoft·材质·贴图·cesium
油丶酸萝卜别吃2 小时前
Vue2 实现全局主题切换功能:CSS Variables + Vuex
开发语言·前端·javascript
油丶酸萝卜别吃2 小时前
Vue 实现主题切换功能(超简单版)
前端·vue.js·tensorflow
flyingonpine3 小时前
本地大模型能看图修 Bug 吗?Strata + Three.js 三维场景生成与多模态视觉反馈实战
javascript·人工智能·大语言模型·webgl·three.js·ai agent