From a1543e0b4f25f03f1536eba2256c8be34df911fd Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 15 Oct 2020 11:08:56 +0100 Subject: [PATCH] Add support for rendering real relationships inside grid --- .../Relationship/RelationshipDisplay.svelte | 58 +++++++++++++++++++ .../src/DataGrid/Relationship/tableCache.js | 21 +++++++ .../src/DataGrid/customRenderer.js | 11 +++- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 packages/standard-components/src/DataGrid/Relationship/RelationshipDisplay.svelte create mode 100644 packages/standard-components/src/DataGrid/Relationship/tableCache.js diff --git a/packages/standard-components/src/DataGrid/Relationship/RelationshipDisplay.svelte b/packages/standard-components/src/DataGrid/Relationship/RelationshipDisplay.svelte new file mode 100644 index 0000000000..d18d6f3be8 --- /dev/null +++ b/packages/standard-components/src/DataGrid/Relationship/RelationshipDisplay.svelte @@ -0,0 +1,58 @@ + + +
+ {#if linkedRows && linkedRows.length && displayColumn} + {#each linkedRows as linkedRow} +
{linkedRow[displayColumn]}
+ {/each} + {/if} +
+ + diff --git a/packages/standard-components/src/DataGrid/Relationship/tableCache.js b/packages/standard-components/src/DataGrid/Relationship/tableCache.js new file mode 100644 index 0000000000..a87f12969c --- /dev/null +++ b/packages/standard-components/src/DataGrid/Relationship/tableCache.js @@ -0,0 +1,21 @@ +import api from "../../api" + +let cache = {} + +async function fetchTable(id) { + const FETCH_TABLE_URL = `/api/tables/${id}` + const response = await api.get(FETCH_TABLE_URL) + return await response.json() +} + +export default async function getTable(tableId) { + if (!tableId) { + return null + } + if (!cache[tableId]) { + console.log("cache miss for " + tableId) + cache[tableId] = fetchTable(tableId) + cache[tableId] = await cache[tableId] + } + return await cache[tableId] +} diff --git a/packages/standard-components/src/DataGrid/customRenderer.js b/packages/standard-components/src/DataGrid/customRenderer.js index e3f07eda47..367841b681 100644 --- a/packages/standard-components/src/DataGrid/customRenderer.js +++ b/packages/standard-components/src/DataGrid/customRenderer.js @@ -4,6 +4,7 @@ import AttachmentCell from "./AttachmentCell/Button.svelte" import Select from "./Select/Wrapper.svelte" import DatePicker from "./DateTime/Wrapper.svelte" +import RelationshipDisplay from "./Relationship/RelationshipDisplay.svelte" const renderers = new Map([ ["boolean", booleanRenderer], @@ -103,14 +104,20 @@ function optionsRenderer({ inclusion }, editable) { } } /* eslint-disable no-unused-vars */ -function linkedRowRenderer(constraints, editable) { +function linkedRowRenderer(constraints, editable, schema) { return params => { let container = document.createElement("div") container.style.display = "grid" container.style.placeItems = "center" container.style.height = "100%" - container.innerText = params.value ? params.value.length : 0 + new RelationshipDisplay({ + target: container, + props: { + row: params.data, + columnName: params.column.colId, + }, + }) return container }