Skip to content

Commit 12e975d

Browse files
authored
Merge pull request #38 from TJJ120635/feat/assets-type-sort
✨Client: Add the function of sorting asset types by their initial letters
2 parents 9712b2b + f603162 commit 12e975d

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

client/src/components/ChartWidget/TableWidget.svelte

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@
3535
}
3636
3737
export let options = []
38+
let typeSortOrder = 'none'
39+
let sortedOptions = []
40+
41+
const getTypeLabel = (item) => (item.alias || item.type || '').trim()
42+
const getSortBucket = (label) => {
43+
if (!label) return 3
44+
const firstChar = label[0]
45+
if (/\d/.test(firstChar) || /[^\p{L}\p{N}]/u.test(firstChar)) return 0
46+
if (/\p{Script=Latin}/u.test(firstChar)) return 1
47+
if (/[\u3400-\u9FFF]/.test(firstChar)) return 2
48+
return 3
49+
}
50+
51+
const baseSortOptions = { numeric: true, sensitivity: 'base' }
52+
const defaultCollator = new Intl.Collator('en', baseSortOptions)
53+
const pinyinCollator = new Intl.Collator('zh-u-co-pinyin', baseSortOptions)
54+
const compareByTypeLabel = (left, right) => {
55+
const leftLabel = getTypeLabel(left)
56+
const rightLabel = getTypeLabel(right)
57+
const bucketDiff = getSortBucket(leftLabel) - getSortBucket(rightLabel)
58+
if (bucketDiff !== 0) return bucketDiff
59+
if (getSortBucket(leftLabel) === 2) return pinyinCollator.compare(leftLabel, rightLabel)
60+
return defaultCollator.compare(leftLabel, rightLabel)
61+
}
62+
63+
$: {
64+
if (typeSortOrder === 'asc') {
65+
sortedOptions = [...options].sort(compareByTypeLabel)
66+
} else if (typeSortOrder === 'desc') {
67+
sortedOptions = [...options].sort((left, right) => compareByTypeLabel(right, left))
68+
} else {
69+
sortedOptions = options
70+
}
71+
}
3872
3973
// 计算转换后的总资产并更新到 store
4074
$: {
@@ -91,6 +125,16 @@
91125
const onResetClick = () => {
92126
dispatch('reset')
93127
}
128+
129+
const onTypeSortToggle = () => {
130+
if (typeSortOrder === 'none') {
131+
typeSortOrder = 'asc'
132+
} else if (typeSortOrder === 'asc') {
133+
typeSortOrder = 'desc'
134+
} else {
135+
typeSortOrder = 'none'
136+
}
137+
}
94138
</script>
95139

96140
<Card
@@ -102,14 +146,26 @@
102146
</div>
103147
<Table hoverable={true} striped={true} class="divide-y last:border-b-0">
104148
<TableHead class="text-sm">
105-
<TableHeadCell>{$_('type')}</TableHeadCell>
149+
<TableHeadCell>
150+
<button
151+
type="button"
152+
class="hover:text-brand inline-flex items-center gap-2 focus:outline-none"
153+
on:click={onTypeSortToggle}>
154+
<span>{$_('type')}</span>
155+
{#if typeSortOrder === 'asc'}
156+
<span class="text-xs">A-Z</span>
157+
{:else if typeSortOrder === 'desc'}
158+
<span class="text-xs">Z-A</span>
159+
{/if}
160+
</button>
161+
</TableHeadCell>
106162
<TableHeadCell>{$_('amount')}</TableHeadCell>
107163
<TableHeadCell>{$_('currency')}</TableHeadCell>
108164
<TableBodyCell><span class="px-4 py-2">{$_('action')}</span></TableBodyCell>
109165
<TableBodyCell><span class="px-4 py-2">{$_('action')}</span></TableBodyCell>
110166
</TableHead>
111167
<TableBody tableBodyClass="py-4">
112-
{#each options as item (item.type)}
168+
{#each sortedOptions as item (item.type)}
113169
<TableBodyRow>
114170
<TableBodyCell>{item.alias || item.type}</TableBodyCell>
115171
<TableBodyCell>

0 commit comments

Comments
 (0)