From c6aa83d988b860e54250237130dac9cd1276e696 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 18 Oct 2024 16:52:40 +0100 Subject: [PATCH] Fix labelling when sorting by calculation columns in tables --- .../components/grid/cells/HeaderCell.svelte | 12 +++++++--- packages/frontend-core/src/fetch/DataFetch.js | 22 ++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte index 6c1c025fcd..bae7bbf40b 100644 --- a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte @@ -52,7 +52,7 @@ $: sortedBy = column.name === $sort.column $: canMoveLeft = orderable && idx > 0 $: canMoveRight = orderable && idx < $scrollableColumns.length - 1 - $: sortingLabels = getSortingLabels(column.schema?.type) + $: sortingLabels = getSortingLabels(column) $: searchable = isColumnSearchable(column) $: resetSearchValue(column.name) $: searching = searchValue != null @@ -66,8 +66,14 @@ editIsOpen = false } - const getSortingLabels = type => { - switch (type) { + const getSortingLabels = column => { + if (column.calculationType) { + return { + ascending: "low-high", + descending: "high-low", + } + } + switch (column?.schema?.type) { case FieldType.NUMBER: case FieldType.BIGINT: return { diff --git a/packages/frontend-core/src/fetch/DataFetch.js b/packages/frontend-core/src/fetch/DataFetch.js index a056cdff5d..fb1dbd5885 100644 --- a/packages/frontend-core/src/fetch/DataFetch.js +++ b/packages/frontend-core/src/fetch/DataFetch.js @@ -2,6 +2,7 @@ import { writable, derived, get } from "svelte/store" import { cloneDeep } from "lodash/fp" import { QueryUtils } from "../utils" import { convertJSONSchemaToTableSchema } from "../utils/json" +import { FieldType, SortOrder, SortType } from "@budibase/types" const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils @@ -37,7 +38,7 @@ export default class DataFetch { // Sorting config sortColumn: null, - sortOrder: "ascending", + sortOrder: SortOrder.ASCENDING, sortType: null, // Pagination config @@ -162,17 +163,22 @@ export default class DataFetch { // If we don't have a sort column specified then just ensure we don't set // any sorting params if (!this.options.sortColumn) { - this.options.sortOrder = "ascending" + this.options.sortOrder = SortOrder.ASCENDING this.options.sortType = null } else { // Otherwise determine what sort type to use base on sort column - const type = schema?.[this.options.sortColumn]?.type - this.options.sortType = - type === "number" || type === "bigint" ? "number" : "string" - + this.options.sortType = SortType.STRING + const fieldSchema = schema?.[this.options.sortColumn] + if ( + fieldSchema?.type === FieldType.NUMBER || + fieldSchema?.type === FieldType.BIGINT || + fieldSchema?.calculationType + ) { + this.options.sortType = SortType.NUMBER + } // If no sort order, default to ascending if (!this.options.sortOrder) { - this.options.sortOrder = "ascending" + this.options.sortOrder = SortOrder.ASCENDING } } @@ -310,7 +316,7 @@ export default class DataFetch { let jsonAdditions = {} Object.keys(schema).forEach(fieldKey => { const fieldSchema = schema[fieldKey] - if (fieldSchema?.type === "json") { + if (fieldSchema?.type === FieldType.JSON) { const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { squashObjects: true, })