问题描述
我的项目使用的是vue2和elementui框架。elementui框架中没有封装分页下拉框组件。因此需要自己设计一个组件。下面描述功能:
- 暂时不考虑多选下拉框,只考虑单选的情况。
- 不使用滚动下拉分页的方式,而是使用elementui中的Pagination 分页。
- 回显功能:切换分页清空用户选中的条件也是合理的,但是编辑操作时回显数据,应该显示到用户点击的那一页(目前怎么实现感到困惑)。
AI回答参考:
正常查询,然后将用户选中的数据加载第一页的第一条。
个人想法:
页面加载时,将用户选中的数据传到后端,然后定位到用户查询的页,返回给前端。不知道大家业务里面这种情况是怎么实现的?
样式效果:

封装组件代码如下:(目前没有做回显)
<template>
<el-select
v-model="copyValue"
:disabled="disabled"
filterable
clearable
placeholder="请选择"
:filter-method="filterMethod"
@clear="selectClear"
@change="updateValue"
:style="{ width: selectWidth + 'px' }"
@focus="focusMethod"
>
<el-option
v-for="item in dataList"
:key="item.value"
:label="item[labelKey]"
:value="item[valueKey]"
/>
<div style="float: right;margin-right:10px;padding-bottom: 10px">
<el-pagination
:current-page="pageInfo.pageNo"
:page-size="pageInfo.pageSize"
layout=" prev, pager, next,total"
:total="pageInfo.total"
@current-change="handleCurrentChange"
/>
</div>
</el-select>
</template>
<script>
export default {
name: 'PaginationSelect',
props: {
// 绑定值
value: String,
// 下拉列表
dataList: Array,
// option的label在列表中对应的key
labelKey: String,
// option的value在列表中对应的key
valueKey: String,
// 分页信息
pageInfo: Object,
disabled: false,
elSelectClass: Boolean,
selectWidth: {
type: [String, Number],
default: 300
}
},
data() {
return {
copyValue: this.value
}
},
watch: {
value: {
handler: function(val) {
this.copyValue = val
},
deep: true
}
},
methods: {
// 更新值
updateValue(val) {
this.$emit('selectChange', this.copyValue)
},
filterMethod: function(query) {
this.copyValue = query
this.$emit('selectPageChange', 1, this.pageInfo.pageSize, query)
},
focusMethod: function() {
if (!this.dataList || (this.dataList && this.dataList.length === 0)) {
this.filterMethod()
}
},
selectClear: function() {
this.$emit('selectPageChange', 1, this.pageInfo.pageSize, '')
},
// 翻页
handleCurrentChange(val) {
this.$emit('selectPageChange', val, this.pageInfo.pageSize)
this.copyValue = ''
},
resetFormFields() {
this.copyValue = ''
},
openEdit(value) {
this.copyValue = value
}
}
}
</script>
<style scoped>
.elPaginationSelect{
width: 100%;
border: none !important;
}
.elPaginationSelect .el-input input{
border: none !important;
width: 100%;
}
</style>
