Commit 74d47424069c4dca71579637916431aa80014fd8

Authored by vben
1 parent e1e9baa8

fix: the action column appears repeatedly in the table (#53)

src/components/Table/src/BasicTable.vue
@@ -190,6 +190,14 @@ @@ -190,6 +190,14 @@
190 return !!unref(getDataSourceRef).length; 190 return !!unref(getDataSourceRef).length;
191 }); 191 });
192 192
  193 + watch(
  194 + () => unref(getDataSourceRef),
  195 + () => {
  196 + handleSummary();
  197 + },
  198 + { immediate: true }
  199 + );
  200 +
193 function getRowClassName(record: TableCustomRecord<any>, index: number) { 201 function getRowClassName(record: TableCustomRecord<any>, index: number) {
194 const { striped, rowClassName } = unref(getMergeProps); 202 const { striped, rowClassName } = unref(getMergeProps);
195 if (!striped) return; 203 if (!striped) return;
@@ -242,14 +250,6 @@ @@ -242,14 +250,6 @@
242 } 250 }
243 } 251 }
244 252
245 - watch(  
246 - () => unref(getDataSourceRef),  
247 - () => {  
248 - handleSummary();  
249 - },  
250 - { immediate: true }  
251 - );  
252 -  
253 function setProps(props: Partial<BasicTableProps>) { 253 function setProps(props: Partial<BasicTableProps>) {
254 innerPropsRef.value = deepMerge(unref(innerPropsRef) || {}, props); 254 innerPropsRef.value = deepMerge(unref(innerPropsRef) || {}, props);
255 } 255 }
src/components/Table/src/hooks/useColumns.ts
1 import { BasicColumn, BasicTableProps } from '../types/table'; 1 import { BasicColumn, BasicTableProps } from '../types/table';
2 import { PaginationProps } from '../types/pagination'; 2 import { PaginationProps } from '../types/pagination';
3 -import { unref, ComputedRef, Ref, computed, watch, ref } from 'vue'; 3 +import { unref, ComputedRef, Ref, computed, watchEffect, ref, toRaw } from 'vue';
4 import { isBoolean, isArray, isObject } from '/@/utils/is'; 4 import { isBoolean, isArray, isObject } from '/@/utils/is';
5 import { PAGE_SIZE } from '../const'; 5 import { PAGE_SIZE } from '../const';
6 import { useProps } from './useProps'; 6 import { useProps } from './useProps';
@@ -10,20 +10,9 @@ export function useColumns( @@ -10,20 +10,9 @@ export function useColumns(
10 getPaginationRef: ComputedRef<false | PaginationProps> 10 getPaginationRef: ComputedRef<false | PaginationProps>
11 ) { 11 ) {
12 const { propsRef } = useProps(refProps); 12 const { propsRef } = useProps(refProps);
13 -  
14 const columnsRef = (ref(unref(propsRef).columns) as unknown) as Ref<BasicColumn[]>; 13 const columnsRef = (ref(unref(propsRef).columns) as unknown) as Ref<BasicColumn[]>;
15 const cacheColumnsRef = (ref(unref(propsRef).columns) as unknown) as Ref<BasicColumn[]>; 14 const cacheColumnsRef = (ref(unref(propsRef).columns) as unknown) as Ref<BasicColumn[]>;
16 15
17 - watch(  
18 - () => unref(propsRef).columns,  
19 - (columns) => {  
20 - columnsRef.value = columns;  
21 - cacheColumnsRef.value = columns;  
22 - },  
23 - {  
24 - immediate: true,  
25 - }  
26 - );  
27 const getColumnsRef = computed(() => { 16 const getColumnsRef = computed(() => {
28 const props = unref(propsRef); 17 const props = unref(propsRef);
29 const { showIndexColumn, indexColumnProps, ellipsis, actionColumn, isTreeTable } = props; 18 const { showIndexColumn, indexColumnProps, ellipsis, actionColumn, isTreeTable } = props;
@@ -81,16 +70,24 @@ export function useColumns( @@ -81,16 +70,24 @@ export function useColumns(
81 } 70 }
82 if (actionColumn) { 71 if (actionColumn) {
83 const hasIndex = columns.findIndex((column) => column.flag === 'ACTION'); 72 const hasIndex = columns.findIndex((column) => column.flag === 'ACTION');
84 - columns.push({  
85 - ...(hasIndex === -1 ? columns[hasIndex] : {}),  
86 - fixed: 'right',  
87 - ...actionColumn,  
88 - flag: 'ACTION',  
89 - }); 73 + if (hasIndex === -1) {
  74 + columns.push({
  75 + ...columns[hasIndex],
  76 + fixed: 'right',
  77 + ...actionColumn,
  78 + flag: 'ACTION',
  79 + });
  80 + }
90 } 81 }
91 return columns; 82 return columns;
92 }); 83 });
93 84
  85 + watchEffect(() => {
  86 + const columns = toRaw(unref(propsRef).columns);
  87 + columnsRef.value = columns;
  88 + cacheColumnsRef.value = columns;
  89 + });
  90 +
94 function setColumns(columns: BasicColumn[] | string[]) { 91 function setColumns(columns: BasicColumn[] | string[]) {
95 if (!isArray(columns)) return; 92 if (!isArray(columns)) return;
96 93