Fix data table and grid rendering views

This commit is contained in:
Andrew Kingston 2020-10-13 19:58:36 +01:00
parent 650ace2f9e
commit 8a10ea899c
3 changed files with 63 additions and 36 deletions

View File

@ -21,6 +21,11 @@
export let height = 500 export let height = 500
export let pagination export let pagination
// These can never change at runtime so don't need to be reactive
let canEdit = editable && datasource && datasource.type !== "view"
let canAddDelete = editable && datasource && datasource.type === "model"
let store = _bb.store
let dataLoaded = false let dataLoaded = false
let data let data
let columnDefs let columnDefs
@ -32,35 +37,50 @@
minWidth: 150, minWidth: 150,
filter: true, filter: true,
}, },
rowSelection: editable ? "multiple" : false, rowSelection: canEdit ? "multiple" : false,
suppressRowClickSelection: !editable, suppressRowClickSelection: !canEdit,
paginationAutoPageSize: true, paginationAutoPageSize: true,
pagination, pagination,
} }
let store = _bb.store
onMount(async () => { onMount(async () => {
if (datasource.tableId) { if (datasource != null) {
const jsonTable = await _bb.api.get(`/api/tables/${datasource.tableId}`) data = await fetchData(datasource, $store)
table = await jsonTable.json() let schemaKeys = []
const { schema } = table let schema = {}
if (!isEmpty(datasource)) {
data = await fetchData(datasource, $store) // Get schema for datasource
columnDefs = Object.keys(schema).map((key, i) => { if (datasource.type === "view") {
return { // For views, use the first object as the schema and pretend it's all strings
headerCheckboxSelection: i === 0 && editable, if (data && data.length) {
checkboxSelection: i === 0 && editable, schemaKeys = Object.keys(data[0])
valueSetter: setters.get(schema[key].type), }
headerName: key.charAt(0).toUpperCase() + key.slice(1), schema = {}
field: key, schemaKeys.forEach(key => {
hide: shouldHideField(key), schema[key] = "string"
sortable: true,
editable: editable,
cellRenderer: getRenderer(schema[key], editable),
autoHeight: true,
}
}) })
} else {
// Fetch the real schema for the table or relationship
const jsonTable = await _bb.api.get(`/api/tables/${datasource.tableId}`)
table = await jsonTable.json()
schema = table.schema
schemaKeys = Object.keys(schema)
} }
columnDefs = schemaKeys.map((key, i) => {
return {
headerCheckboxSelection: i === 0 && canEdit,
checkboxSelection: i === 0 && canEdit,
valueSetter: setters.get(schema[key].type),
headerName: key.charAt(0).toUpperCase() + key.slice(1),
field: key,
hide: shouldHideField(key),
sortable: true,
editable: canEdit,
cellRenderer: getRenderer(schema[key], canEdit),
autoHeight: true,
}
})
dataLoaded = true dataLoaded = true
} }
}) })
@ -117,7 +137,7 @@
<div style="--grid-height: {height}px"> <div style="--grid-height: {height}px">
{#if dataLoaded} {#if dataLoaded}
{#if editable} {#if canAddDelete}
<div class="controls"> <div class="controls">
<CreateRowButton {_bb} {table} on:newRow={handleNewRow} /> <CreateRowButton {_bb} {table} on:newRow={handleNewRow} />
{#if selectedRows.length > 0} {#if selectedRows.length > 0}

View File

@ -35,15 +35,20 @@
const FETCH_TABLE_URL = `/api/tables/${tableId}` const FETCH_TABLE_URL = `/api/tables/${tableId}`
const response = await _bb.api.get(FETCH_TABLE_URL) const response = await _bb.api.get(FETCH_TABLE_URL)
const table = await response.json() const table = await response.json()
schema = table.schema return table.schema
} }
onMount(async () => { onMount(async () => {
if (!isEmpty(datasource)) { if (!isEmpty(datasource)) {
data = await fetchData(datasource, $store) data = await fetchData(datasource, $store)
if (data && data.length) { if (data && data.length) {
await fetchTable(data[0].tableId) if (datasource.type === "view") {
headers = Object.keys(schema).filter(shouldDisplayField) schema = data[0]
headers = Object.keys(schema).filter(shouldDisplayField)
} else {
schema = await fetchTable(datasource.tableId)
headers = Object.keys(schema)
}
} }
} }
}) })
@ -54,7 +59,6 @@
if (name === "type") return false if (name === "type") return false
// tables are always tied to a single tableId, this is irrelevant // tables are always tied to a single tableId, this is irrelevant
if (name === "tableId") return false if (name === "tableId") return false
return true return true
} }
@ -95,11 +99,11 @@
{#each sorted as row (row._id)} {#each sorted as row (row._id)}
<tr> <tr>
{#each headers as header} {#each headers as header}
{#if schema[header]} {#if schema[header] !== undefined}
<!-- Rudimentary solution for attachments on array given this entire table will be replaced by AG Grid --> <!-- Rudimentary solution for attachments on array given this entire table will be replaced by AG Grid -->
{#if schema[header].type === 'attachment'} {#if schema[header] && schema[header].type === 'attachment'}
<AttachmentList files={row[header]} /> <AttachmentList files={row[header]} />
{:else if schema[header].type === 'link'} {:else if schema[header] && schema[header].type === 'link'}
<td>{row[header] ? row[header].length : 0} related row(s)</td> <td>{row[header] ? row[header].length : 0} related row(s)</td>
{:else} {:else}
<td>{row[header] == null ? '' : row[header]}</td> <td>{row[header] == null ? '' : row[header]}</td>

View File

@ -6,16 +6,16 @@ export default async function fetchData(datasource, store) {
if (name) { if (name) {
let rows = [] let rows = []
if (type === "table") { if (type === "table") {
rows = await fetchTableData() rows = fetchTableData()
} else if (type === "view") { } else if (type === "view") {
rows = await fetchViewData() rows = fetchViewData()
} else if (type === "link") { } else if (type === "link") {
rows = await fetchLinkedRowsData() rows = fetchLinkedRowsData()
} }
// Fetch table schema so we can check for linked rows // Fetch table schema so we can check for linked rows
if (rows && rows.length) { if (rows && rows.length) {
const table = await fetchTable(rows[0].tableId) const table = await fetchTable()
const keys = Object.keys(table.schema) const keys = Object.keys(table.schema)
rows.forEach(row => { rows.forEach(row => {
for (let key of keys) { for (let key of keys) {
@ -27,10 +27,12 @@ export default async function fetchData(datasource, store) {
} }
return rows return rows
} else {
return []
} }
async function fetchTable(id) { async function fetchTable() {
const FETCH_TABLE_URL = `/api/tables/${id}` const FETCH_TABLE_URL = `/api/tables/${datasource.tableId}`
const response = await api.get(FETCH_TABLE_URL) const response = await api.get(FETCH_TABLE_URL)
return await response.json() return await response.json()
} }
@ -44,6 +46,7 @@ export default async function fetchData(datasource, store) {
} }
async function fetchViewData() { async function fetchViewData() {
console.log("fetching view")
const { field, groupBy } = datasource const { field, groupBy } = datasource
const params = new URLSearchParams() const params = new URLSearchParams()