Appearance
MotaiList 组件使用文档
文档中心同步副本 · 源文件:
muotai-frontend/src/components/Table/motaiList.md。修改实现时请先更新源文件,再运行npm run docs:sync-source。
概述
MotaiList 是一个基于 tableHeader.vue 和 table.vue 封装的完整列表组件,包含查询表单、表格和分页三个部分,每个部分都支持插槽自定义。
组件结构
┌─────────────────────────────────────┐
│ Header (插槽 - 默认查询表单) │
├─────────────────────────────────────┤
│ Table Header (tableHeader.vue) │
│ Table Body (table.vue) │
├─────────────────────────────────────┤
│ Footer (插槽 - 默认分页) │
└─────────────────────────────────────┘基础使用
vue
<template>
<MotaiList
:search-config="searchConfig"
:columns="columns"
:data="tableData"
:loading="loading"
:pagination="pagination"
@search="handleSearch"
@page-change="handlePageChange"
/>
</template>
<script setup>
import MotaiList from '@/components/Table/motaiList.vue'
const searchConfig = [{ label: '用户名', prop: 'username', type: 'input' }]
const columns = [{ prop: 'username', label: '用户名', checked: true }]
const tableData = ref([])
const pagination = reactive({ currentPage: 1, pageSize: 10, total: 0 })
const loading = ref(false)
const handleSearch = (searchForm) => {
// 处理搜索逻辑
}
const handlePageChange = (page) => {
pagination.currentPage = page
}
</script>Props 属性
查询表单相关
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| searchConfig | 查询表单配置 | SearchConfigItem[] | [] |
SearchConfigItem 配置项
typescript
interface SearchConfigItem {
label?: string // 字段标签
prop: string // 字段名
type: 'input' | 'select' | 'date' | 'daterange' | 'datetime' // 控件类型
placeholder?: string // 占位符
options?: Array<{
// 下拉选项(type='select' 时使用)
label: string
value: any
}>
}支持的控件类型:
input: 输入框select: 下拉选择date: 日期选择daterange: 日期范围选择datetime: 日期时间选择
表格相关
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| columns | 表格列配置 | ColumnOption[] | [] |
| data | 表格数据 | any[] | [] |
| loading | 加载状态 | boolean | false |
| treeOption | 树形表格配置 | object | {} |
| showSummaryStatus | 是否显示合计行 | boolean | false |
| summaryColumns | 合计列配置 | object | {} |
| summaryMethod | 自定义合计方法 | Function | - |
分页相关
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| pagination | 分页配置 | Pagination | { currentPage: 1, pageSize: 10, total: 0 } |
| isPagination | 是否显示分页 | boolean | true |
| pageSizes | 每页条数选项 | number[] | [10, 20, 50, 100] |
| autoCalculatePageSize | 是否自动计算每页条数 | boolean | true |
自动计算分页说明:
- 当
autoCalculatePageSize为true时,组件会根据表格高度自动计算合适的每页条数 - 会从
pageSizes中选择最接近且不超过计算值的选项 - 计算公式:
Math.floor((窗口高度 - calcHeight - 300) / 行高)
TableHeader 组件相关
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| showZebra | 显示斑马纹设置 | boolean | true |
| showBorder | 显示边框设置 | boolean | true |
| showHeaderBackground | 显示表头背景设置 | boolean | true |
| fullClass | 全屏容器类名 | string | 'art-page-view' |
| layout | 组件布局 | string | 'search,refresh,size,fullscreen,columns,settings' |
| calcHeight | 计算高度偏移量 | number | 0 |
Events 事件
| 事件名 | 说明 | 参数 | | ----------------- | ---------------- | ------------------------- | --- | | search | 点击查询按钮 | searchForm: object | | reset | 点击重置按钮 | searchForm: object | | refresh | 点击刷新按钮 | - | | header-search | 点击表头搜索按钮 | - | | selection-change | 选择项改变 | selection: any[] | | page-change | 页码改变 | page: number | | size-change | 每页条数改变 | size: number | | sort-change | 排序改变 | { column, prop, order } | | row-click | 行点击 | row: any | | update:pagination | 分页数据更新 | pagination: Pagination | |
Slots 插槽
组件级插槽
| 插槽名 | 说明 | 作用域参数 |
|---|---|---|
| header | 自定义头部区域 | - |
| table | 自定义表格区域 | { data, loading, columns } |
| footer | 自定义底部区域 | { pagination, total } |
| tableHeaderLeft | 表头左侧区域 | - |
| tableHeaderRight | 表头右侧区域 | - |
| searchExtra | 搜索栏额外按钮 | - |
表格列插槽
所有表格列的插槽都会自动透传到内部的 BaseTable 组件。
vue
<!-- 使用列插槽 -->
<template #columnProp="{ row, $index }">
<span>{{ row.columnProp }}</span>
</template>
<!-- 使用列头插槽 -->
<template #columnPropHeader>
<span>自定义表头</span>
</template>暴露的方法和属性
通过 ref 可以访问组件暴露的方法和属性:
vue
<template>
<MotaiList ref="motaiListRef" />
</template>
<script setup>
const motaiListRef = ref()
// 访问搜索表单
console.log(motaiListRef.value.searchForm)
// 手动触发搜索
motaiListRef.value.handleSearch()
// 手动触发重置
motaiListRef.value.handleReset()
// 访问分页数据
console.log(motaiListRef.value.internalPagination)
// 访问表格 ref
console.log(motaiListRef.value.tableRef)
</script>使用示例
1. 基础列表
vue
<template>
<MotaiList
:search-config="searchConfig"
:columns="columns"
:data="tableData"
:loading="loading"
:pagination="pagination"
@search="handleSearch"
@page-change="handlePageChange"
@size-change="handleSizeChange"
/>
</template>
<script setup>
const searchConfig = [
{ label: '用户名', prop: 'username', type: 'input' },
{
label: '状态',
prop: 'status',
type: 'select',
options: [
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 }
]
}
]
const columns = [
{ type: 'selection', width: 50, checked: true },
{ type: 'index', label: '序号', width: 60, checked: true },
{ prop: 'username', label: '用户名', checked: true },
{ prop: 'status', label: '状态', checked: true }
]
</script>2. 自定义表格列
vue
<template>
<MotaiList :columns="columns" :data="tableData">
<!-- 自定义状态列 -->
<template #status="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
{{ row.status === 1 ? '启用' : '禁用' }}
</el-tag>
</template>
<!-- 自定义操作列 -->
<template #action="{ row }">
<el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
<el-button type="danger" link @click="handleDelete(row)">删除</el-button>
</template>
</MotaiList>
</template>3. 添加额外按钮
vue
<template>
<MotaiList>
<!-- 表头左侧按钮 -->
<template #tableHeaderLeft>
<el-button type="primary" @click="handleAdd">新增</el-button>
<el-button @click="handleExport">导出</el-button>
</template>
<!-- 搜索栏额外按钮 -->
<template #searchExtra>
<el-button @click="handleAdvancedSearch">高级搜索</el-button>
</template>
</MotaiList>
</template>4. 完全自定义头部
vue
<template>
<MotaiList>
<!-- 自定义头部 -->
<template #header>
<div class="custom-header">
<h2>自定义标题</h2>
<el-button type="primary">自定义按钮</el-button>
</div>
</template>
</MotaiList>
</template>5. 自定义分页
vue
<template>
<MotaiList>
<template #footer="{ pagination, total }">
<div class="custom-footer">
<span>共 {{ total }} 条数据</span>
<el-pagination
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
:total="total"
/>
</div>
</template>
</MotaiList>
</template>6. 树形表格
vue
<template>
<MotaiList
:columns="columns"
:data="treeData"
:tree-option="{
rowKey: 'id',
children: 'children',
defaultExpandAll: false
}"
/>
</template>7. 显示合计行
vue
<template>
<MotaiList
:columns="columns"
:data="tableData"
:show-summary-status="true"
:summary-columns="{
price: true, // 计算合计
quantity: true, // 计算合计
name: false // 不计算合计
}"
/>
</template>最佳实践
1. 数据加载流程
vue
<script setup>
const loading = ref(false)
const tableData = ref([])
const pagination = reactive({
currentPage: 1,
pageSize: 10,
total: 0
})
// 加载数据
const loadData = async () => {
loading.value = true
try {
const res = await api.getList({
page: pagination.currentPage,
size: pagination.pageSize,
...searchForm.value
})
tableData.value = res.data
pagination.total = res.total
} finally {
loading.value = false
}
}
// 搜索
const handleSearch = (searchForm) => {
pagination.currentPage = 1
loadData()
}
// 页码改变
const handlePageChange = (page) => {
pagination.currentPage = page
loadData()
}
// 每页条数改变
const handleSizeChange = (size) => {
pagination.pageSize = size
pagination.currentPage = 1
loadData()
}
// 初始加载
onMounted(() => {
loadData()
})
</script>2. 禁用自动计算分页
如果不需要根据高度自动计算分页条数,可以设置 autoCalculatePageSize 为 false:
vue
<MotaiList
:auto-calculate-page-size="false"
:pagination="{ currentPage: 1, pageSize: 20, total: 0 }"
/>3. 表格高度计算
如果页面有固定高度的元素(如页头、面包屑等),可以通过 calcHeight 属性告诉组件需要减去的高度:
vue
<MotaiList :calc-height="200" />注意事项
- 列配置中的
checked属性:用于控制列的显示/隐藏,默认为true - 自动计算分页:首次加载时生效,后续改变窗口大小不会重新计算
- 插槽透传:所有非组件级的插槽都会自动透传给内部的
BaseTable组件 - 分页数据同步:组件内部会自动维护分页状态,也可以通过
update:pagination事件同步到外部
TypeScript 支持
组件已完整支持 TypeScript,所有接口类型都已定义:
typescript
import type { SearchConfigItem, ColumnOption, Pagination } from '@/components/Table/motaiList.vue'