vue2+ant-design-vue a-select组件二次封装(支持单选/多选添加全选/分页(多选跨页选中)/自定义label)

本文主要是介绍vue2+ant-design-vue a-select组件二次封装(支持单选/多选添加全选/分页(多选跨页选中)/自定义label),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、效果图

在这里插入图片描述

二、参数配置

1、代码示例

<t-antd-selectv-model="selectVlaue":optionSource="stepList"@change="selectChange"
/>

2、配置参数(Attributes)继承 a-select Attributes

参数说明类型默认值
v-model绑定值boolean / string / number/Array-
mode设置’multiple’'tags’多选 (显示全选)String-
optionSource下拉数据源Array-
widthselect宽度(可以设置百分比或px)String100%
customLabel是否自定义设置下拉labelString-
valueKey传入的 option 数组中,要作为最终选择项的键值 keyString‘key’
labelKey传入的 option 数组中,要作为显示项的键值名称String‘label’
isShowPagination是否显示分页(分页不显示全选框)Booleanfalse
paginationOption分页配置项Object-

2-1、paginationOption配置参数(Attributes)继承 a-pagination Attributes

参数说明类型默认值
current当前页数number1
pageSize每页显示条目个数number6
total总条目数number0
bind继承a-pagination属性Object-

3、继承 a-select&&a-pagination events

事件名说明返回值
current-change当前页码当前选中的页码

三、源码

<template><div @mousedown="e => {e.preventDefault()selectOpen = true}" ref="main"><a-selectclass="t_select"v-model="childSelectedValue":style="{width: width||'100%'}":placeholder="placeholder":open="selectOpen"@select="handleSelect"v-bind="attrs"v-on="$listeners":mode="mode"><template v-for="(index, name) in $slots" v-slot:[name]><slot :name="name" /></template><template v-for="(index, name) in $scopedSlots" v-slot:[name]="data"><slot :name="name" v-bind="data"></slot></template><div slot="dropdownRender" slot-scope="menu"><a-checkboxv-if="mode&&!isShowPagination":checked="selectChecked"@change="selectAll"class="all_checkbox">全选</a-checkbox><v-nodes :vnodes="menu" /><div class="t_select__pagination" v-if="isShowPagination"><a-pagination:page-size.sync="paginationOption.pageSize"v-model="paginationOption.current":total="paginationOption.total"@change="currentChange"v-bind="{size:'small','hide-on-single-page':true,'showQuickJumper': true,...$attrs,...paginationOption.bind,}"v-on="$listeners"/></div></div><a-select-optionv-for="(item,index) in optionSource":key="index":value="item[valueKey]">{{customLabel?customLabelHandler(item):item[labelKey]}}</a-select-option></a-select></div>
</template>
<script>
export default {name: 'TAntdSelect',components: {VNodes: {functional: true,render: (h, ctx) => ctx.props.vnodes}},props: {value: {type: [String, Number, Array, Boolean, Object]},// 多选 'multiple'mode: {type: String},placeholder: {type: String,default: '请选择'},// 选择框宽度width: {type: String},// 是否自定义设置下拉labelcustomLabel: {type: String},// 传入的option数组中,要作为最终选择项的键值keyvalueKey: {type: String,default: 'key'},// 传入的option数组中,要作为显示项的键值名称labelKey: {type: String,default: 'label'},// 下拉框组件数据源optionSource: {type: Array},// 是否显示分页isShowPagination: {type: Boolean,default: false},// 分页配置项paginationOption: {type: Object,default: () => {return {pageSize: 6, // 每页显示条数current: 1, // 当前页total: 0 // 总条数}}}},data() {return {selectOpen: false}},computed: {childSelectedValue: {get() {return this.value || undefined},set(val) {this.$emit('input', val)}},attrs() {return {allowClear: true,showSearch: true,...this.$attrs}},selectChecked: {get() {return this.childSelectedValue?.length === this.optionSource?.length},set(val) {// console.log('set', val)this.$emit('input', val)}}},mounted() {document.addEventListener('click', this.bodyCloseMenus)},beforeDestroy() {document.removeEventListener('click', this.bodyCloseMenus)},methods: {// 点击空白区域bodyCloseMenus(e) {if (this.$refs.main && !this.$refs.main.contains(e.target)) {if (this.selectOpen == true) {this.selectOpen = false}}},// 点击全选selectAll(val) {const options = JSON.parse(JSON.stringify(this.optionSource))if (val.target.checked) {this.childSelectedValue = options?.map(item => {return item[this.valueKey]})setTimeout(() => {this.$emit('change', this.childSelectedValue)}, 0)} else {this.childSelectedValue = null}this.selectOpen = false},handleSelect(value, option) {if (value) {this.selectOpen = false}this.$emit('select', value, option)},// 切换分页currentChange(val) {// console.log('切换分页', val)if (!this.mode) {this.childSelectedValue = null}setTimeout(() => {this.selectOpen = true}, 0)this.$emit('current-change', val)},// 自定义label显示customLabelHandler(item) {// eslint-disable-next-line no-evalreturn eval(this.customLabel)}}
}
</script>
<style lang="scss">
.all_checkbox {margin-left: 12px;margin-top: 5px;
}
</style>

四、组件地址

gitHub组件地址

gitee码云组件地址

五、相关文章

基于ElementUi再次封装基础组件文档


基于ant-design-vue再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

这篇关于vue2+ant-design-vue a-select组件二次封装(支持单选/多选添加全选/分页(多选跨页选中)/自定义label)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/314028

相关文章

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page