-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-table-view-options.svelte
More file actions
65 lines (61 loc) · 1.99 KB
/
data-table-view-options.svelte
File metadata and controls
65 lines (61 loc) · 1.99 KB
1
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
56
57
58
59
60
61
62
63
64
65
<script module lang="ts">
export interface DataTableViewOptionsProps<TData> {
table: Table<TData>;
default_hidden_columns?: (keyof TData)[];
}
</script>
<script lang="ts" generics="TData">
import { ChevronDown, TwoColumns } from "$/assets/icons";
import type { Table } from "@tanstack/table-core";
import { Button } from "$/components/ui/button/index.js";
import * as DropdownMenu from "$/components/ui/dropdown-menu/index.js";
import { untrack } from "svelte";
let { table, default_hidden_columns = $bindable([]) }: DataTableViewOptionsProps<TData> =
$props();
$effect(() => {
if (!default_hidden_columns) return;
untrack(() => {
table.getAllColumns().forEach((col) => {
if (default_hidden_columns.includes(col.id as keyof TData)) {
col.toggleVisibility(false);
}
});
});
});
</script>
<DropdownMenu.Root>
<DropdownMenu.Trigger>
{#snippet child({ props })}
<Button variant="ghost" size="sm" {...props}>
<TwoColumns />
<span class="hidden lg:inline">Customize Columns</span>
<span class="lg:hidden">Columns</span>
<ChevronDown />
</Button>
{/snippet}
</DropdownMenu.Trigger>
<DropdownMenu.Content align="end" class="w-56">
{#each table
.getAllColumns()
.filter((col) => typeof col.accessorFn !== "undefined" && col.getCanHide()) as column (column.id)}
<DropdownMenu.CheckboxItem
class="capitalize"
checked={column.getIsVisible()}
onCheckedChange={(value) => {
if (
table
.getAllColumns()
.filter((col) => typeof col.accessorFn !== "undefined" && col.getCanHide())
.filter((col) => col.getIsVisible()).length <= 1 &&
!value
) {
return;
}
column.toggleVisibility(!!value);
}}
>
{column.id.replace("Formatted", "")}
</DropdownMenu.CheckboxItem>
{/each}
</DropdownMenu.Content>
</DropdownMenu.Root>