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

一、效果图

二、参数配置

1、代码示例

html 复制代码
<t-antd-select
  v-model="selectVlaue"
  :optionSource="stepList"
  @change="selectChange"
/>

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

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

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

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

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

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

三、源码

html 复制代码
<template>
  <div @mousedown="e => {
    e.preventDefault()
    selectOpen = true
  }" ref="main">
    <a-select
      class="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-checkbox
          v-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-option
        v-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
    },
    // 是否自定义设置下拉label
    customLabel: {
      type: String
    },
    // 传入的option数组中,要作为最终选择项的键值key
    valueKey: {
      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-eval
      return 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再次封装基础组件文档

相关推荐
万叶学编程2 小时前
Day02-JavaScript-Vue
前端·javascript·vue.js
积水成江5 小时前
关于Generator,async 和 await的介绍
前端·javascript·vue.js
计算机学姐5 小时前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
老华带你飞6 小时前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计
qbbmnnnnnn6 小时前
【WebGis开发 - Cesium】如何确保Cesium场景加载完毕
前端·javascript·vue.js·gis·cesium·webgis·三维可视化开发
杨荧7 小时前
【JAVA开源】基于Vue和SpringBoot的水果购物网站
java·开发语言·vue.js·spring boot·spring cloud·开源
霸王蟹8 小时前
Vue3 项目中为啥不需要根标签了?
前端·javascript·vue.js·笔记·学习
老章学编程i9 小时前
Vue工程化开发
开发语言·前端·javascript·vue.js·前端框架
tanxiaomi10 小时前
vue 不是spa 单页面应用吗? 配置路由工作模式为history 后 ,为什么配置Nginx的 try_files 可以根据url 找到对应的文件?
前端·vue.js·nginx
果子切克now10 小时前
vue3导入本地图片2种实现方法
前端·javascript·vue.js