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

156 lines
3 KiB
Svelte
Raw Normal View History

<script>
2020-05-07 21:53:34 +12:00
import { onMount } from "svelte"
import { cssVars, createClasses } from "./cssVars"
export let _bb
export let onLoad
2020-05-07 07:29:47 +12:00
export let model
export let backgroundColor
export let color
export let stripeColor
export let borderColor
let headers = []
2020-05-08 09:15:09 +12:00
let store = _bb.store
$: cssVariables = {
backgroundColor,
color,
stripeColor,
borderColor,
}
const shouldDisplayField = name => {
if (name.startsWith("_")) return false
// always 'record'
if (name === "type") return false
// tables are always tied to a single modelId, this is irrelevant
if (name === "modelId") return false
return true
}
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(shouldDisplayField)
} 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 use:cssVars={cssVariables}>
<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>
2020-07-02 01:46:22 +12:00
table {
width: 100%;
border-collapse: collapse;
overflow: scroll; /* Scrollbar are always visible */
overflow: auto; /* Scrollbar is displayed as it's needed */
2020-07-02 01:46:22 +12:00
}
/* Zebra striping */
2020-07-02 01:46:22 +12:00
tr:nth-of-type(odd) {
background: var(--stripeColor);
}
th {
background-color: var(--backgroundColor);
color: var(--color);
font-weight: bold;
text-transform: capitalize;
}
td,
th {
padding: 16px;
border: 1px solid var(--borderColor);
text-align: left;
}
@media only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
table {
width: 100%;
}
/* Force table to not be like tables anymore */
table,
thead,
tbody,
th,
td,
tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
tr {
border: 1px solid var(--borderColor);
}
td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 10%;
}
td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
/* Label the data */
content: attr(data-column);
}
}
</style>