element-ui 实现输入框下拉树组件(2024-05-23)

用element-ui的 el-input,el-tree,el-popover组件组合封装

css 复制代码
@import url("//unpkg.com/element-ui@2.15.14/lib/theme-chalk/index.css");
html 复制代码
<script src="//unpkg.com/vue@2/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.15.14/lib/index.js"></script>

<div id="app">
  <el-popover
    placement="bottom"
    width="350"
    trigger="click">
    <el-tree
       style="width:300px"
       ref="tree"
       :data="options"
       :check-strictly="false"
        show-checkbox
        node-key="id"
        :default-expanded-keys="[]"
        :default-checked-keys="[]"
        :props="{
           children: 'children',
           label: 'name'
         }"
         @check-change="handleCheckChage"
         @node-click="handleNodeClick"
     >
    </el-tree>

        <el-input slot="reference" 
          style="width:380px"
         v-model="value.name" 
         placeholder="节点" 
         clearable 
         @clear="handleIptClear">
       </el-input>
  </el-popover>
</div>
javascript 复制代码
var Main = {
    data() {
      return {
        options: [
          {id:'1', name: '1',
           children:[
             {id:'11', name: '11'},
             {id:'12', name: '12'}
           ]
          },
          {id:'2', name: '2'}
        ],
        value:{id:'', name: ''}
        
      }
        
    },
    methods: {
      // 清空输入框内容
        handleIptClear(){
            this.$refs.tree.setCheckedNodes([])
            this.value.id = ''
            this.value.name = ''
        },
        // checkbox被选中或取消选中
        handleCheckChage(arg1, arg2, arg3){
            const seltedNodes = this.$refs.tree.getCheckedNodes()
            const ids = seltedNodes.map(n => n.id)
            const names = seltedNodes.map(n => n.name)
            this.value.id = ids
            this.value.name = names
        },
        // 节点被点击
        handleNodeClick(arg1, arg2, arg3){
            console.log('nodes:', arg1, arg2, arg3)
        },
    }
  };

var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

可以根据函数方法拿到里面的参数,实现多选节点效果

相关推荐
li35741 天前
将已有 Vue 项目通过 Electron 打包为桌面客户端的完整步骤
前端·vue.js·electron
Icoolkj1 天前
VuePress 与 VitePress 深度对比:特性、差异与选型指南
前端·javascript·vue.js
excel1 天前
CNN 分层详解:卷积、池化到全连接的作用与原理
前端
excel1 天前
CNN 多层设计详解:从边缘到高级特征的逐层学习
前端
西陵1 天前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
大前端helloworld1 天前
从初中级如何迈入中高级-其实技术只是“入门卷”
前端·面试
东风西巷1 天前
Balabolka:免费高效的文字转语音软件
前端·人工智能·学习·语音识别·软件需求
萌萌哒草头将军1 天前
10个 ES2025 新特性速览!🚀🚀🚀
前端·javascript·vue.js
半夏陌离1 天前
SQL 入门指南:排序与分页查询(ORDER BY 多字段排序、LIMIT 分页实战)
java·前端·数据库