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 $: sortedBy = column.name === $sort.column
$: canMoveLeft = orderable && idx > 0 $: canMoveLeft = orderable && idx > 0
$: canMoveRight = orderable && idx < $scrollableColumns.length - 1 $: canMoveRight = orderable && idx < $scrollableColumns.length - 1
$: sortingLabels = getSortingLabels(column.schema?.type) $: sortingLabels = getSortingLabels(column)
$: searchable = isColumnSearchable(column) $: searchable = isColumnSearchable(column)
$: resetSearchValue(column.name) $: resetSearchValue(column.name)
$: searching = searchValue != null $: searching = searchValue != null
@ -66,8 +66,14 @@
editIsOpen = false editIsOpen = false
} }
const getSortingLabels = type => { const getSortingLabels = column => {
switch (type) { if (column.calculationType) {
return {
ascending: "low-high",
descending: "high-low",
}
}
switch (column?.schema?.type) {
case FieldType.NUMBER: case FieldType.NUMBER:
case FieldType.BIGINT: case FieldType.BIGINT:
return { return {

View File

@ -2,6 +2,7 @@ import { writable, derived, get } from "svelte/store"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { QueryUtils } from "../utils" import { QueryUtils } from "../utils"
import { convertJSONSchemaToTableSchema } from "../utils/json" import { convertJSONSchemaToTableSchema } from "../utils/json"
import { FieldType, SortOrder, SortType } from "@budibase/types"
const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils
@ -37,7 +38,7 @@ export default class DataFetch {
// Sorting config // Sorting config
sortColumn: null, sortColumn: null,
sortOrder: "ascending", sortOrder: SortOrder.ASCENDING,
sortType: null, sortType: null,
// Pagination config // 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 // If we don't have a sort column specified then just ensure we don't set
// any sorting params // any sorting params
if (!this.options.sortColumn) { if (!this.options.sortColumn) {
this.options.sortOrder = "ascending" this.options.sortOrder = SortOrder.ASCENDING
this.options.sortType = null this.options.sortType = null
} else { } else {
// Otherwise determine what sort type to use base on sort column // Otherwise determine what sort type to use base on sort column
const type = schema?.[this.options.sortColumn]?.type this.options.sortType = SortType.STRING
this.options.sortType = const fieldSchema = schema?.[this.options.sortColumn]
type === "number" || type === "bigint" ? "number" : "string" if (
fieldSchema?.type === FieldType.NUMBER ||
fieldSchema?.type === FieldType.BIGINT ||
fieldSchema?.calculationType
) {
this.options.sortType = SortType.NUMBER
}
// If no sort order, default to ascending // If no sort order, default to ascending
if (!this.options.sortOrder) { if (!this.options.sortOrder) {
this.options.sortOrder = "ascending" this.options.sortOrder = SortOrder.ASCENDING
} }
} }
@ -310,7 +316,7 @@ export default class DataFetch {
let jsonAdditions = {} let jsonAdditions = {}
Object.keys(schema).forEach(fieldKey => { Object.keys(schema).forEach(fieldKey => {
const fieldSchema = schema[fieldKey] const fieldSchema = schema[fieldKey]
if (fieldSchema?.type === "json") { if (fieldSchema?.type === FieldType.JSON) {
const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, {
squashObjects: true, squashObjects: true,
}) })