Skip to content

Commit

Permalink
feat: pure-table添加动态表头示例
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxian521 committed Sep 23, 2024
1 parent ab39864 commit 0004f13
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/views/table/high/excel/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ const { columns, dataList, exportExcel } = useColumns();

<template>
<div>
<el-button
type="primary"
class="mb-[20px] float-right"
@click="exportExcel"
>
<el-button type="primary" class="mb-[20px]" @click="exportExcel">
导出
</el-button>
<pure-table row-key="id" border :data="dataList" :columns="columns" />
Expand Down
57 changes: 57 additions & 0 deletions src/views/table/high/header/columns.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { tableData } from "../data";
import { ref, onMounted } from "vue";
import { clone, delay } from "@pureadmin/utils";

export function useColumns() {
const dataList = ref([]);
const columns = ref<TableColumnList>([
{
label: "日期",
prop: "date"
},
{
label: "姓名",
prop: "name"
},
{
label: "地址",
prop: "address"
}
]);

function onChange() {
// 动态表头只需给 columns.value 重新赋值即可,如下
columns.value = [
{
label: "日期" + Math.round(Math.random() * 99),
prop: "date"
},
{
label: Math.round(Math.random() * 99) + "姓名",
prop: "name"
},
{
label: "地址",
prop: "address"
}
];
}

onMounted(() => {
delay(600).then(() => {
const newList = [];
Array.from({ length: 6 }).forEach(() => {
newList.push(clone(tableData, true));
});
newList.flat(Infinity).forEach((item, index) => {
dataList.value.push({ id: index, ...item });
});
});
});

return {
columns,
dataList,
onChange
};
}
25 changes: 25 additions & 0 deletions src/views/table/high/header/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { ref } from "vue";
import { useColumns } from "./columns";
const tableRef = ref();
const { columns, dataList, onChange } = useColumns();
</script>

<template>
<div>
<el-button type="primary" class="mb-[20px]" @click="onChange">
切换表头
</el-button>
<pure-table
ref="tableRef"
border
row-key="id"
alignWhole="center"
showOverflowTooltip
:data="dataList"
:columns="columns"
/>
</div>
</template>
7 changes: 7 additions & 0 deletions src/views/table/high/list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Adaptive from "./adaptive/index.vue";
import Page from "./page/index.vue";
import Header from "./header/index.vue";
import RowDrag from "./drag/row/index.vue";
import ColumnDrag from "./drag/column/index.vue";
import Contextmenu from "./contextmenu/index.vue";
Expand All @@ -25,6 +26,12 @@ export const list = [
title: "分页、加载动画、动态列",
component: Page
},
{
key: "header",
content: rendContent("header"),
title: "动态表头",
component: Header
},
{
key: "tableSelect",
content: rendContent("table-select"),
Expand Down
2 changes: 1 addition & 1 deletion src/views/table/high/prints/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { columns, dataList, print, cellStyle, rowStyle, headerCellStyle } =

<template>
<div>
<el-button type="primary" class="mb-[20px] float-right" @click="print">
<el-button type="primary" class="mb-[20px]" @click="print">
打印
</el-button>
<!-- rowHoverBgColor="transparent" 鼠标经过行时,去掉行的背景色 -->
Expand Down

0 comments on commit 0004f13

Please sign in to comment.