-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(components): Column settings support fixed columns. #874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,19 @@ defineOptions({ | |
| const columns = defineModel<NaiveUI.TableColumnCheck[]>('columns', { | ||
| required: true | ||
| }); | ||
|
|
||
| const tooltipRecord: Record<NaiveUI.TableColumnFixed, App.I18n.I18nKey> = { | ||
| left: 'datatable.fixed.right', | ||
| right: 'datatable.fixed.unFixed', | ||
| unFixed: 'datatable.fixed.left' | ||
| }; | ||
|
|
||
| function handleFixed(column: NaiveUI.TableColumnCheck) { | ||
| const fixedOptions: NaiveUI.TableColumnFixed[] = ['left', 'right', 'unFixed']; | ||
| const index = fixedOptions.findIndex(item => item === column.fixed); | ||
| const nextIndex = index === fixedOptions.length - 1 ? 0 : index + 1; | ||
| column.fixed = fixedOptions[nextIndex]; | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
|
|
@@ -25,16 +38,29 @@ const columns = defineModel<NaiveUI.TableColumnCheck[]>('columns', { | |
| <div | ||
| v-for="item in columns" | ||
| :key="item.key" | ||
| class="h-36px flex-y-center rd-4px hover:(bg-primary bg-opacity-20)" | ||
| class="h-36px flex-y-center justify-between gap-6px" | ||
| :class="{ hidden: !item.visible }" | ||
| > | ||
| <icon-mdi-drag class="mr-8px h-full cursor-move text-icon" /> | ||
| <NCheckbox v-model:checked="item.checked" class="none_draggable flex-1"> | ||
| <template v-if="typeof item.title === 'function'"> | ||
| <component :is="item.title" /> | ||
| </template> | ||
| <template v-else>{{ item.title }}</template> | ||
| </NCheckbox> | ||
| <div class="flex-y-center rd-4px hover:(bg-primary bg-opacity-20)"> | ||
| <icon-mdi-drag class="mr-8px h-full cursor-move text-icon" /> | ||
| <NCheckbox v-model:checked="item.checked" class="none_draggable flex-1"> | ||
| <template v-if="typeof item.title === 'function'"> | ||
| <component :is="item.title" /> | ||
| </template> | ||
| <template v-else>{{ item.title }}</template> | ||
| </NCheckbox> | ||
| </div> | ||
| <ButtonIcon | ||
| :disabled="!item.checked" | ||
| text | ||
| :focusable="false" | ||
| :tooltip-content="$t(tooltipRecord[item.fixed!])" | ||
| @click="handleFixed(item)" | ||
| > | ||
| <icon-octicon-pin-16 v-if="item.fixed === 'unFixed'" /> | ||
| <icon-octicon-pin-16 v-else-if="item.fixed === 'left'" class="rotate-270" /> | ||
| <icon-octicon-pin-slash-16 v-else /> | ||
|
Comment on lines
+60
to
+62
|
||
| </ButtonIcon> | ||
| </div> | ||
| </VueDraggable> | ||
| </NPopover> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -266,20 +266,23 @@ function getColumnChecks<Column extends NaiveUI.TableColumn<any>>( | |||||
| key: column.key as string, | ||||||
| title: column.title!, | ||||||
| checked: true, | ||||||
| fixed: column.fixed ?? 'unFixed', | ||||||
| visible: getColumnVisible?.(column) ?? true | ||||||
| }); | ||||||
| } else if (column.type === 'selection') { | ||||||
| checks.push({ | ||||||
| key: SELECTION_KEY, | ||||||
| title: $t('common.check'), | ||||||
| checked: true, | ||||||
| fixed: column.fixed ?? 'unFixed', | ||||||
| visible: getColumnVisible?.(column) ?? false | ||||||
| }); | ||||||
| } else if (column.type === 'expand') { | ||||||
| checks.push({ | ||||||
| key: EXPAND_KEY, | ||||||
| title: $t('common.expandColumn'), | ||||||
| checked: true, | ||||||
| fixed: column.fixed ?? 'unFixed', | ||||||
| visible: getColumnVisible?.(column) ?? false | ||||||
| }); | ||||||
| } | ||||||
|
|
@@ -301,7 +304,14 @@ function getColumns<Column extends NaiveUI.TableColumn<any>>(cols: Column[], che | |||||
| } | ||||||
| }); | ||||||
|
|
||||||
| const filteredColumns = checks.filter(item => item.checked).map(check => columnMap.get(check.key) as Column); | ||||||
| const filteredColumns = checks | ||||||
| .filter(item => item.checked) | ||||||
| .map(check => { | ||||||
| return { | ||||||
| ...columnMap.get(check.key), | ||||||
| fixed: check.fixed | ||||||
|
||||||
| fixed: check.fixed | |
| fixed: check.fixed !== 'unFixed' ? check.fixed : undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for preserving the fixed state has a bug. The current expression
(fixedMap.get(col.key) !== 'unFixed' ? fixedMap.get(col.key) : undefined) ?? col.fixedwill not correctly preserve a user's explicit choice to unfix a column.When a user sets a column to 'unFixed', this logic evaluates to
undefined ?? col.fixed, which falls back to the defaultcol.fixedvalue. If the column was originally fixed (e.g., 'left' or 'right'), it will revert to that fixed state, ignoring the user's 'unFixed' preference.The logic should be:
This way, if a fixed preference exists in the map (including 'unFixed'), it's used; otherwise, fall back to the default.