Vue 自定义仿word表单下拉框组件,让操作更符合用户习惯

news/2024/10/22 7:50:56/

预览时显示界面

进入编辑框时

 

组件代码

<template><div class = "paper-select ui-select flex flex-col full-width"   ><div ref="content" class="content font-s flex flex-center-cz padding-left-m padding-right-m flex-space-between"             @click="select" style="border:none;"  :class="[readonly === true ? 'readonly' : '']" ><div class="selected flex flex-center-cz text-center font-l" style="height:auto; text-align: center;font-family: FangSong;" >{{text}} </div><div class="down margin-top-s pointer"  ></div></div><div v-if="show" class="position-relative" ><div class="list flex flex-col font-s scroll" :style="{left:leftValue,top:topValue,width:listWidth+'px'}"><div @click="inputClick" v-if="data.length>20"><SearchInput  @search="search" :name="'searchOrg'" :focus="'focus'" class="margin-top-s margin-left-s margin-right-s"></SearchInput></div><template v-if="filterText.length>0"><div  v-for="(item,index) in filterData" :key="index":class="{'active': item.value === val, 'no-acitve': item.value !== val}"class="item show cannotselect pointer"@click="selectItem(item.value)" >{{item.text}}</div></template><template v-else><div  v-for="(item,index) in allData" :key="index":class="{'active': item.value === val, 'no-acitve': item.value !== val}"class="item show cannotselect pointer"@click="selectItem(item.value)" >{{item.text}}</div></template></div></div></div>
</template><script>import SearchInput from "@/components/searchInput/SearchInput.vue";export default {name:"Select",components: {SearchInput},props:{data:{},readonly:{type: Boolean,default:false,required: false},value:{},result:{}},model: {prop: "value",event: "change"},watch:{value:{handler(newValue) {this.val = newValue;this.text = this.getText(newValue);},immediate: true,deep: true   //深度监听},data:{handler(newValue) {this.allData = newValue},immediate: true,deep: true   //深度监听},},data() {return {editMode:false,val:null,text:null,selectData:[],allData:[],filterData:[],filterText:'',show: false,leftValue: '',topValue:'',listWidth:''}},methods:{inputClick(event) {event.stopPropagation();},search(v){console.log(v,this.allData)if(v && v.length>0){this.filterText = vthis.filterData = this.data.filter(e=>e.text.indexOf(this.filterText)>=0)}},updateText() {this.text = this.getText(this.val);console.log("val=====", this.val)console.log("text=====", this.text)},getText(value) {let result = ''if (this.data !== null && this.data !== "") {let find = this.data.filter(b => b.value == value);if (find.length > 0) {result = find[0].text;}}return result;},selectItem(value) {this.val = value;this.text = this.getText(value);this.show = false;this.filterText = ""this.$emit("change", this.val);},select(event) {console.log("click")event.stopPropagation();//阻止冒泡,防止触发下层点击事件document.addEventListener("click", this.closeList, false);// 添加监听点击事件let tValue = this.$el.getBoundingClientRect().top;let lValue = this.$el.getBoundingClientRect().left;let windowHeight = document.documentElement.clientHeight; // 实时屏幕高度let inputHeight = this.$refs['content'].clientHeight;this.listWidth = this.$refs['content'].clientWidth;console.log("listWidth",this.listWidth);let listHeight = 200;this.topValue = tValue + inputHeight + 'px';this.leftValue = lValue  + 'px';if (parseFloat(tValue) + listHeight > windowHeight) {this.topValue = (parseFloat(windowHeight) - listHeight - inputHeight)  + "px";console.log("tvalue1", this.tValue)}this.show = !this.show;this.filterText = ''},closeList() {document.removeEventListener("click", this.closeList, false);//关闭监听点击事件this.show = false;this.filterText = ''},}}
</script><style scoped>.paper-select .content:hover .down   {display: inline-block;height: 0;width: 0;border-top: 7px solid rgba(0,0,0,0.3);border-bottom: 7px solid transparent;border-left: 7px solid transparent;border-right: 7px solid transparent;}.ui-select .content{box-sizing: border-box;background-color: #fff;height: 30px;border-radius: 2px;border: 1px solid rgba(0, 0, 0, .2);
}/* 选中区 */
.ui-select .content .selected {height: 28px;width: calc(100% - 30px);
}.ui-select .content .selected .item{height: 22px;
}/* 待选列表 */
.ui-select .list {background-color: #fff;position: fixed;border: 1px solid rgba(0, 0, 0, .2);left:0;top:0;right: 0;height: 200px;z-index: 99;
}/* 待选列表行高 */
.ui-select .list .item {padding-left: 5px;line-height: 24px;
}.ui-select .list .item:hover {background-color: rgba(0, 0, 0, .1);
}.ui-select .list .active {background-color: #0091FF;
}.ui-select .list .no-active {background-color: #fff;
}</style>

使用组件

 <PagerSelect :data="hyData" ></PagerSelect>hyData:
[{value:1,text:'已婚'},{value:0,text:'未婚'}
],

以上代码仅作参考,如需完整代码可以私信我。


http://www.ppmy.cn/news/1295651.html

相关文章

Leetcode 3003. Maximize the Number of Partitions After Operations

Leetcode 3003. Maximize the Number of Partitions After Operations 1. 解题思路2. 代码实现 题目链接&#xff1a;10038. Maximize the Number of Partitions After Operations 1. 解题思路 这一题我看实际比赛当中只有72个人做出来&#xff0c;把我吓得够呛&#xff0c;…

CCNP课程实验-07-OSPF-Trouble-Shooting

目录 实验条件网络拓朴 环境配置开始排错错点1&#xff1a;R1-R2之间认证不匹配错误2&#xff1a;hello包的时间配置不匹配错误3&#xff1a;R2的e0/1接口区域配置不正确错误4&#xff1a;R4的e0/1接口没有配置进OSPF错误5&#xff1a;R2的区域1没有配置成特殊区域错误6&#x…

地理空间分析5——空间关联分析与Python

目录 写在开头1.空间自相关2.空间回归分析2.1 构建地理权重矩阵2.2 执行空间回归分析2.3 解释结果3 地理加权回归3.1 构建地理权重矩阵3.2 执行地理加权回归分析3.3 解释地理加权回归结果写在最后写在开头 空间关联分析是数据科学领域中一个重要的技术,尤其在地理信息系统(G…

docker运行状态

systemctl status docker Active: active (running) since 一 2024-01-08 06:21:10 CST; 3min 57s ago [rootlocalhost ~]# systemctl status docker ● docker.service - Docker Application Container EngineLoaded: loaded (/usr/lib/systemd/system/docker.service; enabl…

【SQL】加快SQL查询的九种优秀实践

1.只检索需要的列 对于那些所谓的数据库开发老司机而言,他们会有一个常见的SQL习惯:在编写查询代码时,频繁地使用SELECT *,一次性列出所有可能需要的数据列。显然,如果查询一个存储了一百多列的数据表的所有列,您可以想象会发生什么?毕竟在真实的系统应用环境中,这样的…

SpringCloud-高级篇(十一)

&#xff08;1&#xff09;搭建Redis-主从架构 前面我们实现了Redis的持久化&#xff0c;解决了数据安全问题&#xff0c;但是还有需要解决的问题&#xff0c;下面学习Redis的主从集群&#xff0c;解决Redis的并发能力的问题 Redis的集群往往是主从集群&#xff0c;Redsi为什么…

什么是跨域以及怎么处理跨域问题

文章目录 什么是跨域&#xff1f;跨域问题常见场景怎么处理跨域1、配置代理2、CORS&#xff08;跨域资源共享&#xff09;3、JSONP&#xff08;仅限 GET 请求&#xff09;4、使用 WebSocket 注意事项&#xff1a; 什么是跨域&#xff1f; 跨域&#xff08;Cross-Origin&#x…

Ubuntu 24.04 Preview 版安装 libtinfo5

Ubuntu 24.04 Preview 版安装 libtinfo5 0. 背景1. 安装 libtinfo52. 安装 cuda 0. 背景 Ubuntu 24.04 Preview 版安装 Cuda 时报确实 libtinfo5 的错误。 1. 安装 libtinfo5 wget http://archive.ubuntu.com/ubuntu/pool/universe/n/ncurses/libtinfo5_6.4-2_amd64.deb dpk…