1
0
Fork 0
mirror of synced 2024-08-08 06:37:55 +12:00
budibase/packages/standard-components/src/DataTable.svelte

97 lines
1.8 KiB
Svelte
Raw Normal View History

<script>
2020-05-07 21:53:34 +12:00
import { onMount } from "svelte"
export let _bb
export let onLoad
2020-05-07 07:29:47 +12:00
export let model
let headers = []
2020-05-08 09:15:09 +12:00
let store = _bb.store
async function fetchData() {
2020-06-19 03:59:31 +12:00
const FETCH_RECORDS_URL = `/api/views/all_${model}`
2020-05-07 21:53:34 +12:00
const response = await _bb.api.get(FETCH_RECORDS_URL)
if (response.status === 200) {
2020-05-07 21:53:34 +12:00
const json = await response.json()
2020-05-08 09:15:09 +12:00
store.update(state => {
state[model] = json
2020-05-08 09:15:09 +12:00
return state
2020-05-18 22:01:09 +12:00
})
2020-05-08 09:15:09 +12:00
headers = Object.keys(json[0]).filter(key => !key.startsWith("_"))
} else {
2020-05-07 21:53:34 +12:00
throw new Error("Failed to fetch records.", response)
}
}
$: data = $store[model] || []
$: if (model) fetchData()
2020-05-08 09:15:09 +12:00
onMount(async () => {
2020-05-07 21:53:34 +12: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 21:53:34 +12: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 {
2020-06-09 22:49:06 +12:00
background: #393c44;
border: 1px solid #ccc;
height: 40px;
text-align: left;
margin-right: 60px;
}
thead th {
color: #ffffff;
text-transform: capitalize;
font-weight: 500;
font-size: 14px;
text-rendering: optimizeLegibility;
justify-content: left;
padding: 16px 20px 16px 8px;
margin-right: 20px;
}
tbody tr {
border-bottom: 1px solid #ccc;
transition: 0.3s background-color;
2020-06-09 22:49:06 +12:00
color: #393c44;
font-size: 14px;
height: 40px;
}
tbody tr:hover {
background: var(--grey-1);
}
</style>