139 lines
3.4 KiB
Vue
139 lines
3.4 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref } from "vue";
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableColumn,
|
|
TableContent,
|
|
TableHeader,
|
|
TableRow,
|
|
TableScrollContainer,
|
|
TableSortableColumnHeader,
|
|
} from "@heroui-vue/components";
|
|
|
|
interface User {
|
|
id: number;
|
|
name: string;
|
|
role: string;
|
|
status: string;
|
|
email: string;
|
|
}
|
|
|
|
const users: User[] = [
|
|
{
|
|
email: "kate@acme.com",
|
|
id: 1,
|
|
name: "Kate Moore",
|
|
role: "首席执行官",
|
|
status: "在职",
|
|
},
|
|
{
|
|
email: "john@acme.com",
|
|
id: 2,
|
|
name: "John Smith",
|
|
role: "首席技术官",
|
|
status: "在职",
|
|
},
|
|
{
|
|
email: "sara@acme.com",
|
|
id: 3,
|
|
name: "Sara Johnson",
|
|
role: "首席营销官",
|
|
status: "休假",
|
|
},
|
|
{
|
|
email: "michael@acme.com",
|
|
id: 4,
|
|
name: "Michael Brown",
|
|
role: "首席财务官",
|
|
status: "在职",
|
|
},
|
|
{
|
|
email: "emily@acme.com",
|
|
id: 5,
|
|
name: "Emily Davis",
|
|
role: "产品经理",
|
|
status: "未激活",
|
|
},
|
|
];
|
|
|
|
const sortDescriptor = ref<{
|
|
column: string;
|
|
direction: "ascending" | "descending";
|
|
}>({
|
|
column: "name",
|
|
direction: "ascending",
|
|
});
|
|
|
|
const sortedUsers = computed(() => {
|
|
const col = sortDescriptor.value.column as keyof User;
|
|
return [...users].sort((a, b) => {
|
|
let cmp = String(a[col]).localeCompare(String(b[col]));
|
|
if (sortDescriptor.value.direction === "descending") cmp *= -1;
|
|
return cmp;
|
|
});
|
|
});
|
|
|
|
function dirOf(id: string): "ascending" | "descending" | undefined {
|
|
return sortDescriptor.value.column === id
|
|
? sortDescriptor.value.direction
|
|
: undefined;
|
|
}
|
|
function onSortChange(
|
|
d: { column: string; direction: "ascending" | "descending" | "none" } | null,
|
|
) {
|
|
if (d && d.direction !== "none") {
|
|
sortDescriptor.value = { column: d.column, direction: d.direction };
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Table>
|
|
<TableScrollContainer>
|
|
<TableContent
|
|
aria-label="可排序表格"
|
|
class="min-w-[600px]"
|
|
:sort-descriptor="sortDescriptor"
|
|
@update:sort-descriptor="onSortChange"
|
|
>
|
|
<TableHeader>
|
|
<TableColumn id="name" is-row-header allows-sorting>
|
|
<TableSortableColumnHeader :sort-direction="dirOf('name')">
|
|
姓名
|
|
</TableSortableColumnHeader>
|
|
</TableColumn>
|
|
<TableColumn id="role" allows-sorting>
|
|
<TableSortableColumnHeader :sort-direction="dirOf('role')">
|
|
角色
|
|
</TableSortableColumnHeader>
|
|
</TableColumn>
|
|
<TableColumn id="status" allows-sorting>
|
|
<TableSortableColumnHeader :sort-direction="dirOf('status')">
|
|
状态
|
|
</TableSortableColumnHeader>
|
|
</TableColumn>
|
|
<TableColumn id="email" allows-sorting>
|
|
<TableSortableColumnHeader :sort-direction="dirOf('email')">
|
|
邮箱
|
|
</TableSortableColumnHeader>
|
|
</TableColumn>
|
|
</TableHeader>
|
|
<TableBody>
|
|
<TableRow
|
|
v-for="user in sortedUsers"
|
|
:key="user.id"
|
|
:row-key="String(user.id)"
|
|
>
|
|
<TableCell>{{ user.name }}</TableCell>
|
|
<TableCell>{{ user.role }}</TableCell>
|
|
<TableCell>{{ user.status }}</TableCell>
|
|
<TableCell>{{ user.email }}</TableCell>
|
|
</TableRow>
|
|
</TableBody>
|
|
</TableContent>
|
|
</TableScrollContainer>
|
|
</Table>
|
|
</template>
|