Add ability to customise format of grid columns via bindings

This commit is contained in:
Andrew Kingston 2025-01-24 14:50:27 +00:00
parent 5ae2ab7c2c
commit e7378ede97
No known key found for this signature in database
6 changed files with 28 additions and 3 deletions

View File

@ -69,6 +69,7 @@ const toGridFormat = draggableListColumns => {
active: entry.active, active: entry.active,
width: entry.width, width: entry.width,
conditions: entry.conditions, conditions: entry.conditions,
format: entry.format,
})) }))
} }
@ -85,6 +86,7 @@ const toDraggableListFormat = (gridFormatColumns, createComponent, schema) => {
columnType: column.columnType || schema[column.field].type, columnType: column.columnType || schema[column.field].type,
width: column.width, width: column.width,
conditions: column.conditions, conditions: column.conditions,
format: column.format,
}, },
{} {}
) )

View File

@ -3089,6 +3089,11 @@
"type": "tableConditions", "type": "tableConditions",
"label": "Conditions", "label": "Conditions",
"key": "conditions" "key": "conditions"
},
{
"type": "text",
"label": "Format",
"key": "format"
} }
] ]
}, },
@ -7685,7 +7690,8 @@
{ {
"type": "columns/grid", "type": "columns/grid",
"key": "columns", "key": "columns",
"resetOn": "table" "resetOn": "table",
"nested": true
} }
] ]
}, },

View File

@ -5,6 +5,7 @@
import { get, derived, readable } from "svelte/store" import { get, derived, readable } from "svelte/store"
import { featuresStore } from "stores" import { featuresStore } from "stores"
import { Grid } from "@budibase/frontend-core" import { Grid } from "@budibase/frontend-core"
import { processStringSync } from "@budibase/string-templates"
// table is actually any datasource, but called table for legacy compatibility // table is actually any datasource, but called table for legacy compatibility
export let table export let table
@ -42,6 +43,7 @@
let gridContext let gridContext
let minHeight = 0 let minHeight = 0
$: id = $component.id
$: currentTheme = $context?.device?.theme $: currentTheme = $context?.device?.theme
$: darkMode = !currentTheme?.includes("light") $: darkMode = !currentTheme?.includes("light")
$: parsedColumns = getParsedColumns(columns) $: parsedColumns = getParsedColumns(columns)
@ -65,7 +67,6 @@
const clean = gridContext?.rows.actions.cleanRow || (x => x) const clean = gridContext?.rows.actions.cleanRow || (x => x)
const cleaned = rows.map(clean) const cleaned = rows.map(clean)
const goldenRow = generateGoldenSample(cleaned) const goldenRow = generateGoldenSample(cleaned)
const id = get(component).id
return { return {
// Not sure what this one is for... // Not sure what this one is for...
[id]: goldenRow, [id]: goldenRow,
@ -104,6 +105,7 @@
order: idx, order: idx,
conditions: column.conditions, conditions: column.conditions,
visible: !!column.active, visible: !!column.active,
format: createFormatter(column),
} }
if (column.width) { if (column.width) {
overrides[column.field].width = column.width overrides[column.field].width = column.width
@ -112,6 +114,16 @@
return overrides return overrides
} }
const createFormatter = column => {
if (!column.format?.length) {
return null
}
return row => {
console.log("processStringSync")
return processStringSync(column.format, { [id]: row })
}
}
const enrichButtons = buttons => { const enrichButtons = buttons => {
if (!buttons?.length) { if (!buttons?.length) {
return null return null

View File

@ -3,6 +3,7 @@
import GridCell from "./GridCell.svelte" import GridCell from "./GridCell.svelte"
import { getCellRenderer } from "../lib/renderers" import { getCellRenderer } from "../lib/renderers"
import { derived, writable } from "svelte/store" import { derived, writable } from "svelte/store"
import { processStringSync } from "@budibase/string-templates"
const { const {
rows, rows,
@ -36,6 +37,8 @@
let api let api
$: value = column.format ? column.format(row) : row[column.name]
// Get the error for this cell if the cell is focused or selected // Get the error for this cell if the cell is focused or selected
$: error = getErrorStore(rowFocused, cellId) $: error = getErrorStore(rowFocused, cellId)
@ -138,7 +141,7 @@
<svelte:component <svelte:component
this={getCellRenderer(column)} this={getCellRenderer(column)}
bind:api bind:api
value={row[column.name]} {value}
schema={column.schema} schema={column.schema}
onChange={cellAPI.setValue} onChange={cellAPI.setValue}
{focused} {focused}

View File

@ -188,6 +188,7 @@ export const initialise = (context: StoreContext) => {
conditions: fieldSchema.conditions, conditions: fieldSchema.conditions,
related: fieldSchema.related, related: fieldSchema.related,
calculationType: fieldSchema.calculationType, calculationType: fieldSchema.calculationType,
format: fieldSchema.format,
__left: undefined as any, // TODO __left: undefined as any, // TODO
__idx: undefined as any, // TODO __idx: undefined as any, // TODO
} }

View File

@ -4,6 +4,7 @@ export type UIColumn = FieldSchema & {
label: string label: string
readonly: boolean readonly: boolean
conditions: any conditions: any
format?: () => any
related?: { related?: {
field: string field: string
subField: string subField: string