组件封装
This commit is contained in:
parent
db9b98af8e
commit
2fded9a646
|
|
@ -0,0 +1,116 @@
|
|||
<template>
|
||||
<div class="pagination-container">
|
||||
<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="pageSizes"
|
||||
:total="total" :disabled="disabled" :background="background" :small="small"
|
||||
:hide-on-single-page="hideOnSinglePage" :layout="layout" @size-change="handleSizeChange"
|
||||
@current-change="handleCurrentChange" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
|
||||
// 定义组件接收的属性
|
||||
const props = defineProps({
|
||||
// 当前页码(双向绑定)
|
||||
modelValue: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 1
|
||||
},
|
||||
// 每页显示条数(双向绑定)
|
||||
pageSizeValue: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 10
|
||||
},
|
||||
// 总数据条数
|
||||
total: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0
|
||||
},
|
||||
// 可选的每页条数
|
||||
pageSizes: {
|
||||
type: Array,
|
||||
default: () => [10, 20, 50, 100]
|
||||
},
|
||||
// 是否禁用分页
|
||||
disabled: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 是否显示分页背景
|
||||
background: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
// 是否为小型分页
|
||||
small: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 只有一页时是否隐藏
|
||||
hideOnSinglePage: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
// 分页布局(Element Plus 原生配置)
|
||||
layout: {
|
||||
type: String,
|
||||
default: 'total, sizes, prev, pager, next, jumper'
|
||||
}
|
||||
})
|
||||
|
||||
// 定义派发的事件
|
||||
const emit = defineEmits([
|
||||
// 页码变更事件(v-model 双向绑定)
|
||||
'update:modelValue',
|
||||
// 每页条数变更事件(v-model 双向绑定)
|
||||
'update:pageSizeValue',
|
||||
// 分页变更统一回调(方便父组件处理)
|
||||
'pagination-change'
|
||||
])
|
||||
|
||||
// 内部当前页(同步props)
|
||||
const currentPage = props.modelValue
|
||||
// 内部每页条数(同步props)
|
||||
const pageSize = props.pageSizeValue
|
||||
|
||||
// 监听当前页变化,派发事件
|
||||
watch(
|
||||
() => currentPage,
|
||||
(newVal) => {
|
||||
emit('update:modelValue', newVal)
|
||||
emit('pagination-change', { currentPage: newVal, pageSize })
|
||||
}
|
||||
)
|
||||
|
||||
// 监听每页条数变化,派发事件
|
||||
watch(
|
||||
() => pageSize,
|
||||
(newVal) => {
|
||||
emit('update:pageSizeValue', newVal)
|
||||
emit('pagination-change', { currentPage, pageSize: newVal })
|
||||
}
|
||||
)
|
||||
|
||||
// 每页条数变更处理(兼容Element Plus原生事件)
|
||||
const handleSizeChange = (val: number) => {
|
||||
emit('update:pageSizeValue', val)
|
||||
emit('pagination-change', { currentPage, pageSize: val })
|
||||
}
|
||||
|
||||
// 当前页变更处理(兼容Element Plus原生事件)
|
||||
const handleCurrentChange = (val: number) => {
|
||||
emit('update:modelValue', val)
|
||||
emit('pagination-change', { currentPage: val, pageSize })
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.pagination-container {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
}
|
||||
</style>
|
||||
|
|
@ -0,0 +1,143 @@
|
|||
<template>
|
||||
<div class="search-container">
|
||||
<el-form :model="searchForm" inline @submit.prevent>
|
||||
<!-- 动态生成的表单项 -->
|
||||
<template v-for="item in searchOptions" :key="item.prop">
|
||||
<el-form-item :label="item.label" :prop="item.prop" v-if="!item.slot"
|
||||
:style="{ minWidth: item.minWidth }">
|
||||
<!-- 输入框 -->
|
||||
<el-input v-if="item.type === 'input'" v-model="searchForm[item.prop]"
|
||||
:placeholder="item.placeholder || `请输入${item.label}`" :clearable="item.clearable ?? true"
|
||||
:disabled="item.disabled" />
|
||||
|
||||
<!-- 选择器 -->
|
||||
<el-select v-else-if="item.type === 'select'" v-model="searchForm[item.prop]"
|
||||
:placeholder="item.placeholder || `请选择${item.label}`" :clearable="item.clearable ?? true"
|
||||
:disabled="item.disabled">
|
||||
<el-option v-for="option in item.options" :key="option.value" :label="option.label"
|
||||
:value="option.value" />
|
||||
</el-select>
|
||||
|
||||
<!-- 日期选择器(单个/范围) -->
|
||||
<el-date-picker v-else-if="item.type === 'date'" v-model="searchForm[item.prop]"
|
||||
:type="item.dateType || 'date'" :range-separator="item.rangeSeparator || '至'"
|
||||
:start-placeholder="item.startPlaceholder || '开始日期'"
|
||||
:end-placeholder="item.endPlaceholder || '结束日期'"
|
||||
:value-format="item.valueFormat || 'YYYY-MM-DD'" :clearable="item.clearable ?? true"
|
||||
:disabled="item.disabled" />
|
||||
|
||||
<!-- 单选框组 -->
|
||||
<el-radio-group v-else-if="item.type === 'radio'" v-model="searchForm[item.prop]"
|
||||
:disabled="item.disabled">
|
||||
<el-radio v-for="option in item.options" :key="option.value" :label="option.value">
|
||||
{{ option.label }}
|
||||
</el-radio>
|
||||
</el-radio-group>
|
||||
|
||||
<!-- 复选框组 -->
|
||||
<el-checkbox-group v-else-if="item.type === 'checkbox'" v-model="searchForm[item.prop]"
|
||||
:disabled="item.disabled">
|
||||
<el-checkbox v-for="option in item.options" :key="option.value" :label="option.value">
|
||||
{{ option.label }}
|
||||
</el-checkbox>
|
||||
</el-checkbox-group>
|
||||
</el-form-item>
|
||||
|
||||
<!-- 自定义插槽项 -->
|
||||
<el-form-item :label="item.label" :prop="item.prop" v-else>
|
||||
<slot :name="item.prop" :form="searchForm" />
|
||||
</el-form-item>
|
||||
</template>
|
||||
|
||||
<!-- 额外插槽:用于插入配置外的自定义项 -->
|
||||
<slot name="extra" :form="searchForm" />
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="$emit('search')">
|
||||
<el-icon>
|
||||
<Search />
|
||||
</el-icon>
|
||||
{{ buttonConfig.searchText || '查询' }}
|
||||
</el-button>
|
||||
<el-button @click="$emit('reset')">
|
||||
{{ buttonConfig.resetText || '重置' }}
|
||||
</el-button>
|
||||
<!-- 按钮区插槽 -->
|
||||
<slot name="button" :form="searchForm" />
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { Search } from '@element-plus/icons-vue'
|
||||
|
||||
// 定义配置项类型
|
||||
interface SearchOption {
|
||||
prop: string; // 绑定的表单字段名
|
||||
label: string; // 标签文本
|
||||
type: 'input' | 'select' | 'date' | 'radio' | 'checkbox'; // 表单类型
|
||||
slot?: boolean; // 是否为自定义插槽项
|
||||
placeholder?: string; // 占位符
|
||||
clearable?: boolean; // 是否显示清空按钮
|
||||
disabled?: boolean; // 是否禁用
|
||||
options?: { label: string; value: any }[]; // 下拉/单选/复选选项
|
||||
dateType?: 'date' | 'daterange' | 'datetime' | 'datetimerange'; // 日期选择器类型
|
||||
rangeSeparator?: string; // 日期范围分隔符
|
||||
startPlaceholder?: string; // 日期范围开始占位符
|
||||
endPlaceholder?: string; // 日期范围结束占位符
|
||||
valueFormat?: string; // 日期格式
|
||||
width?: string; // 输入框宽度
|
||||
minWidth?: string; // 最小宽度
|
||||
}
|
||||
|
||||
// 按钮配置类型
|
||||
interface ButtonConfig {
|
||||
searchText?: string;
|
||||
resetText?: string;
|
||||
}
|
||||
|
||||
// 组件Props
|
||||
const props = defineProps({
|
||||
searchForm: {
|
||||
type: Object,
|
||||
required: true,
|
||||
description: '搜索表单数据对象'
|
||||
},
|
||||
searchOptions: {
|
||||
type: Array as () => SearchOption[],
|
||||
required: true,
|
||||
description: '搜索项配置数组'
|
||||
},
|
||||
buttonConfig: {
|
||||
type: Object as () => ButtonConfig,
|
||||
default: () => ({}),
|
||||
description: '按钮文本配置'
|
||||
}
|
||||
})
|
||||
|
||||
// 暴露事件
|
||||
defineEmits(['search', 'reset'])
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.search-container {
|
||||
margin-bottom: 20px;
|
||||
.el-form{
|
||||
display: flex;
|
||||
width: 100%;
|
||||
.el-form-item{
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
}
|
||||
:deep(.el-form-item) {
|
||||
margin-bottom: 0;
|
||||
margin-right: 16px;
|
||||
}
|
||||
|
||||
:deep(.el-button) {
|
||||
margin-left: 8px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -97,13 +97,13 @@ const staticMenuList = ref<ElMenuItem[]>(
|
|||
{
|
||||
id: 3,
|
||||
label: '账户管理',
|
||||
path: '3', // 对应 el-sub-menu 的 index="3"
|
||||
path: 'account', // 对应 el-sub-menu 的 index="account"
|
||||
icon: 'UserFilled', // 对应 <UserFilled /> 图标
|
||||
children: [
|
||||
{
|
||||
id: 31,
|
||||
label: '客户管理',
|
||||
path: '3-1', // index="3-1"
|
||||
path: '/account/client', // index="client"
|
||||
icon: ''
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -78,6 +78,33 @@ const routes = [
|
|||
}
|
||||
]
|
||||
},
|
||||
//模板配置
|
||||
{
|
||||
path: 'template',
|
||||
name: 'Template',
|
||||
meta: {
|
||||
title: '模板配置', // 左侧菜单显示的标题
|
||||
},
|
||||
children: []
|
||||
},
|
||||
//账户管理
|
||||
{
|
||||
path: 'account',
|
||||
name: 'Account',
|
||||
meta: {
|
||||
title: '账户管理', // 左侧菜单显示的标题
|
||||
},
|
||||
children: [
|
||||
{
|
||||
path: 'client',
|
||||
name: 'Client',
|
||||
component: () => import('@/views/account/client/index.vue'),
|
||||
meta: {
|
||||
title: '客户账户', // 左侧菜单显示的标题
|
||||
}
|
||||
},
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,135 @@
|
|||
<template>
|
||||
<div>
|
||||
<h1>客户管理</h1>
|
||||
<div class="client">
|
||||
<!-- 动态搜索组件 -->
|
||||
<ClientSearch :search-form="searchForm" :search-options="clientSearchOptions"
|
||||
:button-config="{ searchText: '搜索', resetText: '重置' }" @search="handleSearch" @reset="handleReset">
|
||||
<!-- 这里可以插入自定义插槽(如果需要) -->
|
||||
</ClientSearch>
|
||||
|
||||
<!-- 下方的表格区域(与你的图片对应) -->
|
||||
<el-table :data="tableData" border>
|
||||
<el-table-column prop="username" label="用户名" />
|
||||
<el-table-column prop="registerStartTime" label="注册开始时间" />
|
||||
<el-table-column prop="registerEndTime" label="注册结束时间" />
|
||||
<el-table-column prop="status" label="状态">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
|
||||
{{ row.status === 1 ? '开启' : '关闭' }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作">
|
||||
<template #default>
|
||||
<el-button link type="primary">搜索</el-button>
|
||||
<el-button link type="primary">重置</el-button>
|
||||
<el-button link type="primary">新增用户</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<ClientPagination v-model="currentPage" v-model:page-size-value="pageSize" :total="total"
|
||||
@pagination-change="handlePaginationChange" />
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
// 引入组件
|
||||
import ClientSearch from '@/components/common/Search.vue'
|
||||
import ClientTable from '@/components/common/Table.vue'
|
||||
import ClientPagination from '@/components/common/Pagination.vue'
|
||||
|
||||
// 引入搜索配置
|
||||
import { clientSearchOptions } from './options'
|
||||
|
||||
// 定义搜索表单(与配置项
|
||||
const searchForm = ref({
|
||||
username: '', // 用户名
|
||||
registerTime: [], // 注册时间范围(daterange 类型)
|
||||
status: '' // 状态(开启/关闭)
|
||||
})
|
||||
|
||||
// 模拟表格数据
|
||||
const tableData = ref([
|
||||
{ username: 'admin', registerStartTime: '2026-01-01', registerEndTime: '2026-01-02', status: 1 },
|
||||
{ username: 'test', registerStartTime: '2026-02-01', registerEndTime: '2026-02-02', status: 0 }
|
||||
])
|
||||
|
||||
// 3. 查询逻辑(点击「搜索」触发)
|
||||
const handleSearch = () => {
|
||||
console.log('发起查询,参数:', searchForm)
|
||||
|
||||
// 实际项目中,需要将 registerTime 数组拆分为 start 和 end
|
||||
const params = {
|
||||
...searchForm.value,
|
||||
registerStartTime: searchForm.value.registerTime[0] || '',
|
||||
registerEndTime: searchForm.value.registerTime[1] || ''
|
||||
}
|
||||
delete params.registerTime // 删除原数组字段
|
||||
|
||||
// 调用接口获取列表数据
|
||||
// api.getUserList(params).then(res => {
|
||||
// tableData.value = res.list
|
||||
// })
|
||||
}
|
||||
|
||||
// 4. 重置逻辑(点击「重置」触发)
|
||||
const handleReset = () => {
|
||||
// 清空表单
|
||||
searchForm.value.username = ''
|
||||
searchForm.value.registerTime = []
|
||||
searchForm.value.status = ''
|
||||
|
||||
// 重置后自动发起一次查询
|
||||
handleSearch()
|
||||
}
|
||||
|
||||
// 页面初始化时自动查询一次
|
||||
handleSearch()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 分页核心参数
|
||||
const currentPage = ref(1) // 当前页
|
||||
const pageSize = ref(10) // 每页条数
|
||||
const total = ref(0) // 总条数
|
||||
|
||||
// 初始化加载数据
|
||||
onMounted(() => {
|
||||
fetchData()
|
||||
})
|
||||
|
||||
// 数据请求方法(父组件处理业务逻辑)
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
// 模拟接口请求:传递分页参数
|
||||
// const res = await api.getList({ page: currentPage.value, size: pageSize.value })
|
||||
// 模拟返回数据
|
||||
const res = {
|
||||
list: [/* 业务数据 */],
|
||||
total: 100 // 总条数
|
||||
}
|
||||
tableData.value = res.list
|
||||
total.value = res.total
|
||||
} catch (error) {
|
||||
console.error('数据请求失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 分页变更回调(处理业务逻辑)
|
||||
const handlePaginationChange = ({ currentPage: page, pageSize: size }) => {
|
||||
// 更新分页参数
|
||||
currentPage.value = page
|
||||
pageSize.value = size
|
||||
// 重新请求数据
|
||||
fetchData()
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
export const clientSearchOptions = [
|
||||
{
|
||||
prop: 'username',
|
||||
label: '用户名',
|
||||
type: 'input' as const,
|
||||
placeholder: '请输入用户名',
|
||||
minWidth: '120px'
|
||||
},
|
||||
{
|
||||
prop: 'registerTime',
|
||||
label: '注册开始时间', // 标签与图片一致
|
||||
type: 'date' as const,
|
||||
dateType: 'daterange' as const, // 时间范围选择器
|
||||
startPlaceholder: '开始日期',
|
||||
endPlaceholder: '结束日期',
|
||||
valueFormat: 'YYYY-MM-DD',
|
||||
minWidth: '200px'
|
||||
},
|
||||
{
|
||||
prop: 'status',
|
||||
label: '状态',
|
||||
type: 'select' as const,
|
||||
placeholder: '选择状态',
|
||||
width: '120px',
|
||||
options: [
|
||||
{ label: '全部', value: '' },
|
||||
{ label: '开启', value: 1 },
|
||||
{ label: '关闭', value: 0 }
|
||||
],
|
||||
}
|
||||
]
|
||||
Loading…
Reference in New Issue