Element input树型下拉框的实现代码
程序员文章站
2023-11-14 21:15:16
1.效果图
1.1 input聚焦时显示下拉框,再次点击下拉框或点击其他处下拉框消失,主要靠z-index添加遮罩实现
1.2 实时过滤效果
2.代码 ( v...
1.效果图
1.1 input聚焦时显示下拉框,再次点击下拉框或点击其他处下拉框消失,主要靠z-index添加遮罩实现
1.2 实时过滤效果
2.代码 ( vue.js + element-ui )
2.1 html
<el-form :model="form" size="mini" > <el-row> <el-col :span='12'> <el-form-item label="会计主管" > <el-input placeholder="请选择会计主管" class="width-220 selecttree-input" v-model="form.manager_name" icon="caret-bottom" auto-complete="off" @focus="focus($event)" @click.native="changeselecttree()"> </el-input> <div v-show="isshowselect" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 102;" @click="cancelmanager"> </div> <el-tree v-show="isshowselect" empty-text="暂无数据" :highlight-current = true :default-expand-all = false :expand-on-click-node="false" :filter-node-method="filternode" :data="userlist" node-key="chr_id" :props="defaultprops" @node-click="selectmanage" class="objecttree" ref="selecttree"> </el-tree> </el-form-item> </el-col> <el-col :span='12'> </el-col> </el-row> </el-form>
2.2 js
import 'babel-polyfill'//兼容语法 async focus export default { data(){ return { form: { manager_name: '', manager_id: '', }, isshowselect: false,// 是否显示会计主管的树状选择器 userlist: [],// 会计主管的选项数据 defaultprops: { // 会计主管 树状选择器 的选项的配置参数 children: 'children', label: 'code_name', }, } }, watch: { form: {//form.manager_name变化时过滤节点 handler(form){ if(this.isshowselect){ this.$refs.selecttree.filter(form.manager_name); } }, deep: true,//深度监听,重要 }, }, methods:{ //下拉框的显示与隐藏 changeselecttree(){ this.isshowselect = !this.isshowselect; }, //input获取焦点事件,初始化树 async focus(e) { let vm = this; vm.$refs.selecttree.filter(""); vm.$refs.selecttree.setcurrentnode([]); }, // 选择器的树节点 filternode(value, data) { if (!value) return true; if(!data.code_name){ data.code_name = data.chr_code + " " + data.chr_name } return data.code_name.indexof(value) !== -1; }, //选择会计主管 selectmanage(data, node) { this.form.manager_name = data.code_name;//input赋值 this.form.manager_id = data.chr_id; this.isshowselect = false;// 关闭选择器 }, //点击遮罩层,取消选择会计主管 cancelmanager(){ this.isshowselect = false }, } }
2.3 css
<style lang="scss"> /*下拉框选择树*/ .objecttree { position: absolute; overflow: auto; z-index: 100; width: 110%; height: 200px; border: 1px solid #ddd; line-height: normal; z-index: 204; } .selecttree-input { input:focus { z-index: 204;//103 } } .width-220{ width: 220px } </style>
2.4 参考数据
//示例下拉框数据 this.userlist = [{ chr_code: "001001051", chr_id: "9853", chr_name: "张海舒", is_leaf: "1", user_type: "0", }]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: jQuery 遍历方法大全