Skip to content

Commit c800a95

Browse files
authored
Implement persistent table sorting by integrating with view options. (#10665)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
1 parent 7add62a commit c800a95

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

plugins/view-resources/src/components/RelationshipTable.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@
8585
$: associations = buildConfigAssociation(config)
8686
8787
let _sortKey = prefferedSorting
88+
let sortOrder = SortingOrder.Descending
8889
let userSorting = false
89-
$: if (!userSorting) {
90+
$: if (!userSorting && !viewOptions?.orderBy) {
9091
_sortKey = prefferedSorting
9192
}
9293
93-
let sortOrder = SortingOrder.Descending
94+
$: if (viewOptions?.orderBy) {
95+
_sortKey = viewOptions.orderBy[0]
96+
sortOrder = viewOptions.orderBy[1]
97+
}
98+
9499
let loading = 0
95100
96101
let objects: Doc[] = []
@@ -204,6 +209,7 @@
204209
} else {
205210
sortOrder = sortOrder === SortingOrder.Ascending ? SortingOrder.Descending : SortingOrder.Ascending
206211
}
212+
dispatch('sort', { key: _sortKey, order: sortOrder })
207213
}
208214
209215
const joinProps = (attribute: AttributeModel, object: Doc, readonly: boolean) => {

plugins/view-resources/src/components/RelationshipTableBrowser.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
// limitations under the License.
1414
-->
1515
<script lang="ts">
16-
import { Class, Doc, DocumentQuery, FindOptions, Ref, generateId } from '@hcengineering/core'
16+
import { Class, Doc, DocumentQuery, FindOptions, Ref, generateId, SortingOrder } from '@hcengineering/core'
1717
import { ActionContext } from '@hcengineering/presentation'
1818
import { Scroller, tableSP } from '@hcengineering/ui'
1919
import { BuildModelKey, Viewlet, ViewOptionModel, ViewOptions } from '@hcengineering/view'
2020
import { onDestroy, onMount } from 'svelte'
2121
import { type ViewletContext, ViewletContextStore, viewletContextStore } from '../viewletContextStore'
2222
import RelationshipTable from './RelationshipTable.svelte'
23+
import { setViewOptions } from '../viewOptions'
2324
2425
export let _class: Ref<Class<Doc>>
2526
export let query: DocumentQuery<Doc>
@@ -33,6 +34,13 @@
3334
export let readonly = false
3435
3536
const contextId = generateId()
37+
async function onSort (event: CustomEvent<{ key: string, order: SortingOrder }>) {
38+
const { key, order } = event.detail
39+
if (viewlet && viewOptions) {
40+
viewOptions.orderBy = [key, order]
41+
setViewOptions(viewlet, viewOptions)
42+
}
43+
}
3644
3745
// Set viewlet context in store when component mounts/updates
3846
$: {
@@ -92,5 +100,6 @@
92100
{viewOptions}
93101
viewOptionsConfig={viewOptionsConfig ?? viewlet?.viewOptions?.other}
94102
{readonly}
103+
on:sort={onSort}
95104
/>
96105
</Scroller>

plugins/view-resources/src/components/Table.svelte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
const client = getClient()
8787
const hierarchy = client.getHierarchy()
8888
89+
let sortOrder = SortingOrder.Descending
90+
8991
let lookup = buildConfigLookup(hierarchy, _class, config, options?.lookup)
9092
let associations = buildConfigAssociation(config)
9193
@@ -94,11 +96,15 @@
9496
9597
let _sortKey = prefferedSorting
9698
let userSorting = false
97-
$: if (!userSorting) {
99+
$: if (!userSorting && !viewOptions?.orderBy) {
98100
_sortKey = prefferedSorting
99101
}
100102
101-
let sortOrder = SortingOrder.Descending
103+
$: if (viewOptions?.orderBy) {
104+
_sortKey = viewOptions.orderBy[0]
105+
sortOrder = viewOptions.orderBy[1]
106+
}
107+
102108
let loading = 0
103109
104110
let objects: Doc[] = []
@@ -225,6 +231,7 @@
225231
} else {
226232
sortOrder = sortOrder === SortingOrder.Ascending ? SortingOrder.Descending : SortingOrder.Ascending
227233
}
234+
dispatch('sort', { key: _sortKey, order: sortOrder })
228235
}
229236
230237
$: checkedSet = new Set<Ref<Doc>>(checked.map((it) => it._id))

plugins/view-resources/src/components/TableBrowser.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
-->
1515
<script lang="ts">
1616
import type { Class, Doc, DocumentQuery, FindOptions, Ref } from '@hcengineering/core'
17-
import { generateId } from '@hcengineering/core'
17+
import { generateId, SortingOrder } from '@hcengineering/core'
1818
import { ActionContext } from '@hcengineering/presentation'
1919
import { FadeOptions, Scroller, tableSP } from '@hcengineering/ui'
2020
import { BuildModelKey, ViewOptionModel, ViewOptions, Viewlet } from '@hcengineering/view'
@@ -23,6 +23,7 @@
2323
import { LoadingProps } from '../utils'
2424
import { type ViewletContext, ViewletContextStore, viewletContextStore } from '../viewletContextStore'
2525
import Table from './Table.svelte'
26+
import { setViewOptions } from '../viewOptions'
2627
2728
export let _class: Ref<Class<Doc>>
2829
export let query: DocumentQuery<Doc>
@@ -56,6 +57,13 @@
5657
const selection = listProvider.selection
5758
5859
const contextId = generateId()
60+
async function onSort (event: CustomEvent<{ key: string, order: SortingOrder }>) {
61+
const { key, order } = event.detail
62+
if (viewlet && viewOptions) {
63+
viewOptions.orderBy = [key, order]
64+
setViewOptions(viewlet, viewOptions)
65+
}
66+
}
5967
6068
// Set viewlet context in store when component mounts/updates
6169
$: {
@@ -129,6 +137,7 @@
129137
on:content={(evt) => {
130138
listProvider.update(evt.detail)
131139
}}
140+
on:sort={onSort}
132141
on:check={(evt) => {
133142
listProvider.updateSelection(evt.detail.docs, evt.detail.value)
134143
}}

0 commit comments

Comments
 (0)