图片预览

滚轮缩放 · 拖动移动 · Esc 关闭

Skip to content

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加载状态booleanfalse
treeOption树形表格配置object{}
showSummaryStatus是否显示合计行booleanfalse
summaryColumns合计列配置object{}
summaryMethod自定义合计方法Function-

分页相关 ​

参数说明类型默认值
pagination分页配置Pagination{ currentPage: 1, pageSize: 10, total: 0 }
isPagination是否显示分页booleantrue
pageSizes每页条数选项number[][10, 20, 50, 100]
autoCalculatePageSize是否自动计算每页条数booleantrue

自动计算分页说明:

  • 当 autoCalculatePageSize 为 true 时,组件会根据表格高度自动计算合适的每页条数
  • 会从 pageSizes 中选择最接近且不超过计算值的选项
  • 计算公式:Math.floor((窗口高度 - calcHeight - 300) / 行高)

TableHeader 组件相关 ​

参数说明类型默认值
showZebra显示斑马纹设置booleantrue
showBorder显示边框设置booleantrue
showHeaderBackground显示表头背景设置booleantrue
fullClass全屏容器类名string'art-page-view'
layout组件布局string'search,refresh,size,fullscreen,columns,settings'
calcHeight计算高度偏移量number0

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" />

注意事项 ​

  1. 列配置中的 checked 属性:用于控制列的显示/隐藏,默认为 true
  2. 自动计算分页:首次加载时生效,后续改变窗口大小不会重新计算
  3. 插槽透传:所有非组件级的插槽都会自动透传给内部的 BaseTable 组件
  4. 分页数据同步:组件内部会自动维护分页状态,也可以通过 update:pagination 事件同步到外部

TypeScript 支持 ​

组件已完整支持 TypeScript,所有接口类型都已定义:

typescript
import type { SearchConfigItem, ColumnOption, Pagination } from '@/components/Table/motaiList.vue'