Skip to content

Commit

Permalink
optimize(projects): optimize table rowSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 13, 2024
1 parent d0f8e55 commit 49c572b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/hooks/common/table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { computed, effectScope, onScopeDispose, reactive, ref, shallowRef, toValue, watch } from 'vue';
import type { MaybeRef, Ref } from 'vue';
import type { TablePaginationConfig } from 'ant-design-vue';
import type { TableRowSelection } from 'ant-design-vue/es/table/interface';
import { cloneDeep } from 'lodash-es';
import { useElementSize } from '@vueuse/core';
import { useBoolean, useHookTable } from '@sa/hooks';
Expand Down Expand Up @@ -181,6 +182,14 @@ export function useTableOperate<T extends TableData = TableData>(data: Ref<T[]>,
checkedRowKeys.value = keys as T['id'][];
}

const rowSelection = computed<TableRowSelection<T>>(() => {
return {
type: 'checkbox',
selectedRowKeys: checkedRowKeys.value,
onChange: onSelectChange
};
});

/** the hook after the batch delete operation is completed */
async function onBatchDeleted() {
window.$message?.success($t('common.deleteSuccess'));
Expand All @@ -207,6 +216,7 @@ export function useTableOperate<T extends TableData = TableData>(data: Ref<T[]>,
handleEdit,
checkedRowKeys,
onSelectChange,
rowSelection,
onBatchDeleted,
onDeleted
};
Expand Down
8 changes: 2 additions & 6 deletions src/views/manage/menu/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const { columns, columnChecks, data, loading, pagination, getData } = useTable({
]
});
const { checkedRowKeys, onBatchDeleted, onDeleted, onSelectChange } = useTableOperate(data, getData);
const { checkedRowKeys, rowSelection, onBatchDeleted, onDeleted } = useTableOperate(data, getData);
const operateType = ref<OperateType>('add');
Expand Down Expand Up @@ -238,11 +238,7 @@ init();
ref="tableWrapperRef"
:columns="columns"
:data-source="data"
:row-selection="{
type: 'checkbox',
selectedRowKeys: checkedRowKeys,
onChange: onSelectChange
}"
:row-selection="rowSelection"
size="small"
:loading="loading"
row-key="id"
Expand Down
10 changes: 3 additions & 7 deletions src/views/manage/role/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ const {
handleAdd,
handleEdit,
checkedRowKeys,
rowSelection,
onBatchDeleted,
onDeleted,
onSelectChange
onDeleted
// closeDrawer
} = useTableOperate(data, getData);
Expand Down Expand Up @@ -142,11 +142,7 @@ function edit(id: number) {
ref="tableWrapperRef"
:columns="columns"
:data-source="data"
:row-selection="{
type: 'checkbox',
selectedRowKeys: checkedRowKeys,
onChange: onSelectChange
}"
:row-selection="rowSelection"
:loading="loading"
row-key="id"
size="small"
Expand Down
8 changes: 2 additions & 6 deletions src/views/manage/user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ const {
operateType,
editingData,
handleAdd,
onSelectChange,
handleEdit,
checkedRowKeys,
rowSelection,
onBatchDeleted,
onDeleted
// closeDrawer
Expand Down Expand Up @@ -178,11 +178,7 @@ function edit(id: number) {
:columns="columns"
:data-source="data"
size="small"
:row-selection="{
type: 'checkbox',
selectedRowKeys: checkedRowKeys,
onChange: onSelectChange
}"
:row-selection="rowSelection"
:scroll="scrollConfig"
:loading="loading"
row-key="id"
Expand Down

0 comments on commit 49c572b

Please sign in to comment.