budibase/packages/standard-components/src/DataTable.svelte

90 lines
1.7 KiB
Svelte
Raw Normal View History

<script>
2020-05-07 11:53:34 +02:00
import { onMount } from "svelte"
export let _bb
export let onLoad
export let _instanceId
2020-05-06 21:29:47 +02:00
export let model
let headers = []
2020-05-07 23:15:09 +02:00
let store = _bb.store
async function fetchData() {
2020-05-07 23:15:09 +02:00
const FETCH_RECORDS_URL = `/api/${_instanceId}/all_${model._id}/records`
2020-05-07 11:53:34 +02:00
const response = await _bb.api.get(FETCH_RECORDS_URL)
if (response.status === 200) {
2020-05-07 11:53:34 +02:00
const json = await response.json()
2020-05-07 23:15:09 +02:00
store.update(state => {
state[model._id] = json
return state
2020-05-18 12:01:09 +02:00
})
2020-05-07 23:15:09 +02:00
headers = Object.keys(json[0]).filter(key => !key.startsWith("_"))
} else {
2020-05-07 11:53:34 +02:00
throw new Error("Failed to fetch records.", response)
}
}
2020-05-07 23:15:09 +02:00
$: data = $store[model._id] || []
onMount(async () => {
2020-05-07 11:53:34 +02:00
await fetchData()
})
</script>
<table class="uk-table">
<thead>
<tr>
{#each headers as header}
<th>{header}</th>
{/each}
</tr>
</thead>
<tbody>
{#each data as row}
<tr>
2020-05-07 11:53:34 +02:00
{#each headers as header}
{#if row[header]}
<td>{row[header]}</td>
{/if}
{/each}
</tr>
{/each}
</tbody>
</table>
<style>
table {
border: 1px solid #ccc;
background: #fff;
border-radius: 3px;
border-collapse: collapse;
}
thead {
background: #f9f9f9;
border: 1px solid #ccc;
}
thead th {
color: var(--button-text);
text-transform: capitalize;
font-weight: 500;
font-size: 14px;
text-rendering: optimizeLegibility;
letter-spacing: 1px;
}
tbody tr {
border-bottom: 1px solid #ccc;
transition: 0.3s background-color;
color: var(--secondary100);
font-size: 14px;
}
tbody tr:hover {
background: #fafafa;
}
</style>