diff options
Diffstat (limited to 'fe/src/lib/Table.svelte')
-rw-r--r-- | fe/src/lib/Table.svelte | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/fe/src/lib/Table.svelte b/fe/src/lib/Table.svelte index d1cd7da..621157e 100644 --- a/fe/src/lib/Table.svelte +++ b/fe/src/lib/Table.svelte | |||
@@ -5,6 +5,10 @@ | |||
5 | export let omit: string[] = ["id"]; | 5 | export let omit: string[] = ["id"]; |
6 | export let title: string | undefined = undefined; | 6 | export let title: string | undefined = undefined; |
7 | 7 | ||
8 | export let sortBy: string = 'date'; | ||
9 | |||
10 | type SortComparator = (a, b) => number | ||
11 | |||
8 | function getDataKeys(data: any[]): string[] { | 12 | function getDataKeys(data: any[]): string[] { |
9 | if (!data || data.length === 0) return []; | 13 | if (!data || data.length === 0) return []; |
10 | return Object.keys(data[0]) | 14 | return Object.keys(data[0]) |
@@ -16,11 +20,8 @@ | |||
16 | return Object.entries(row).filter((r) => !omit.includes(r[0])); | 20 | return Object.entries(row).filter((r) => !omit.includes(r[0])); |
17 | } | 21 | } |
18 | 22 | ||
19 | 23 | function sort(arr: Array<Record<string, any>>, fn: SortComparator = (a , b) => new Date(b[sortBy]) - new Date(a[sortBy])) { | |
20 | let limitedData: Array<any> = []; | 24 | return arr.sort(fn) |
21 | |||
22 | if (data && (data as any[]).length > 0) { | ||
23 | limitedData = (data as any[]).slice(0, 4); | ||
24 | } | 25 | } |
25 | 26 | ||
26 | const formatter = new Intl.DateTimeFormat("en", { | 27 | const formatter = new Intl.DateTimeFormat("en", { |
@@ -62,7 +63,7 @@ | |||
62 | {/if} | 63 | {/if} |
63 | <tbody> | 64 | <tbody> |
64 | {#if data} | 65 | {#if data} |
65 | {#each limitedData as row} | 66 | {#each sort(data) as row} |
66 | <tr> | 67 | <tr> |
67 | {#each getRow(row) as datum} | 68 | {#each getRow(row) as datum} |
68 | <td>{formatDatum(datum)}</td> | 69 | <td>{formatDatum(datum)}</td> |