From 59174c4c1af25c4017a0fa6a4e767b37d005ddb7 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 14 Oct 2020 09:23:20 +0100 Subject: [PATCH] Improve grid and table rendering of any type of datasource --- .../src/DataGrid/Component.svelte | 18 +++++------------- .../standard-components/src/DataTable.svelte | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/standard-components/src/DataGrid/Component.svelte b/packages/standard-components/src/DataGrid/Component.svelte index c6522f56b4..6ec91558e5 100644 --- a/packages/standard-components/src/DataGrid/Component.svelte +++ b/packages/standard-components/src/DataGrid/Component.svelte @@ -46,28 +46,20 @@ onMount(async () => { if (datasource != null) { data = await fetchData(datasource, $store) - let schemaKeys = [] let schema = {} // Get schema for datasource - if (datasource.type === "view") { - // For views, use the first object as the schema and pretend it's all strings - if (data && data.length) { - schemaKeys = Object.keys(data[0]) - } - schema = {} - schemaKeys.forEach(key => { - schema[key] = "string" - }) + // Views with "Calculate" applied provide their own schema. + // For everything else, use the tableId property to pull to table schema + if (datasource.schema) { + schema = datasource.schema } 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) => { + columnDefs = Object.keys(schema).map((key, i) => { return { headerCheckboxSelection: i === 0 && canEdit, checkboxSelection: i === 0 && canEdit, diff --git a/packages/standard-components/src/DataTable.svelte b/packages/standard-components/src/DataTable.svelte index 3344de1258..80f2b306a2 100644 --- a/packages/standard-components/src/DataTable.svelte +++ b/packages/standard-components/src/DataTable.svelte @@ -40,15 +40,18 @@ onMount(async () => { if (!isEmpty(datasource)) { + console.log(datasource) data = await fetchData(datasource, $store) - if (data && data.length) { - if (datasource.type === "view") { - schema = data[0] - headers = Object.keys(schema).filter(shouldDisplayField) - } else { - schema = await fetchTable(datasource.tableId) - headers = Object.keys(schema) - } + + // Get schema for datasource + // Views with "Calculate" applied provide their own schema. + // For everything else, use the tableId property to pull to table schema + if (datasource.schema) { + schema = datasource.schema + headers = Object.keys(schema).filter(shouldDisplayField) + } else { + schema = await fetchTable(datasource.tableId) + headers = Object.keys(schema).filter(shouldDisplayField) } } })