Add support for tables updating server-side sorting

This commit is contained in:
Andrew Kingston 2021-11-03 15:26:15 +00:00
parent d054d496be
commit 512ed5aa2a
3 changed files with 69 additions and 15 deletions

View File

@ -35,28 +35,36 @@
let pageNumber = 0 let pageNumber = 0
let query = null let query = null
// Sorting can be overridden at run time, so we can't use the prop directly
let currentSortColumn = sortColumn
let currentSortOrder = sortOrder
$: query = buildLuceneQuery(filter) $: query = buildLuceneQuery(filter)
$: internalTable = dataSource?.type === "table" $: internalTable = dataSource?.type === "table"
$: nestedProvider = dataSource?.type === "provider" $: nestedProvider = dataSource?.type === "provider"
$: hasNextPage = bookmarks[pageNumber + 1] != null $: hasNextPage = bookmarks[pageNumber + 1] != null
$: hasPrevPage = pageNumber > 0 $: hasPrevPage = pageNumber > 0
$: getSchema(dataSource) $: getSchema(dataSource)
$: sortType = getSortType(schema, sortColumn) $: sortType = getSortType(schema, currentSortColumn)
$: {
// Wait until schema loads before loading data, so that we can determine // Wait until schema loads before loading data, so that we can determine
// the correct sort type first time // the correct sort type first time
$: {
if (schemaLoaded) { if (schemaLoaded) {
fetchData( fetchData(
dataSource, dataSource,
schema,
query, query,
limit, limit,
sortColumn, currentSortColumn,
sortOrder, currentSortOrder,
sortType, sortType,
paginate paginate
) )
} }
} }
// Reactively filter and sort rows if required
$: { $: {
if (internalTable) { if (internalTable) {
// Internal tables are already processed server-side // Internal tables are already processed server-side
@ -65,10 +73,17 @@
// For anything else we use client-side implementations to filter, sort // For anything else we use client-side implementations to filter, sort
// and limit // and limit
const filtered = luceneQuery(allRows, query) const filtered = luceneQuery(allRows, query)
const sorted = luceneSort(filtered, sortColumn, sortOrder, sortType) const sorted = luceneSort(
filtered,
currentSortColumn,
currentSortOrder,
sortType
)
rows = luceneLimit(sorted, limit) rows = luceneLimit(sorted, limit)
} }
} }
// Build our action context
$: actions = [ $: actions = [
{ {
type: ActionTypes.RefreshDatasource, type: ActionTypes.RefreshDatasource,
@ -79,7 +94,20 @@
type: ActionTypes.SetDataProviderQuery, type: ActionTypes.SetDataProviderQuery,
callback: newQuery => (query = newQuery), callback: newQuery => (query = newQuery),
}, },
{
type: ActionTypes.SetDataProviderSorting,
callback: ({ column, order }) => {
if (column) {
currentSortColumn = column
}
if (order) {
currentSortOrder = order
}
},
},
] ]
// Build our data context
$: dataContext = { $: dataContext = {
rows, rows,
schema, schema,
@ -88,7 +116,11 @@
// Undocumented properties. These aren't supposed to be used in builder // Undocumented properties. These aren't supposed to be used in builder
// bindings, but are used internally by other components // bindings, but are used internally by other components
id: $component?.id, id: $component?.id,
state: { query }, state: {
query,
sortColumn: currentSortColumn,
sortOrder: currentSortOrder,
},
loaded, loaded,
} }
@ -104,10 +136,11 @@
if (schemaLoaded && !nestedProvider) { if (schemaLoaded && !nestedProvider) {
fetchData( fetchData(
dataSource, dataSource,
schema,
query, query,
limit, limit,
sortColumn, currentSortColumn,
sortOrder, currentSortOrder,
sortType, sortType,
paginate paginate
) )
@ -116,6 +149,7 @@
const fetchData = async ( const fetchData = async (
dataSource, dataSource,
schema,
query, query,
limit, limit,
sortColumn, sortColumn,
@ -125,12 +159,16 @@
) => { ) => {
loading = true loading = true
if (dataSource?.type === "table") { if (dataSource?.type === "table") {
// Sanity check sort column, as using a non-existant column will prevent
// results coming back at all
const sort = schema?.[sortColumn] ? sortColumn : undefined
// For internal tables we use server-side processing // For internal tables we use server-side processing
const res = await API.searchTable({ const res = await API.searchTable({
tableId: dataSource.tableId, tableId: dataSource.tableId,
query, query,
limit, limit,
sort: sortColumn, sort,
sortOrder: sortOrder?.toLowerCase() ?? "ascending", sortOrder: sortOrder?.toLowerCase() ?? "ascending",
sortType, sortType,
paginate, paginate,
@ -181,13 +219,14 @@
if (!hasNextPage || !internalTable) { if (!hasNextPage || !internalTable) {
return return
} }
const sort = schema?.[currentSortColumn] ? currentSortColumn : undefined
const res = await API.searchTable({ const res = await API.searchTable({
tableId: dataSource?.tableId, tableId: dataSource?.tableId,
query, query,
bookmark: bookmarks[pageNumber + 1], bookmark: bookmarks[pageNumber + 1],
limit, limit,
sort: sortColumn, sort,
sortOrder: sortOrder?.toLowerCase() ?? "ascending", sortOrder: currentSortOrder?.toLowerCase() ?? "ascending",
sortType, sortType,
paginate: true, paginate: true,
}) })
@ -202,13 +241,14 @@
if (!hasPrevPage || !internalTable) { if (!hasPrevPage || !internalTable) {
return return
} }
const sort = schema?.[currentSortColumn] ? currentSortColumn : undefined
const res = await API.searchTable({ const res = await API.searchTable({
tableId: dataSource?.tableId, tableId: dataSource?.tableId,
query, query,
bookmark: bookmarks[pageNumber - 1], bookmark: bookmarks[pageNumber - 1],
limit, limit,
sort: sortColumn, sort,
sortOrder: sortOrder?.toLowerCase() ?? "ascending", sortOrder: currentSortOrder?.toLowerCase() ?? "ascending",
sortType, sortType,
paginate: true, paginate: true,
}) })

View File

@ -11,7 +11,11 @@
export let size export let size
const component = getContext("component") const component = getContext("component")
const { styleable } = getContext("sdk") const { styleable, getAction, ActionTypes } = getContext("sdk")
const setSorting = getAction(
dataProvider?.id,
ActionTypes.SetDataProviderSorting
)
const customColumnKey = `custom-${Math.random()}` const customColumnKey = `custom-${Math.random()}`
const customRenderers = [ const customRenderers = [
{ {
@ -70,6 +74,13 @@
}) })
return newSchema return newSchema
} }
const onSort = e => {
setSorting({
column: e.detail.column,
order: e.detail.order,
})
}
</script> </script>
<div use:styleable={$component.styles} class={size}> <div use:styleable={$component.styles} class={size}>
@ -84,6 +95,8 @@
allowEditRows={false} allowEditRows={false}
allowEditColumns={false} allowEditColumns={false}
showAutoColumns={true} showAutoColumns={true}
disableSorting
on:sort={onSort}
> >
<slot /> <slot />
</Table> </Table>

View File

@ -6,6 +6,7 @@ export const ActionTypes = {
ValidateForm: "ValidateForm", ValidateForm: "ValidateForm",
RefreshDatasource: "RefreshDatasource", RefreshDatasource: "RefreshDatasource",
SetDataProviderQuery: "SetDataProviderQuery", SetDataProviderQuery: "SetDataProviderQuery",
SetDataProviderSorting: "SetDataProviderSorting",
ClearForm: "ClearForm", ClearForm: "ClearForm",
ChangeFormStep: "ChangeFormStep", ChangeFormStep: "ChangeFormStep",
} }