refactor: 表格列选择器逻辑优化

This commit is contained in:
RubyLiu 2023-07-20 14:23:26 +08:00 committed by fit2-zhao
parent 2a2c2959be
commit ab85dc7cb9
5 changed files with 57 additions and 63 deletions

View File

@ -6,15 +6,13 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted } from 'vue'; import { computed } from 'vue';
import enUS from '@arco-design/web-vue/es/locale/lang/en-us'; import enUS from '@arco-design/web-vue/es/locale/lang/en-us';
import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn'; import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn';
import GlobalSetting from '@/components/pure/global-setting/index.vue'; import GlobalSetting from '@/components/pure/global-setting/index.vue';
import useLocale from '@/locale/useLocale'; import useLocale from '@/locale/useLocale';
import { useTableStore } from './store';
const { currentLocale } = useLocale(); const { currentLocale } = useLocale();
const tableStore = useTableStore();
const locale = computed(() => { const locale = computed(() => {
switch (currentLocale.value) { switch (currentLocale.value) {
case 'zh-CN': case 'zh-CN':
@ -25,7 +23,4 @@
return zhCN; return zhCN;
} }
}); });
onMounted(() => {
tableStore.initColumn();
});
</script> </script>

View File

@ -6,7 +6,6 @@
unmount-on-close unmount-on-close
:footer="false" :footer="false"
:title="t('msTable.columnSetting.display')" :title="t('msTable.columnSetting.display')"
:descriptions="[]"
@cancel="handleCancel" @cancel="handleCancel"
> >
<div class="ms-table-column-seletor"> <div class="ms-table-column-seletor">
@ -34,13 +33,13 @@
<MsButton :disabled="!hasChange" @click="handleReset">{{ t('msTable.columnSetting.resetDefault') }}</MsButton> <MsButton :disabled="!hasChange" @click="handleReset">{{ t('msTable.columnSetting.resetDefault') }}</MsButton>
</div> </div>
<div class="flex-col"> <div class="flex-col">
<div v-for="(item, idx) in firstColumns" :key="item.dataIndex" class="column-item"> <div v-for="(item, idx) in nonSortColumn" :key="item.dataIndex" class="column-item">
<div>{{ t(item.title as string) }}</div> <div>{{ t(item.title as string) }}</div>
<a-switch size="small" :model-value="item.showInTable" @change="handleFisrtColumnChange(idx)" /> <a-switch size="small" :model-value="item.showInTable" @change="handleFisrtColumnChange(idx)" />
</div> </div>
</div> </div>
<a-divider orientation="center" class="non-sort">{{ t('msTable.columnSetting.nonSort') }}</a-divider> <a-divider orientation="center" class="non-sort">{{ t('msTable.columnSetting.nonSort') }}</a-divider>
<Draggable tag="div" :list="secondColumns" class="list-group" handle=".handle" item-key="dateIndex"> <Draggable tag="div" :list="couldSortColumn" class="list-group" handle=".handle" item-key="dateIndex">
<template #item="{ element, index }"> <template #item="{ element, index }">
<div class="column-drag-item"> <div class="column-drag-item">
<div class="flex items-center"> <div class="flex items-center">
@ -57,7 +56,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useI18n } from '@/hooks/useI18n'; import { useI18n } from '@/hooks/useI18n';
import { onMounted, ref } from 'vue'; import { onBeforeMount, ref } from 'vue';
import { useTableStore } from '@/store'; import { useTableStore } from '@/store';
import { MsTableColumn } from './type'; import { MsTableColumn } from './type';
import MsButton from '@/components/pure/ms-button/index.vue'; import MsButton from '@/components/pure/ms-button/index.vue';
@ -70,9 +69,9 @@
const { t } = useI18n(); const { t } = useI18n();
const currentMode = ref(''); const currentMode = ref('');
// //
const firstColumns = ref<MsTableColumn>([]); const nonSortColumn = ref<MsTableColumn>([]);
// //
const secondColumns = ref<MsTableColumn>([]); const couldSortColumn = ref<MsTableColumn>([]);
// //
const hasChange = ref(false); const hasChange = ref(false);
@ -93,7 +92,7 @@
const handleCancel = () => { const handleCancel = () => {
tableStore.setColumns( tableStore.setColumns(
props.tableKey, props.tableKey,
[...firstColumns.value, ...secondColumns.value], [...nonSortColumn.value, ...couldSortColumn.value],
currentMode.value as TableOpenDetailMode currentMode.value as TableOpenDetailMode
); );
visible.value = false; visible.value = false;
@ -101,9 +100,9 @@
}; };
const loadColumn = (key: string) => { const loadColumn = (key: string) => {
const { nonSortableColumns: noSort, couldSortableColumns: couldSort } = tableStore.getColumns(key); const { nonSort, couldSort } = tableStore.getColumns(key);
firstColumns.value = noSort; nonSortColumn.value = nonSort;
secondColumns.value = couldSort; couldSortColumn.value = couldSort;
}; };
const handleReset = () => { const handleReset = () => {
@ -111,13 +110,13 @@
}; };
const handleFisrtColumnChange = (idx: number) => { const handleFisrtColumnChange = (idx: number) => {
const item = firstColumns.value[idx]; const item = nonSortColumn.value[idx];
item.showInTable = !item.showInTable; item.showInTable = !item.showInTable;
hasChange.value = true; hasChange.value = true;
}; };
const handleSecondColumnChange = (idx: number) => { const handleSecondColumnChange = (idx: number) => {
const item = secondColumns.value[idx]; const item = couldSortColumn.value[idx];
item.showInTable = !item.showInTable; item.showInTable = !item.showInTable;
hasChange.value = true; hasChange.value = true;
}; };
@ -127,7 +126,7 @@
tableStore.setMode(props.tableKey, value as TableOpenDetailMode); tableStore.setMode(props.tableKey, value as TableOpenDetailMode);
}; };
onMounted(() => { onBeforeMount(() => {
if (props.tableKey) { if (props.tableKey) {
currentMode.value = tableStore.getMode(props.tableKey); currentMode.value = tableStore.getMode(props.tableKey);
loadColumn(props.tableKey); loadColumn(props.tableKey);

View File

@ -1,7 +1,5 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { MsTableSelectorItem, MsTableState, TableOpenDetailMode } from './types'; import { MsTableSelectorItem, MsTableState, TableOpenDetailMode } from './types';
import userGroupUsercolumns from './module/setting/system/usergroup';
import { TableKeyEnum } from '@/enums/tableEnum';
import { MsTableColumn } from '@/components/pure/ms-table/type'; import { MsTableColumn } from '@/components/pure/ms-table/type';
const msTableStore = defineStore('msTable', { const msTableStore = defineStore('msTable', {
@ -11,13 +9,12 @@ const msTableStore = defineStore('msTable', {
selectorColumnMap: new Map<string, MsTableSelectorItem>(), selectorColumnMap: new Map<string, MsTableSelectorItem>(),
}), }),
actions: { actions: {
initColumn() { initColumn(tableKey: string, column: MsTableColumn, mode: TableOpenDetailMode) {
const tmpMap = new Map<string, MsTableSelectorItem>(); if (!this.selectorColumnMap.has(tableKey)) {
tmpMap.set(TableKeyEnum.USERGROUPUSER, { const tmpMap = this.selectorColumnMap;
mode: 'drawer', tmpMap.set(tableKey, { mode, column });
column: userGroupUsercolumns, this.selectorColumnMap = tmpMap;
}); }
this.selectorColumnMap = tmpMap;
}, },
getMode(key: string): string { getMode(key: string): string {
if (this.selectorColumnMap.has(key)) { if (this.selectorColumnMap.has(key)) {
@ -33,14 +30,14 @@ const msTableStore = defineStore('msTable', {
} }
} }
}, },
getColumns(key: string): { nonSortableColumns: MsTableColumn; couldSortableColumns: MsTableColumn } { getColumns(key: string): { nonSort: MsTableColumn; couldSort: MsTableColumn } {
if (this.selectorColumnMap.has(key)) { if (this.selectorColumnMap.has(key)) {
const tmpArr = this.selectorColumnMap.get(key)?.column || []; const tmpArr = this.selectorColumnMap.get(key)?.column || [];
const nonSortableColumns = tmpArr.filter((item) => !item.showDrag); const nonSortableColumns = tmpArr.filter((item) => !item.showDrag);
const couldSortableColumns = tmpArr.filter((item) => item.showDrag); const couldSortableColumns = tmpArr.filter((item) => item.showDrag);
return { nonSortableColumns, couldSortableColumns }; return { nonSort: nonSortableColumns, couldSort: couldSortableColumns };
} }
return { nonSortableColumns: [], couldSortableColumns: [] }; return { nonSort: [], couldSort: [] };
}, },
setColumns(key: string, columns: MsTableColumn, mode: TableOpenDetailMode) { setColumns(key: string, columns: MsTableColumn, mode: TableOpenDetailMode) {
this.selectorColumnMap.set(key, { mode, column: columns }); this.selectorColumnMap.set(key, { mode, column: columns });

View File

@ -1,31 +0,0 @@
import { MsTableColumn } from '@/components/pure/ms-table/type';
const userGroupUsercolumns: MsTableColumn = [
{
title: 'system.userGroup.name',
dataIndex: 'name',
showDrag: false,
showInTable: true,
},
{
title: 'system.userGroup.email',
dataIndex: 'email',
showDrag: false,
showInTable: true,
},
{
title: 'system.userGroup.phone',
dataIndex: 'email',
showDrag: true,
showInTable: true,
},
{
title: 'system.userGroup.operation',
slotName: 'action',
fixed: 'right',
width: 200,
showDrag: true,
showInTable: true,
},
];
export default userGroupUsercolumns;

View File

@ -10,21 +10,55 @@
import { useI18n } from '@/hooks/useI18n'; import { useI18n } from '@/hooks/useI18n';
import useTable from '@/components/pure/ms-table/useTable'; import useTable from '@/components/pure/ms-table/useTable';
import MsBaseTable from '@/components/pure/ms-table/base-table.vue'; import MsBaseTable from '@/components/pure/ms-table/base-table.vue';
import { useUserGroupStore } from '@/store'; import { useUserGroupStore, useTableStore } from '@/store';
import { watchEffect } from 'vue'; import { watchEffect } from 'vue';
import { postUserByUserGroup, deleteUserFromUserGroup } from '@/api/modules/setting/usergroup'; import { postUserByUserGroup, deleteUserFromUserGroup } from '@/api/modules/setting/usergroup';
import { UserTableItem } from '@/models/setting/usergroup'; import { UserTableItem } from '@/models/setting/usergroup';
import { TableKeyEnum } from '@/enums/tableEnum'; import { TableKeyEnum } from '@/enums/tableEnum';
import MsButton from '@/components/pure/ms-button/index.vue'; import MsButton from '@/components/pure/ms-button/index.vue';
import { MsTableColumn } from '@/components/pure/ms-table/type';
const { t } = useI18n(); const { t } = useI18n();
const store = useUserGroupStore(); const store = useUserGroupStore();
const tableStore = useTableStore();
const userGroupUsercolumns: MsTableColumn = [
{
title: 'system.userGroup.name',
dataIndex: 'name',
showDrag: false,
showInTable: true,
},
{
title: 'system.userGroup.email',
dataIndex: 'email',
showDrag: false,
showInTable: true,
},
{
title: 'system.userGroup.phone',
dataIndex: 'email',
showDrag: true,
showInTable: true,
},
{
title: 'system.userGroup.operation',
slotName: 'action',
fixed: 'right',
width: 200,
showDrag: true,
showInTable: true,
},
];
tableStore.initColumn(TableKeyEnum.USERGROUPUSER, userGroupUsercolumns, 'drawer');
const { propsRes, propsEvent, loadList, setLoadListParams } = useTable(postUserByUserGroup, { const { propsRes, propsEvent, loadList, setLoadListParams } = useTable(postUserByUserGroup, {
tableKey: TableKeyEnum.USERGROUPUSER, tableKey: TableKeyEnum.USERGROUPUSER,
scroll: { y: 750, x: '600px' }, scroll: { y: 750, x: '600px' },
selectable: true, selectable: true,
}); });
const fetchData = async () => { const fetchData = async () => {
await loadList(); await loadList();
}; };