参考资料: juejin.cn/post/716606…
组件使用:
- 配置表格配置项并传递到子组件
// 表格配置项
const columns: ColumnProps[] = [
{ prop: "meta.title", label: "菜单名称", align: "left", search: { el: "input" } },
{ prop: "meta.icon", label: "菜单图标" },
{ prop: "name", label: "菜单 name", search: { el: "input" } },
{ prop: "path", label: "菜单路径", width: 300, search: { el: "input" } },
{ prop: "component", label: "组件路径", width: 300 },
{ prop: "operation", label: "操作", width: 250, fixed: "right" }
]
<template>
<div class="table-box">
<ProTable ref="proTable" title="菜单列表" row-key="path" :indent="20" :columns="columns" :data="menuData">
<!-- 表格 header 按钮 -->
<template #tableHeader>
<el-button type="primary" :icon="CirclePlus">新增菜单 </el-button>
</template>
<!-- 菜单图标 -->
<template #icon="scope">
<el-icon :size="18">
<component :is="scope.row.meta.icon"></component>
</el-icon>
</template>
<!-- 菜单操作 -->
<template #operation>
<el-button type="primary" link :icon="EditPen"> 编辑 </el-button>
<el-button type="primary" link :icon="Delete"> 删除 </el-button>
</template>
</ProTable>
</div>
</template>
- 组件接收参数
// 接受父组件参数,配置默认值
const props = withDefaults(defineProps<ProTableProps>(), {
columns: () => [],
requestAuto: true,
pagination: true,
initParam: {},
border: true,
toolButton: true,
rowKey: "id",
searchCol: () => ({ xs: 1, sm: 2, md: 2, lg: 3, xl: 4 })
})
ProTableProps接口类型
export interface ProTableProps {
columns: ColumnProps[] // 列配置项 ==> 必传
data?: any[] // 静态 table data 数据,若存在则不会使用 requestApi 返回的 data ==> 非必传
requestApi?: (params: any) => Promise<any> // 请求表格数据的 api ==> 非必传
requestAuto?: boolean // 是否自动执行请求 api ==> 非必传(默认为true)
requestError?: (params: any) => void // 表格 api 请求错误监听 ==> 非必传
dataCallback?: (data: any) => any // 返回数据的回调函数,可以对数据进行处理 ==> 非必传
title?: string // 表格标题 ==> 非必传
pagination?: boolean // 是否需要分页组件 ==> 非必传(默认为true)
initParam?: any // 初始化请求参数 ==> 非必传(默认为{})
border?: boolean // 是否带有纵向边框 ==> 非必传(默认为true)
toolButton?: ("refresh" | "setting" | "search")[] | boolean // 是否显示表格功能按钮 ==> 非必传(默认为true)
rowKey?: string // 行数据的 Key,用来优化 Table 的渲染,当表格数据多选时,所指定的 id ==> 非必传(默认为 id)
searchCol?: number | Record<BreakPoint, number> // 表格搜索项 每列占比配置 ==> 非必传 { xs: 1, sm: 2, md: 2, lg: 3, xl: 4 }
}
ProTable组件由五部分组成这里我们逐一讲解
表格搜索区域
- 使用及接收参数
<!-- 查询表单-->
<SearchForm
v-show="isShowSearch"
:search="_search"
:reset="_reset"
:columns="searchColumns"
:search-param="searchParam"
:search-col="searchCol"
/>
interface ProTableProps {
columns?: ColumnProps[] // 搜索配置列
searchParam?: { [key: string]: any } // 搜索参数
searchCol: number | Record<BreakPoint, number>
search: (params: any) => void // 搜索方法
reset: (params: any) => void // 重置方法
}
// 默认值
const props = withDefaults(defineProps<ProTableProps>(), {
columns: () => [],
searchParam: () => ({})
})
- 设置参数:
- searchColumns搜索参数
- searchCol
- searchColumns搜索参数