Add support for displaying linked records in DataTable
This commit is contained in:
parent
49875d5c42
commit
9287b16598
|
@ -13,6 +13,7 @@
|
|||
export let stripeColor
|
||||
export let borderColor
|
||||
export let datasource = {}
|
||||
export let _bb
|
||||
|
||||
let data = []
|
||||
let headers = []
|
||||
|
@ -29,10 +30,18 @@
|
|||
|
||||
$: 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 () => {
|
||||
if (!isEmpty(datasource)) {
|
||||
data = await fetchData(datasource)
|
||||
if (data) {
|
||||
if (data && data.length) {
|
||||
await fetchModel(data[0].modelId)
|
||||
headers = Object.keys(data[0]).filter(shouldDisplayField)
|
||||
}
|
||||
}
|
||||
|
@ -85,12 +94,16 @@
|
|||
{#each sorted as row (row._id)}
|
||||
<tr>
|
||||
{#each headers as header}
|
||||
{#if schema[header]}
|
||||
<!-- Rudimentary solution for attachments on array given this entire table will be replaced by AG Grid -->
|
||||
{#if Array.isArray(row[header])}
|
||||
{#if schema[header].type === 'attachment'}
|
||||
<AttachmentList files={row[header]} />
|
||||
{: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}
|
||||
{/each}
|
||||
</tr>
|
||||
{/each}
|
||||
|
|
Loading…
Reference in New Issue