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

相关文章

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

禁止HTML页面滚动的操作方法

《禁止HTML页面滚动的操作方法》:本文主要介绍了三种禁止HTML页面滚动的方法:通过CSS的overflow属性、使用JavaScript的滚动事件监听器以及使用CSS的position:fixed属性,每种方法都有其适用场景和优缺点,详细内容请阅读本文,希望能对你有所帮助... 在前端开发中,禁止htm

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态