Add support for displaying linked records in DataTable

This commit is contained in:
Andrew Kingston 2020-10-06 16:05:52 +01:00
parent 49875d5c42
commit 9287b16598
1 changed files with 19 additions and 6 deletions

View File

@ -13,6 +13,7 @@
export let stripeColor export let stripeColor
export let borderColor export let borderColor
export let datasource = {} export let datasource = {}
export let _bb
let data = [] let data = []
let headers = [] let headers = []
@ -29,10 +30,18 @@
$: sorted = sort.direction ? fsort(data)[sort.direction](sort.column) : data $: sorted = sort.direction ? fsort(data)[sort.direction](sort.column) : data
async function fetchModel(modelId) {
const FETCH_MODEL_URL = `/api/models/${modelId}`
const response = await _bb.api.get(FETCH_MODEL_URL)
const model = await response.json()
schema = model.schema
}
onMount(async () => { onMount(async () => {
if (!isEmpty(datasource)) { if (!isEmpty(datasource)) {
data = await fetchData(datasource) data = await fetchData(datasource)
if (data) { if (data && data.length) {
await fetchModel(data[0].modelId)
headers = Object.keys(data[0]).filter(shouldDisplayField) headers = Object.keys(data[0]).filter(shouldDisplayField)
} }
} }
@ -85,11 +94,15 @@
{#each sorted as row (row._id)} {#each sorted as row (row._id)}
<tr> <tr>
{#each headers as header} {#each headers as header}
<!-- Rudimentary solution for attachments on array given this entire table will be replaced by AG Grid --> {#if schema[header]}
{#if Array.isArray(row[header])} <!-- Rudimentary solution for attachments on array given this entire table will be replaced by AG Grid -->
<AttachmentList files={row[header]} /> {#if schema[header].type === 'attachment'}
{:else if row[header]} <AttachmentList files={row[header]} />
<td>{row[header]}</td> {:else if schema[header].type === 'link'}
<td>{row[header] ? row[header].length : 0} related row(s)</td>
{:else if row[header]}
<td>{row[header]}</td>
{/if}
{/if} {/if}
{/each} {/each}
</tr> </tr>