表格顶栏
标题栏
通过 title 属性设置标题栏。
<script setup lang="ts">
import type { TableColumn } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data" title="我是表格的标题栏" />
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
顶栏插槽
通过 head-left 插槽设置顶栏左侧内容,该插槽内容会覆盖标题栏,因此可以更灵活自定义标题栏或添加按钮。
通过 head-right 插槽设置顶栏右侧内容,该插槽会覆盖工具栏。
<script setup lang="ts">
import type { TableColumn } from "@/components/pro/table";
import { CirclePlus, Upload, Download } from "@element-plus/icons-vue";
import { ProTable } from "@/components/pro/table";
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data">
<template #head-left>
<el-button type="primary" :icon="CirclePlus">新增用户</el-button>
<el-button type="primary" :icon="Upload" plain>批量添加用户</el-button>
</template>
<template #head-right>
<el-button type="primary" :icon="Download" plain>导出用户数据</el-button>
</template>
</ProTable>
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
工具栏插槽
工具栏插槽有 2 个:
head-tool-before插槽设置工具栏左侧内容head-tool-after插槽设置工具栏左侧内容
其中这两个插槽可以如下参数:
tooltipProps:ElTooltip 组件 PropshandleSizeCommand:改变表格大小事件handleRefresh:刷新表格事件handleExport:导出表格事件toggleColumnSetting:显示列设置抽屉事件
如果需要使用内置工具栏的按钮样式,请给你的 el-button 添加 head__tool-button 类名。
<script setup lang="ts">
import type { TableColumn } from "@/components/pro/table";
import { CirclePlus, Upload, Download } from "@element-plus/icons-vue";
import { ProTable } from "@/components/pro/table";
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data">
<template #head-tool-before="{ tooltipProps }">
<el-button type="primary" :icon="CirclePlus">新增用户</el-button>
<el-tooltip content="新增" v-bind="tooltipProps">
<el-button plain :icon="CirclePlus" class="head__tool-button" />
</el-tooltip>
</template>
<template #head-tool-after>
<el-button type="primary" :icon="Download" plain>导出用户数据</el-button>
</template>
</ProTable>
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
工具栏按钮权限
通过 toolButton 数组指定显示的工具按钮,通过 disabledToolButton 指禁用的工具按钮。
<script setup lang="ts">
import type { TableColumn } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable
:columns="columns"
:data="data"
:tool-button="['size', 'columnSetting', 'baseSetting']"
:disabled-tool-button="['size']"
/>
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
密度配置
通过 size 配置项可以修改表格默认密度大小,并且通过 sizeStyle 设置不同密度下 rowStyle、cellStyle、headerRowStyle、headerCellStyle 的样式。
提示
你仍然可以直接使用 ElTable 的 rowStyle、cellStyle、headerRowStyle、headerCellStyle 配置项,在 ProTable 上传入即可,但 sizeStyle 优先级更高。
<script setup lang="ts">
import type { TableColumn } from "@/components/pro/table";
import { TableSizeEnum } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const sizeStyle = {
[TableSizeEnum.Large]: {
rowStyle: { backgroundColor: "#a40ec947" },
cellStyle: { color: "red" },
},
[TableSizeEnum.Mini]: {
rowStyle: { backgroundColor: "#fffff1" },
cellStyle: { color: "green" },
headerCellStyle: { backgroundColor: "#f5f5f5" },
},
};
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data" size="mini" :size-style :headerCellStyle="{ backgroundColor: 'red' }" />
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
导出配置
通过传入 exportProps 配置项,可以自定义导出相关功能。
导出默认使用 file-saver 和 xlsx 插件进行前端导出,mode 有如下规则:
- label:Excel 表头为列配置的
label,默认值 - prop:Excel 表头为列配置的
prop - dataKey:Excel 表头为数据的
key,值得注意的是,如果数据为空,则 Excel 表头为空
如果想实现后端导出,则自行实现 exportFile 函数,参数为当前表格的数据。
<script setup lang="ts">
import type { ExportProps, TableColumn } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const exportProps: ExportProps = {
mode: "label",
fileName: "表格数据" + new Date().getTime(),
title: "请选择导出列",
// ElMessageBox 配置项
options: {
draggable: true,
},
// 自定义导出逻辑
// exportFile: data => {},
};
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data" :exportProps />
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
列配置
往 ProTable 直接传入 columnSetting 可以全局配置所有列在列配置的显示、筛选的 禁用/启用/隐藏 状态。
也可以在每一个 column 里单独配置列配置的显示、排序、筛选的 禁用/启用 状态。
<script setup lang="ts">
import type { ColumnSetting, TableColumn } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const columnSetting: ColumnSetting = {
disabledFilter: true,
};
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy", disabledHidden: true },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
disabledSortable: true,
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
disabledFilter: true,
},
{ prop: "birthday", label: "生日", disabledDragSort: true },
];
</script>
<template>
<ProTable :columns="columns" :data="data" :column-setting />
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
基础配置
通过 baseSetting 修改表格的默认基础设置,如禁用边框选择、启用斑马纹、关闭表头背景色等。
您仍然可以使用 ElementPlus 自带的属性,如边框、斑马纹开启等配置。
<script setup lang="ts">
import type { BaseSetting, TableColumn } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const baseSetting: BaseSetting = {
headerBackground: false,
disabledBorder: true,
};
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data" border :base-setting />
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
顶栏控制显隐
通过 hideHead 配置来隐藏顶栏。
通过 controlHeadColumn 来开启控制顶栏功能,该配置会在表格最后一列添加一个图标,点击后可以控制顶栏的显隐,同时 controlHeadColumnProps 可以给该列传入一个 ElTableColumn 的配置,如 width 宽度等。
<script setup lang="ts">
import type { TableColumn } from "@/components/pro/table";
import { ProTable } from "@/components/pro/table";
const data = [
{ username: "张三", gender: 1, progress: 20, birthday: "2023-01-01" },
{ username: "李四", gender: 2, progress: 40, birthday: "2023-02-01" },
{ username: "王五", gender: 1, progress: 60, birthday: "2023-03-01" },
];
const columns: TableColumn[] = [
{ prop: "username", label: "用户姓名", el: "copy" },
{
prop: "gender",
label: "性别",
el: "el-tag",
elProps: (value: number) => {
if (value === 1) return { type: "success" };
return { type: "danger" };
},
options: async () => {
return [
{ label: "男", value: 1 },
{ label: "女", value: 2 },
];
},
},
{
prop: "progress",
label: "进度",
el: "el-progress",
elSlots: {
default: ({ value }) => value + "%",
},
},
{ prop: "birthday", label: "生日" },
];
</script>
<template>
<ProTable :columns="columns" :data="data" hide-head control-head-column />
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42