vue+ElementUI el-select实现下拉列表分页的功能(下拉分页组件---新增选中值返回对象示例)

本文介绍了如何在Element UI中使用TPaginationSelect组件解决因数据量大导致的下拉框分页问题,提供详细配置和源码示例,适用于快速筛选大量选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、最终效果

在这里插入图片描述

二、需求

因下拉框中的数据过多,所以需要使用分页的方式来实现

三、参数配置

1、代码示例

<t-select
  v-model="selectVlaue"
  :optionSource="listTypeInfo.stepList"
/>

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

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

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

参数说明类型默认值
currentPage当前页数number1
pageSize每页显示条目个数number6
pagerCount设置最大页码按钮数。 页码按钮的数量,当总页数超过该值时会折叠number5
total总条目数number0
layout组件布局,子组件名用逗号分隔string‘total,prev, pager, next’
bind继承el-pagination属性Object-

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

四、源码

<template>
  <el-select
    popper-class="t_select"
    v-model="childSelectedValue"
    :style="{width: width||'100%'}"
    v-bind="attrs"
    v-on="$listeners"
    :multiple="multiple"
  >
    <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>
    <el-checkbox
      v-if="multiple&&!isShowPagination"
      v-model="selectChecked"
      @change="selectAll"
      class="all_checkbox"
    >{{selectAllText}}</el-checkbox>
    <el-option
      v-for="(item,index) in optionSource"
      :key="index+'i'"
      :label="customLabel?customLabelHandler(item):item[labelKey]"
      :value="returnObject?item:item[valueKey]"
      :disabled="item.disabled"
    />
    <div class="t_select__pagination" v-if="isShowPagination">
      <el-pagination
        :layout="paginationOption.layout || 'total,prev, pager, next'"
        :page-size="paginationOption.pageSize"
        :current-page="paginationOption.currentPage"
        :pager-count="paginationOption.pagerCount"
        :total="paginationOption.total"
        @current-change="currentChange"
        v-bind="{
            small: true,
            'hide-on-single-page':true,
            background: true,
            ...$attrs,
            ...paginationOption.bind,
          }"
        v-on="$listeners"
      />
    </div>
  </el-select>
</template>
<script>
export default {
  name: 'TSelect',
  props: {
    value: {
      type: [String, Number, Array, Boolean, Object]
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false
    },
    // 全选文字
    selectAllText: {
      type: String,
      default: '全选'
    },
    // 选择框宽度
    width: {
      type: String
    },
    // 是否自定义设置下拉label
    customLabel: {
      type: String,
      default: ''
    },
    // 传入的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, // 每页显示条数
          currentPage: 1, // 当前页
          pagerCount: 5, // 按钮数,超过时会折叠
          total: 0 // 总条数
        }
      }
    },
    // 选中值返回对象
    returnObject: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    childSelectedValue: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    attrs() {
      return {
        clearable: true,
        filterable: true,
        ...this.$attrs
      }
    },
    selectChecked: {
      get() {
        return this.childSelectedValue?.length === this.optionSource?.length
      },
      set(val) {
        // console.log('set', val)
        this.$emit('input', val)
      }
    }
  },
  methods: {
    // 点击全选
    selectAll(val) {
      const options = JSON.parse(JSON.stringify(this.optionSource))
      if (val) {
        this.childSelectedValue = options.map(item => {
          return item[this.valueKey]
        })
        setTimeout(() => {
          this.$emit('change', this.childSelectedValue)
        }, 0)
      } else {
        this.childSelectedValue = null
      }
    },
    currentChange(val) {
      this.childSelectedValue = null
      this.$emit('current-change', val)
    },
    // 自定义label显示
    customLabelHandler(_item) {
      let fun = new Function('_item', 'return ' + this.customLabel)
      // let fun = function (_item) {
      //   return this.customLabel
      // }
      return fun(_item)
    }
  }
}
</script>
<style lang="scss">
.t_select {
  .all_checkbox {
    margin-left: 15px;
  }
  .el-pagination {
    display: flex;
    background-color: #fff;
    align-items: center;
    .el-pagination__total,
    .el-pager,
    button {
      display: flex;
      align-items: center;
    }
  }
}
</style>

5、组件地址

gitHub组件地址

gitee码云组件地址

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wocwin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值