Commit b97d58839228c29fba696e52213664f16adf399f

Authored by GauharChan
Committed by GitHub
1 parent 6e716c56

refactor: 完善ColumnSetting的操作逻辑 (#2745)

src/components/Table/src/BasicTable.vue
@@ -184,6 +184,7 @@ @@ -184,6 +184,7 @@
184 getViewColumns, 184 getViewColumns,
185 getColumns, 185 getColumns,
186 setCacheColumnsByField, 186 setCacheColumnsByField,
  187 + setCacheColumns,
187 setColumns, 188 setColumns,
188 getColumnsRef, 189 getColumnsRef,
189 getCacheColumns, 190 getCacheColumns,
@@ -323,6 +324,7 @@ @@ -323,6 +324,7 @@
323 getSize: () => { 324 getSize: () => {
324 return unref(getBindValues).size as SizeType; 325 return unref(getBindValues).size as SizeType;
325 }, 326 },
  327 + setCacheColumns,
326 }; 328 };
327 createTableContext({ ...tableAction, wrapRef, getBindValues }); 329 createTableContext({ ...tableAction, wrapRef, getBindValues });
328 330
src/components/Table/src/components/settings/ColumnSetting.vue
@@ -99,7 +99,7 @@ @@ -99,7 +99,7 @@
99 </Tooltip> 99 </Tooltip>
100 </template> 100 </template>
101 <script lang="ts"> 101 <script lang="ts">
102 - import type { BasicColumn, ColumnChangeParam } from '../../types/table'; 102 + import type { BasicColumn, BasicTableProps, ColumnChangeParam } from '../../types/table';
103 import { 103 import {
104 defineComponent, 104 defineComponent,
105 ref, 105 ref,
@@ -159,6 +159,10 @@ @@ -159,6 +159,10 @@
159 159
160 const defaultRowSelection = omit(table.getRowSelection(), 'selectedRowKeys'); 160 const defaultRowSelection = omit(table.getRowSelection(), 'selectedRowKeys');
161 let inited = false; 161 let inited = false;
  162 + // 是否当前的setColums触发的
  163 + let isSetColumnsFromThis = false;
  164 + // 是否当前组件触发的setProps
  165 + let isSetPropsFromThis = false;
162 166
163 const cachePlainOptions = ref<Options[]>([]); 167 const cachePlainOptions = ref<Options[]>([]);
164 const plainOptions = ref<Options[] | any>([]); 168 const plainOptions = ref<Options[] | any>([]);
@@ -172,7 +176,8 @@ @@ -172,7 +176,8 @@
172 checkedList: [], 176 checkedList: [],
173 defaultCheckList: [], 177 defaultCheckList: [],
174 }); 178 });
175 - 179 + /** 缓存初始化props */
  180 + let cacheTableProps: Partial<BasicTableProps<any>> = {};
176 const checkIndex = ref(false); 181 const checkIndex = ref(false);
177 const checkSelect = ref(false); 182 const checkSelect = ref(false);
178 183
@@ -185,7 +190,9 @@ @@ -185,7 +190,9 @@
185 watchEffect(() => { 190 watchEffect(() => {
186 const columns = table.getColumns(); 191 const columns = table.getColumns();
187 setTimeout(() => { 192 setTimeout(() => {
188 - if (columns.length && !state.isInit) { 193 + if (isSetColumnsFromThis) {
  194 + isSetColumnsFromThis = false;
  195 + } else if (columns.length) {
189 init(); 196 init();
190 } 197 }
191 }, 0); 198 }, 0);
@@ -193,6 +200,11 @@ @@ -193,6 +200,11 @@
193 200
194 watchEffect(() => { 201 watchEffect(() => {
195 const values = unref(getValues); 202 const values = unref(getValues);
  203 + if (isSetPropsFromThis) {
  204 + isSetPropsFromThis = false;
  205 + } else {
  206 + cacheTableProps = cloneDeep(values);
  207 + }
196 checkIndex.value = !!values.showIndexColumn; 208 checkIndex.value = !!values.showIndexColumn;
197 checkSelect.value = !!values.rowSelection; 209 checkSelect.value = !!values.rowSelection;
198 }); 210 });
@@ -209,8 +221,17 @@ @@ -209,8 +221,17 @@
209 return ret; 221 return ret;
210 } 222 }
211 223
212 - function init() {  
213 - const columns = getColumns(); 224 + async function init(isReset = false) {
  225 + // Sortablejs存在bug,不知道在哪个步骤中会向el append了一个childNode,因此这里先清空childNode
  226 + // 有可能复现上述问题的操作:拖拽一个元素,快速的上下移动,最后放到最后的位置中松手
  227 + plainOptions.value = [];
  228 + const columnListEl = unref(columnListRef);
  229 + if (columnListEl && (columnListEl as any).$el) {
  230 + const el = (columnListEl as any).$el as Element;
  231 + Array.from(el.children).forEach((item) => el.removeChild(item));
  232 + }
  233 + await nextTick();
  234 + const columns = isReset ? cloneDeep(cachePlainOptions.value) : getColumns();
214 235
215 const checkList = table 236 const checkList = table
216 .getColumns({ ignoreAction: true, ignoreIndex: true }) 237 .getColumns({ ignoreAction: true, ignoreIndex: true })
@@ -221,31 +242,25 @@ @@ -221,31 +242,25 @@
221 return item.dataIndex || item.title; 242 return item.dataIndex || item.title;
222 }) 243 })
223 .filter(Boolean) as string[]; 244 .filter(Boolean) as string[];
224 -  
225 - if (!plainOptions.value.length) {  
226 - plainOptions.value = columns;  
227 - plainSortOptions.value = columns;  
228 - cachePlainOptions.value = columns;  
229 - state.defaultCheckList = checkList;  
230 - } else {  
231 - // const fixedColumns = columns.filter((item) =>  
232 - // Reflect.has(item, 'fixed')  
233 - // ) as BasicColumn[];  
234 -  
235 - unref(plainOptions).forEach((item: BasicColumn) => {  
236 - const findItem = columns.find((col: BasicColumn) => col.dataIndex === item.dataIndex);  
237 - if (findItem) {  
238 - item.fixed = findItem.fixed;  
239 - }  
240 - });  
241 - }  
242 - state.isInit = true; 245 + plainOptions.value = columns;
  246 + plainSortOptions.value = columns;
  247 + // 更新缓存配置
  248 + table.setCacheColumns?.(columns);
  249 + !isReset && (cachePlainOptions.value = cloneDeep(columns));
  250 + state.defaultCheckList = checkList;
243 state.checkedList = checkList; 251 state.checkedList = checkList;
  252 + // 是否列展示全选
  253 + state.checkAll = checkList.length === columns.length;
  254 + inited = false;
  255 + handleVisibleChange();
244 } 256 }
245 257
246 // checkAll change 258 // checkAll change
247 function onCheckAllChange(e: CheckboxChangeEvent) { 259 function onCheckAllChange(e: CheckboxChangeEvent) {
248 - const checkList = plainOptions.value.map((item) => item.value); 260 + const checkList = plainSortOptions.value.map((item) => item.value);
  261 + plainSortOptions.value.forEach(
  262 + (item) => ((item as BasicColumn).defaultHidden = !e.target.checked),
  263 + );
249 if (e.target.checked) { 264 if (e.target.checked) {
250 state.checkedList = checkList; 265 state.checkedList = checkList;
251 setColumns(checkList); 266 setColumns(checkList);
@@ -270,6 +285,9 @@ @@ -270,6 +285,9 @@
270 checkedList.sort((prev, next) => { 285 checkedList.sort((prev, next) => {
271 return sortList.indexOf(prev) - sortList.indexOf(next); 286 return sortList.indexOf(prev) - sortList.indexOf(next);
272 }); 287 });
  288 + unref(plainSortOptions).forEach((item) => {
  289 + (item as BasicColumn).defaultHidden = !checkedList.includes(item.value);
  290 + });
273 setColumns(checkedList); 291 setColumns(checkedList);
274 } 292 }
275 293
@@ -277,11 +295,14 @@ @@ -277,11 +295,14 @@
277 let sortableOrder: string[] = []; 295 let sortableOrder: string[] = [];
278 // reset columns 296 // reset columns
279 function reset() { 297 function reset() {
280 - state.checkedList = [...state.defaultCheckList];  
281 - state.checkAll = true;  
282 - plainOptions.value = unref(cachePlainOptions);  
283 - plainSortOptions.value = unref(cachePlainOptions);  
284 - setColumns(table.getCacheColumns()); 298 + setColumns(cachePlainOptions.value);
  299 + init(true);
  300 + checkIndex.value = !!cacheTableProps.showIndexColumn;
  301 + checkSelect.value = !!cacheTableProps.rowSelection;
  302 + table.setProps({
  303 + showIndexColumn: checkIndex.value,
  304 + rowSelection: checkSelect.value ? defaultRowSelection : undefined,
  305 + });
285 sortable.sort(sortableOrder); 306 sortable.sort(sortableOrder);
286 } 307 }
287 308
@@ -316,12 +337,7 @@ @@ -316,12 +337,7 @@
316 } 337 }
317 338
318 plainSortOptions.value = columns; 339 plainSortOptions.value = columns;
319 -  
320 - setColumns(  
321 - columns  
322 - .map((col: Options) => col.value)  
323 - .filter((value: string) => state.checkedList.includes(value)),  
324 - ); 340 + setColumns(columns.filter((item) => state.checkedList.includes(item.value)));
325 }, 341 },
326 }); 342 });
327 // 记录原始order 序列 343 // 记录原始order 序列
@@ -332,6 +348,8 @@ @@ -332,6 +348,8 @@
332 348
333 // Control whether the serial number column is displayed 349 // Control whether the serial number column is displayed
334 function handleIndexCheckChange(e: CheckboxChangeEvent) { 350 function handleIndexCheckChange(e: CheckboxChangeEvent) {
  351 + isSetPropsFromThis = true;
  352 + isSetColumnsFromThis = true;
335 table.setProps({ 353 table.setProps({
336 showIndexColumn: e.target.checked, 354 showIndexColumn: e.target.checked,
337 }); 355 });
@@ -339,6 +357,8 @@ @@ -339,6 +357,8 @@
339 357
340 // Control whether the check box is displayed 358 // Control whether the check box is displayed
341 function handleSelectCheckChange(e: CheckboxChangeEvent) { 359 function handleSelectCheckChange(e: CheckboxChangeEvent) {
  360 + isSetPropsFromThis = true;
  361 + isSetColumnsFromThis = true;
342 table.setProps({ 362 table.setProps({
343 rowSelection: e.target.checked ? defaultRowSelection : undefined, 363 rowSelection: e.target.checked ? defaultRowSelection : undefined,
344 }); 364 });
@@ -360,11 +380,14 @@ @@ -360,11 +380,14 @@
360 if (isFixed && !item.width) { 380 if (isFixed && !item.width) {
361 item.width = 100; 381 item.width = 100;
362 } 382 }
  383 + updateSortOption(item);
363 table.setCacheColumnsByField?.(item.dataIndex as string, { fixed: isFixed }); 384 table.setCacheColumnsByField?.(item.dataIndex as string, { fixed: isFixed });
364 setColumns(columns); 385 setColumns(columns);
365 } 386 }
366 387
367 function setColumns(columns: BasicColumn[] | string[]) { 388 function setColumns(columns: BasicColumn[] | string[]) {
  389 + isSetPropsFromThis = true;
  390 + isSetColumnsFromThis = true;
368 table.setColumns(columns); 391 table.setColumns(columns);
369 const data: ColumnChangeParam[] = unref(plainSortOptions).map((col) => { 392 const data: ColumnChangeParam[] = unref(plainSortOptions).map((col) => {
370 const visible = 393 const visible =
@@ -384,6 +407,14 @@ @@ -384,6 +407,14 @@
384 : getParentContainer(); 407 : getParentContainer();
385 } 408 }
386 409
  410 + function updateSortOption(column: BasicColumn) {
  411 + plainSortOptions.value.forEach((item) => {
  412 + if (item.value === column.dataIndex) {
  413 + Object.assign(item, column);
  414 + }
  415 + });
  416 + }
  417 +
387 return { 418 return {
388 t, 419 t,
389 ...toRefs(state), 420 ...toRefs(state),
src/components/Table/src/hooks/useColumns.ts
@@ -260,6 +260,10 @@ export function useColumns( @@ -260,6 +260,10 @@ export function useColumns(
260 function getCacheColumns() { 260 function getCacheColumns() {
261 return cacheColumns; 261 return cacheColumns;
262 } 262 }
  263 + function setCacheColumns(columns: BasicColumn[]) {
  264 + if (!isArray(columns)) return;
  265 + cacheColumns = columns.filter((item) => !item.flag);
  266 + }
263 267
264 return { 268 return {
265 getColumnsRef, 269 getColumnsRef,
@@ -268,6 +272,7 @@ export function useColumns( @@ -268,6 +272,7 @@ export function useColumns(
268 setColumns, 272 setColumns,
269 getViewColumns, 273 getViewColumns,
270 setCacheColumnsByField, 274 setCacheColumnsByField,
  275 + setCacheColumns,
271 }; 276 };
272 } 277 }
273 278
src/components/Table/src/types/table.ts
@@ -116,6 +116,7 @@ export interface TableActionType { @@ -116,6 +116,7 @@ export interface TableActionType {
116 setShowPagination: (show: boolean) => Promise<void>; 116 setShowPagination: (show: boolean) => Promise<void>;
117 getShowPagination: () => boolean; 117 getShowPagination: () => boolean;
118 setCacheColumnsByField?: (dataIndex: string | undefined, value: BasicColumn) => void; 118 setCacheColumnsByField?: (dataIndex: string | undefined, value: BasicColumn) => void;
  119 + setCacheColumns?: (columns: BasicColumn[]) => void;
119 } 120 }
120 121
121 export interface FetchSetting { 122 export interface FetchSetting {