Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Table #125

Merged
merged 24 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4dbe5f5
feat: resource table layout
Lama64 Jul 3, 2024
0d3510e
Merge branch 'refs/heads/main' into feat/resourceTableLayout
Lama64 Jul 4, 2024
1f2d781
Merge branch 'refs/heads/main' into feat/resourceTableLayout
Lama64 Jul 4, 2024
851be38
Merge branch 'refs/heads/main' into feat/resourceTableLayout
Lama64 Jul 4, 2024
84c8179
Merge branch 'refs/heads/main' into feat/resourceTableLayout
Lama64 Jul 4, 2024
a1a698d
Merge branch 'refs/heads/main' into feat/resourceTableLayout
Lama64 Jul 4, 2024
bbafc31
Merge branch 'refs/heads/main' into feat/resourceTableLayout
Lama64 Jul 4, 2024
4ecf33c
Formatted table, Table from database
Lama64 Jul 7, 2024
50480f8
added filtering, sorting and paging via the server
Lama64 Jul 10, 2024
4b840fa
fixed type errrors
Lama64 Jul 11, 2024
510450c
Fixed complaints of web tests
Lama64 Jul 11, 2024
f5810a1
replaced call to loadPage function
Lama64 Jul 11, 2024
fe0616b
Added tables to all relevant resource pages
Lama64 Jul 11, 2024
3d5884e
Added action menu, delete selected entries with del key and fixed row…
Lama64 Jul 13, 2024
51f1922
Merge remote-tracking branch 'origin/main' into feat/resourceTableLayout
Lama64 Jul 14, 2024
bae33e2
Fixed multiple smaller things
Lama64 Jul 14, 2024
5ab5194
forgot some things
Lama64 Jul 14, 2024
1e2a443
Changes from comments
Lama64 Jul 14, 2024
a4d9193
Fixed lint, because im stupid
Lama64 Jul 14, 2024
114094d
Removed stores in page files.
Lama64 Jul 14, 2024
d85fec7
fixed warnings and error code
SuperMarcomen Jul 16, 2024
626af1c
fixed merge conflict, I hope
SuperMarcomen Jul 16, 2024
1653c29
linter
SuperMarcomen Jul 16, 2024
f2a3c0b
linter 2.0 and error codes
SuperMarcomen Jul 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import Ellipsis from 'lucide-svelte/icons/ellipsis';
import * as DropdownMenu from '$lib/elements/ui/dropdown-menu';
import * as Button from '$lib/elements/ui/button';
import Button from '$lib/elements/ui/button/button.svelte';

export let id: string;
</script>
Expand Down
Lama64 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import { Checkbox } from '$lib/elements/ui/checkbox';
import { cn } from '$lib/utils';
import type { Writable } from 'svelte/store';

export let checked: Writable<boolean>;
</script>

<Checkbox bind:checked={$checked} class={cn("border-white data-[state=checked]:bg-white data-[state=checked]:text-foreground")}/>
49 changes: 25 additions & 24 deletions web/src/lib/elements/ui/dataTable/data-table.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { createTable, Render, Subscribe, createRender } from 'svelte-headless-table';
import { writable } from 'svelte/store';
import * as Table from '$lib/elements/ui/table';
import DataTableActions from './data-table-actions.svelte';
import {
Expand All @@ -11,22 +10,24 @@
addSelectedRows,
} from 'svelte-headless-table/plugins';
import { Button } from '$lib/elements/ui/button';
import ArrowUpDown from 'lucide-svelte/icons/arrow-up-down';
import ChevronDown from 'lucide-svelte/icons/chevron-down';
import * as Input from '$lib/elements/ui/input';
import * as DropdownMenu from '$lib/elements/ui/dropdown-menu';
import DataTableCheckbox from './data-table-checkbox.svelte';
import DataTableHeaderCheckbox from './data-table-header-checkbox.svelte';
import { ArrowDown, ArrowUp } from 'lucide-svelte';
import Input from '$lib/elements/ui/input/input.svelte';

interface DataItem {
id: string;

[key: string]: string | string[] | number;
}

export let data: DataItem[];
export let tableData;
Lama64 marked this conversation as resolved.
Show resolved Hide resolved
export let columnNames;
export let keys;

const table = createTable(writable(data), {
const table = createTable(tableData, {
page: addPagination(),
sort: addSortBy(),
filter: addTableFilter({
Expand All @@ -36,18 +37,16 @@
select: addSelectedRows(),
});

let idKey = Object.keys(data[0])[0];
console.log(idKey);
let columns = table.createColumns([
table.column({
//first column only contains the checkboxes.
accessor: (item) => {
return item[idKey]; //'item' is of type 'unknown' error dont know how to fix
return item[keys[0]];
},
id: 'id',
header: (_, { pluginStates }) => {
const { allPageRowsSelected } = pluginStates.select;
return createRender(DataTableCheckbox, {
return createRender(DataTableHeaderCheckbox, {
checked: allPageRowsSelected,
});
},
Expand All @@ -67,12 +66,11 @@
}),
]);
for (const [i, columnName] of columnNames.entries()) {
let currentKey = Object.keys(data[0])[i + 1];
columns = columns.concat(
table.createColumns([
table.column({
accessor: (item: DataItem) => {
return item[currentKey];
accessor: (item) => {
return item[keys[i + 1]];
},
header: columnName,
}),
Expand All @@ -84,7 +82,7 @@
table.createColumns([
table.column({
accessor: (item) => {
return item[idKey];
return item[keys[0]];
},
header: '',
id: 'actions',
Expand Down Expand Up @@ -114,15 +112,15 @@
.filter(([, hide]) => !hide)
.map(([id]) => id);

const hidableCols = ['status', 'email', 'amount'];
const hidableCols = columnNames;
</script>

<div>
<div class = "border-foreground">
<div class="flex items-center py-4">
<Input bind:value={$filterValue} class="max-w-sm" placeholder="Suche..." type="text" />
<Input bind:value={$filterValue} class="max-w-sm rounded-none border-0 border-b-4 border-foreground focus-visible:ring-0 focus-visible:border-b-4" placeholder="Suche..." type="text" />
<DropdownMenu.Root>
<DropdownMenu.Trigger asChild let:builder>
<Button builders={[builder]} class="ml-auto" variant="outline">
<Button builders={[builder]} class="ml-auto" variant="secondary">
Filter
<ChevronDown class="ml-2 h-4 w-4" />
</Button>
Expand All @@ -138,19 +136,22 @@
</DropdownMenu.Content>
</DropdownMenu.Root>
</div>
<div class="rounded-md border">
<div>
<Table.Root {...$tableAttrs}>
<Table.Header>
<Table.Header class="bg-foreground">
{#each $headerRows as headerRow}
<Subscribe rowAttrs={headerRow.attrs()}>
<Table.Row>
{#each headerRow.cells as cell (cell.id)}
<Subscribe attrs={cell.attrs()} let:attrs props={cell.props()} let:props>
<Table.Head {...attrs} class="[&:has([role=checkbox])]:pl-3">
<Table.Head {...attrs} class="[&:has([role=checkbox])]:pl-4">
{#if cell.id !== 'actions' && cell.id !== 'id'}
<Button variant="ghost" on:click={props.sort.toggle}>
<Button variant="ghost" on:click={props.sort.toggle} class="text-white">
<Render of={cell.render()} />
<ArrowUpDown class={'ml-2 h-4 w-4'} />
<div class="flex ml-2">
<ArrowUp class="h-4 w-4 {props.sort.order === 'desc' ? 'text-accent' : ''}" />
<ArrowDown class="ml-[-4px] h-4 w-4 {props.sort.order === 'asc' ? 'text-accent' : ''}" />
</div>
</Button>
{:else}
<Render of={cell.render()} />
Expand Down Expand Up @@ -184,10 +185,10 @@
{Object.keys($selectedDataIds).length} von{' '}
{$rows.length} Zeile(n) ausgewählt.
</div>
<Button disabled={!$hasPreviousPage} on:click={() => ($pageIndex = $pageIndex - 1)} size="sm" variant="outline"
<Button disabled={!$hasPreviousPage} on:click={() => ($pageIndex = $pageIndex - 1)} size="sm" variant="secondary"
>Zurück
</Button>
<Button disabled={!$hasNextPage} on:click={() => ($pageIndex = $pageIndex + 1)} size="sm" variant="outline"
<Button disabled={!$hasNextPage} on:click={() => ($pageIndex = $pageIndex + 1)} size="sm" variant="secondary"
>Weiter
</Button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/elements/ui/table/table-body.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
export { className as class };
</script>

<tbody class={cn('[&_tr:last-child]:border-0', className)} {...$$restProps}>
<tbody class={cn('[&_tr:last-child]:border-0 bg-white', className)} {...$$restProps}>
<slot />
</tbody>
2 changes: 1 addition & 1 deletion web/src/lib/elements/ui/table/table-row.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</script>

<tr
class={cn('border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted', className)}
class={cn('border-b transition-colors data-[state=selected]:bg-muted', className)}
{...$$restProps}
on:click
on:keydown
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/elements/ui/table/table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export { className as class };
</script>

<div class="relative w-full overflow-auto">
<div class="relative w-full overflow-auto rounded-md border border-foreground">
<table class={cn('w-full caption-bottom text-sm', className)} {...$$restProps}>
<slot />
</table>
Expand Down
6 changes: 4 additions & 2 deletions web/src/routes/admin/(resources)/rooms/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
import DataTable from '$lib/elements/ui/dataTable/data-table.svelte';
import { writable } from 'svelte/store';
let columnNames = ['Name', 'Gebäude', 'Kapazität', 'test'];
let keys = ['id', 'amount', 'status', 'email'];

//demo data from shadcn
const data = [
Expand All @@ -20,6 +22,6 @@
</script>

<!--not yet formatted-->
<div class="container mx-auto py-10 bg-white w-full mt-5">
<DataTable {data} {columnNames} />
<div class="mx-auto p-10 w-full">
<DataTable tableData={writable(data)} {columnNames} {keys}/>
</div>
22 changes: 22 additions & 0 deletions web/src/routes/admin/(resources)/students/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,0 +1,22 @@
<script lang="ts">
import { Button } from '$lib/elements/ui/button';
import DataTable from '$lib/elements/ui/dataTable/data-table.svelte';
import { createStudent, type Student, type StudentRequestDto } from '$lib/sdk/fetch-client';
import { writable, type Writable } from 'svelte/store';
let columnNames = ['Vorname', 'Nachname', 'Tags'];
let keys = ['id', 'firstName', 'lastName', 'tags'];
export let data;
let tableData: Writable<Student[] | undefined> = writable([]);

$: $tableData = data.students;
const create = async () => {

Check failure on line 12 in web/src/routes/admin/(resources)/students/+page.svelte

View workflow job for this annotation

GitHub Actions / Web Test

Async arrow function 'create' has no 'await' expression
let student: StudentRequestDto = { firstName: 'Max', lastName: 'Mustermann', tagIds: [] };
createStudent(student);

Check failure on line 14 in web/src/routes/admin/(resources)/students/+page.svelte

View workflow job for this annotation

GitHub Actions / Web Test

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
};
</script>

<div class="p-10 bg-white w-full">
<DataTable {tableData} {columnNames} {keys} />
</div>

<Button on:click={create}>Add</Button>
7 changes: 6 additions & 1 deletion web/src/routes/admin/(resources)/students/+page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { getStudents, type Pageable, type PageStudent } from '$lib/sdk/fetch-client';
import type { PageLoad } from './$types';

export const load = (() => {
export const load = (async () => {
const pageable: Pageable = { page: 0, size: 10 };
const result: PageStudent = await getStudents(pageable);
const students = result.content;
return {
students,
meta: {
title: 'Students',
},
Expand Down
Loading