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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
<script lang="ts">
export let data: Array<any> | undefined = undefined;
export let nofooter: boolean = false;
export let noheader: boolean = false;
export let omit: string[] = ["id"];
export let title: string | undefined = undefined;
export let sortBy: string = 'date';
type SortComparator = (a:any , b:any) => number
function getDataKeys(data: any[]): string[] {
if (!data || data.length === 0) return [];
return Object.keys(data[0])
.map((k) => k.split("_").join(" "))
.filter((k) => !omit.includes(k));
}
function getRow(row: Record<string, any>): Array<any> {
return Object.entries(row).filter((r) => !omit.includes(r[0]));
}
function sort(arr: Array<Record<string, any>>, fn: SortComparator = (a , b) => Date.parse(b[sortBy]) - Date.parse(a[sortBy])) {
return arr.sort(fn)
}
const formatter = new Intl.DateTimeFormat("en", {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "2-digit",
second: "2-digit",
timeZone: "America/New_York"
});
function formatDatum([key, value]: any[]) {
if (key === "date") {
const parsedDate = new Date(value);
return formatter.format(parsedDate);
}
if (key === "user") {
return value["name"];
}
return value;
}
</script>
<table>
{#if title}
<h2>{title}</h2>
{/if}
{#if !noheader && data}
<thead>
<tr>
{#each getDataKeys(data) as header}
<th>{header}</th>
{/each}
</tr>
</thead>
{/if}
<tbody>
{#if data}
{#each sort(data) as row}
<tr>
{#each getRow(row) as datum}
<td>{formatDatum(datum)}</td>
{/each}
</tr>
{/each}
{:else}
<tr> There is not data.</tr>
{/if}
</tbody>
{#if !nofooter}
<slot name="footer">
<tfoot>
<tr>
<td>Table Footer</td>
</tr>
</tfoot>
</slot>
{/if}
</table>
<style>
table {
padding: 16px;
margin: 8px;
border: solid 1px black;
border-collapse: collapse;
overflow-y: hidden;
width: calc(100% - 16px);
}
thead tr {
background: rgba(0, 0, 23, 0.34);
}
tbody tr:nth-child(odd) {
background: rgba(0, 0, 23, 0.14);
}
th,
td {
text-transform: capitalize;
padding: 1em;
border: 1px solid rgba(0, 0, 0, 1);
}
</style>
|