294 lines
8.4 KiB
Vue
294 lines
8.4 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref } from "vue";
|
|
import {
|
|
Avatar,
|
|
AvatarFallback,
|
|
AvatarImage,
|
|
Button,
|
|
Checkbox,
|
|
CheckboxContent,
|
|
CheckboxControl,
|
|
CheckboxIndicator,
|
|
Chip,
|
|
Table,
|
|
type TableKey,
|
|
TableBody,
|
|
TableCell,
|
|
TableColumn,
|
|
TableContent,
|
|
TableHeader,
|
|
TableRow,
|
|
TableScrollContainer,
|
|
TableSortableColumnHeader,
|
|
} from "@heroui-vue/components";
|
|
import { IconCopy, IconEye, IconPencil, IconTrash } from "@tabler/icons-vue";
|
|
|
|
interface User {
|
|
id: number;
|
|
name: string;
|
|
image_url: string;
|
|
role: string;
|
|
status: "在职" | "未激活" | "休假";
|
|
email: string;
|
|
}
|
|
|
|
const statusColorMap: Record<string, "success" | "danger" | "warning"> = {
|
|
休假: "warning",
|
|
在职: "success",
|
|
未激活: "danger",
|
|
};
|
|
|
|
const users: User[] = [
|
|
{
|
|
email: "kate@acme.com",
|
|
id: 4586932,
|
|
image_url:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
|
|
name: "Kate Moore",
|
|
role: "首席执行官",
|
|
status: "在职",
|
|
},
|
|
{
|
|
email: "john@acme.com",
|
|
id: 5273849,
|
|
image_url:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
|
|
name: "John Smith",
|
|
role: "首席技术官",
|
|
status: "在职",
|
|
},
|
|
{
|
|
email: "sara@acme.com",
|
|
id: 7492836,
|
|
image_url:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
|
|
name: "Sara Johnson",
|
|
role: "首席营销官",
|
|
status: "休假",
|
|
},
|
|
{
|
|
email: "michael@acme.com",
|
|
id: 8293746,
|
|
image_url:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
|
|
name: "Michael Brown",
|
|
role: "首席财务官",
|
|
status: "在职",
|
|
},
|
|
{
|
|
email: "emily@acme.com",
|
|
id: 1234567,
|
|
image_url:
|
|
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
|
|
name: "Emily Davis",
|
|
role: "产品经理",
|
|
status: "未激活",
|
|
},
|
|
];
|
|
|
|
const selectedKeys = ref<TableKey[]>([]);
|
|
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 };
|
|
}
|
|
}
|
|
function initials(name: string) {
|
|
return name
|
|
.split(" ")
|
|
.map((n) => n[0])
|
|
.join("");
|
|
}
|
|
|
|
const selectableIds = computed(() =>
|
|
sortedUsers.value.map((u) => String(u.id)),
|
|
);
|
|
const allChecked = computed(
|
|
() =>
|
|
selectableIds.value.length > 0 &&
|
|
selectableIds.value.every((id) => selectedKeys.value.includes(id)),
|
|
);
|
|
const someChecked = computed(
|
|
() =>
|
|
!allChecked.value &&
|
|
selectableIds.value.some((id) => selectedKeys.value.includes(id)),
|
|
);
|
|
function toggleAll() {
|
|
selectedKeys.value = allChecked.value ? [] : [...selectableIds.value];
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Table>
|
|
<TableScrollContainer>
|
|
<TableContent
|
|
aria-label="自定义单元格表格"
|
|
class="min-w-[800px]"
|
|
selection-mode="multiple"
|
|
:selected-keys="selectedKeys"
|
|
:sort-descriptor="sortDescriptor"
|
|
@update:selected-keys="(k: TableKey[]) => (selectedKeys = k)"
|
|
@update:sort-descriptor="onSortChange"
|
|
>
|
|
<TableHeader>
|
|
<TableColumn class="pe-0">
|
|
<Checkbox
|
|
aria-label="全选"
|
|
:is-selected="allChecked"
|
|
:is-indeterminate="someChecked"
|
|
@update:checked="toggleAll"
|
|
>
|
|
<CheckboxContent>
|
|
<CheckboxControl>
|
|
<CheckboxIndicator />
|
|
</CheckboxControl>
|
|
</CheckboxContent>
|
|
</Checkbox>
|
|
</TableColumn>
|
|
<TableColumn
|
|
id="id"
|
|
allows-sorting
|
|
is-row-header
|
|
class="after:hidden"
|
|
>
|
|
<TableSortableColumnHeader :sort-direction="dirOf('id')">
|
|
员工 ID
|
|
</TableSortableColumnHeader>
|
|
</TableColumn>
|
|
<TableColumn id="name" 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 class="text-end">操作</TableColumn>
|
|
</TableHeader>
|
|
<TableBody>
|
|
<TableRow
|
|
v-for="user in sortedUsers"
|
|
:key="user.id"
|
|
:row-key="String(user.id)"
|
|
>
|
|
<TableCell class="pe-0">
|
|
<Checkbox
|
|
:aria-label="`选择 ${user.name}`"
|
|
variant="secondary"
|
|
:is-selected="selectedKeys.includes(String(user.id))"
|
|
@update:checked="
|
|
() => {
|
|
const id = String(user.id);
|
|
selectedKeys = selectedKeys.includes(id)
|
|
? selectedKeys.filter((k) => k !== id)
|
|
: [...selectedKeys, id];
|
|
}
|
|
"
|
|
>
|
|
<CheckboxContent>
|
|
<CheckboxControl>
|
|
<CheckboxIndicator />
|
|
</CheckboxControl>
|
|
</CheckboxContent>
|
|
</Checkbox>
|
|
</TableCell>
|
|
<TableCell class="font-medium">
|
|
<div class="flex items-center gap-2">
|
|
#{{ user.id }}
|
|
<Button
|
|
is-icon-only
|
|
:aria-label="`复制 ID #${user.id}`"
|
|
size="sm"
|
|
variant="ghost"
|
|
>
|
|
<IconCopy class="size-4 text-muted" aria-hidden="true" />
|
|
</Button>
|
|
</div>
|
|
</TableCell>
|
|
<TableCell>
|
|
<div class="flex items-center gap-3">
|
|
<Avatar size="sm">
|
|
<AvatarImage :src="user.image_url" />
|
|
<AvatarFallback>{{ initials(user.name) }}</AvatarFallback>
|
|
</Avatar>
|
|
<div class="flex flex-col">
|
|
<span class="text-xs">{{ user.name }}</span>
|
|
<span class="text-xs text-muted">{{ user.email }}</span>
|
|
</div>
|
|
</div>
|
|
</TableCell>
|
|
<TableCell class="min-w-52">{{ user.role }}</TableCell>
|
|
<TableCell class="min-w-25">
|
|
<Chip
|
|
:color="statusColorMap[user.status]"
|
|
size="sm"
|
|
variant="soft"
|
|
>
|
|
{{ user.status }}
|
|
</Chip>
|
|
</TableCell>
|
|
<TableCell>
|
|
<div class="flex items-center gap-1">
|
|
<Button
|
|
is-icon-only
|
|
:aria-label="`查看 ${user.name}`"
|
|
size="sm"
|
|
variant="tertiary"
|
|
>
|
|
<IconEye class="size-4" aria-hidden="true" />
|
|
</Button>
|
|
<Button
|
|
is-icon-only
|
|
:aria-label="`编辑 ${user.name}`"
|
|
size="sm"
|
|
variant="tertiary"
|
|
>
|
|
<IconPencil class="size-4" aria-hidden="true" />
|
|
</Button>
|
|
<Button
|
|
is-icon-only
|
|
:aria-label="`删除 ${user.name}`"
|
|
size="sm"
|
|
variant="danger-soft"
|
|
>
|
|
<IconTrash class="size-4" aria-hidden="true" />
|
|
</Button>
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableBody>
|
|
</TableContent>
|
|
</TableScrollContainer>
|
|
</Table>
|
|
</template>
|