From a62c8608c96579130a73e6274604419fe90c7505 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 28 Jul 2020 17:43:29 +0100 Subject: [PATCH] initial table sorting algo --- .../standard-components/src/DataMap.svelte | 31 ----------- .../standard-components/src/DataTable.svelte | 54 +++++++++++++++++-- packages/standard-components/src/index.js | 1 - 3 files changed, 50 insertions(+), 36 deletions(-) delete mode 100644 packages/standard-components/src/DataMap.svelte diff --git a/packages/standard-components/src/DataMap.svelte b/packages/standard-components/src/DataMap.svelte deleted file mode 100644 index 9cbb3c97c2..0000000000 --- a/packages/standard-components/src/DataMap.svelte +++ /dev/null @@ -1,31 +0,0 @@ - - - diff --git a/packages/standard-components/src/DataTable.svelte b/packages/standard-components/src/DataTable.svelte index 0d75b1e4e8..9c9c256bb2 100644 --- a/packages/standard-components/src/DataTable.svelte +++ b/packages/standard-components/src/DataTable.svelte @@ -12,6 +12,8 @@ let headers = [] let store = _bb.store + let sort = {} + let sorted = [] $: cssVariables = { backgroundColor, @@ -20,6 +22,31 @@ borderColor, } + $: data = $store[model] || [] + $: sorted = data + $: { + let result = [...sorted] + + if (!sort.column) sorted = result + + console.log(sort); + + if (sort.direction === "ASC") { + result.sort((a, b) => { + console.log(a[sort.column], b[sort.column]) + return String(a[sort.column]).localeCompare(String(b[sort.column])) + } + ) + } + + if (sort.direction === "DESC") { + result.reverse() + } + + sorted = result + } + $: if (model) fetchData() + const shouldDisplayField = name => { if (name.startsWith("_")) return false // always 'record' @@ -48,8 +75,20 @@ } } - $: data = $store[model] || [] - $: if (model) fetchData() + function sortColumn(column) { + if (column === sort.column) { + sort = { + direction: sort.direction === "ASC" ? "DESC" : null, + column: sort.direction === "ASC" ? sort.column : null, + } + return + } + + sort = { + column, + direction: "ASC", + } + } onMount(async () => { await fetchData() @@ -60,12 +99,15 @@ {#each headers as header} - {header} + sortColumn(header)}> + {header} + {#if sort.column === header}v{/if} + {/each} - {#each data as row} + {#each sorted as row (row._id)} {#each headers as header} {#if row[header]} @@ -120,6 +162,10 @@ display: block; } + th { + cursor: pointer; + } + /* Hide table headers (but not display: none;, for accessibility) */ thead tr { position: absolute; diff --git a/packages/standard-components/src/index.js b/packages/standard-components/src/index.js index a0c6ee025d..067b1e9896 100644 --- a/packages/standard-components/src/index.js +++ b/packages/standard-components/src/index.js @@ -21,6 +21,5 @@ export { default as datachart } from "./DataChart.svelte" export { default as datalist } from "./DataList.svelte" export { default as list } from "./List.svelte" export { default as datasearch } from "./DataSearch.svelte" -export { default as datamap } from "./DataMap.svelte" export { default as embed } from "./Embed.svelte" export { default as recorddetail } from "./RecordDetail.svelte"