Fix labelling when sorting by calculation columns in tables

This commit is contained in:
Andrew Kingston 2024-10-18 16:52:40 +01:00
parent 2cd86f5667
commit c6aa83d988
No known key found for this signature in database
2 changed files with 23 additions and 11 deletions

View File

@ -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 {

View File

@ -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,
})