Skip to content

Commit 021d1d3

Browse files
committed
feat(components): Column settings support fixed columns.
1 parent 232e1ac commit 021d1d3

File tree

7 files changed

+67
-12
lines changed

7 files changed

+67
-12
lines changed

packages/hooks/src/use-table.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type TableColumnCheck = {
2323
title: TableColumnCheckTitle;
2424
checked: boolean;
2525
visible: boolean;
26+
fixed: 'left' | 'right' | 'unFixed';
2627
};
2728

2829
export interface UseTableOptions<ResponseData, ApiData, Column, Pagination extends boolean> {
@@ -78,12 +79,14 @@ export default function useTable<ResponseData, ApiData, Column, Pagination exten
7879

7980
function reloadColumns() {
8081
const checkMap = new Map(columnChecks.value.map(col => [col.key, col.checked]));
82+
const fixedMap = new Map(columnChecks.value.map(col => [col.key, col.fixed]));
8183

8284
const defaultChecks = getColumnChecks(columns());
8385

8486
columnChecks.value = defaultChecks.map(col => ({
8587
...col,
86-
checked: checkMap.get(col.key) ?? col.checked
88+
checked: checkMap.get(col.key) ?? col.checked,
89+
fixed: (fixedMap.get(col.key) !== 'unFixed' ? fixedMap.get(col.key) : undefined) ?? col.fixed
8790
}));
8891
}
8992

src/components/advanced/table-column-setting.vue

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ defineOptions({
99
const columns = defineModel<NaiveUI.TableColumnCheck[]>('columns', {
1010
required: true
1111
});
12+
13+
const tooltipRecord: Record<NaiveUI.TableColumnFixed, App.I18n.I18nKey> = {
14+
left: 'datatable.fixed.right',
15+
right: 'datatable.fixed.unFixed',
16+
unFixed: 'datatable.fixed.left'
17+
};
18+
19+
function handleFixed(column: NaiveUI.TableColumnCheck) {
20+
const fixedOptions: NaiveUI.TableColumnFixed[] = ['left', 'right', 'unFixed'];
21+
const index = fixedOptions.findIndex(item => item === column.fixed);
22+
const nextIndex = index === fixedOptions.length - 1 ? 0 : index + 1;
23+
column.fixed = fixedOptions[nextIndex];
24+
}
1225
</script>
1326

1427
<template>
@@ -25,16 +38,29 @@ const columns = defineModel<NaiveUI.TableColumnCheck[]>('columns', {
2538
<div
2639
v-for="item in columns"
2740
:key="item.key"
28-
class="h-36px flex-y-center rd-4px hover:(bg-primary bg-opacity-20)"
41+
class="h-36px flex-y-center justify-between gap-6px"
2942
:class="{ hidden: !item.visible }"
3043
>
31-
<icon-mdi-drag class="mr-8px h-full cursor-move text-icon" />
32-
<NCheckbox v-model:checked="item.checked" class="none_draggable flex-1">
33-
<template v-if="typeof item.title === 'function'">
34-
<component :is="item.title" />
35-
</template>
36-
<template v-else>{{ item.title }}</template>
37-
</NCheckbox>
44+
<div class="flex-y-center rd-4px hover:(bg-primary bg-opacity-20)">
45+
<icon-mdi-drag class="mr-8px h-full cursor-move text-icon" />
46+
<NCheckbox v-model:checked="item.checked" class="none_draggable flex-1">
47+
<template v-if="typeof item.title === 'function'">
48+
<component :is="item.title" />
49+
</template>
50+
<template v-else>{{ item.title }}</template>
51+
</NCheckbox>
52+
</div>
53+
<ButtonIcon
54+
:disabled="!item.checked"
55+
text
56+
:focusable="false"
57+
:tooltip-content="$t(tooltipRecord[item.fixed!])"
58+
@click="handleFixed(item)"
59+
>
60+
<icon-octicon-pin-16 v-if="item.fixed === 'unFixed'" />
61+
<icon-octicon-pin-16 v-else-if="item.fixed === 'left'" class="rotate-270" />
62+
<icon-octicon-pin-slash-16 v-else />
63+
</ButtonIcon>
3864
</div>
3965
</VueDraggable>
4066
</NPopover>

src/hooks/common/table.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,23 @@ function getColumnChecks<Column extends NaiveUI.TableColumn<any>>(
266266
key: column.key as string,
267267
title: column.title!,
268268
checked: true,
269+
fixed: column.fixed ?? 'unFixed',
269270
visible: getColumnVisible?.(column) ?? true
270271
});
271272
} else if (column.type === 'selection') {
272273
checks.push({
273274
key: SELECTION_KEY,
274275
title: $t('common.check'),
275276
checked: true,
277+
fixed: column.fixed ?? 'unFixed',
276278
visible: getColumnVisible?.(column) ?? false
277279
});
278280
} else if (column.type === 'expand') {
279281
checks.push({
280282
key: EXPAND_KEY,
281283
title: $t('common.expandColumn'),
282284
checked: true,
285+
fixed: column.fixed ?? 'unFixed',
283286
visible: getColumnVisible?.(column) ?? false
284287
});
285288
}
@@ -301,7 +304,14 @@ function getColumns<Column extends NaiveUI.TableColumn<any>>(cols: Column[], che
301304
}
302305
});
303306

304-
const filteredColumns = checks.filter(item => item.checked).map(check => columnMap.get(check.key) as Column);
307+
const filteredColumns = checks
308+
.filter(item => item.checked)
309+
.map(check => {
310+
return {
311+
...columnMap.get(check.key),
312+
fixed: check.fixed
313+
} as Column;
314+
});
305315

306316
return filteredColumns;
307317
}

src/locales/langs/en-us.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,12 @@ const local: App.I18n.Schema = {
356356
unpin: 'Unpin'
357357
},
358358
datatable: {
359-
itemCount: 'Total {total} items'
359+
itemCount: 'Total {total} items',
360+
fixed: {
361+
left: 'Left Fixed',
362+
right: 'Right Fixed',
363+
unFixed: 'Unfixed'
364+
}
360365
}
361366
};
362367

src/locales/langs/zh-cn.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,12 @@ const local: App.I18n.Schema = {
352352
unpin: '取消固定'
353353
},
354354
datatable: {
355-
itemCount: '共 {total} 条'
355+
itemCount: '共 {total} 条',
356+
fixed: {
357+
left: '左固定',
358+
right: '右固定',
359+
unFixed: '取消固定'
360+
}
356361
}
357362
};
358363

src/typings/app.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ declare namespace App {
578578
};
579579
datatable: {
580580
itemCount: string;
581+
fixed: {
582+
left: string;
583+
right: string;
584+
unFixed: string;
585+
};
581586
};
582587
};
583588

src/typings/naive-ui.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare namespace NaiveUI {
77
type DataTableSelectionColumn<T> = import('naive-ui').DataTableSelectionColumn<T>;
88
type TableColumnGroup<T> = import('naive-ui/es/data-table/src/interface').TableColumnGroup<T>;
99
type TableColumnCheck = import('@sa/hooks').TableColumnCheck;
10+
type TableColumnFixed = import('@sa/hooks').TableColumnCheck['fixed'];
1011

1112
type SetTableColumnKey<C, T> = Omit<C, 'key'> & { key: keyof T | (string & {}) };
1213

0 commit comments

Comments
 (0)