Commit b97d58839228c29fba696e52213664f16adf399f
Committed by
GitHub
1 parent
6e716c56
refactor: 完善ColumnSetting的操作逻辑 (#2745)
Showing
4 changed files
with
75 additions
and
36 deletions
src/components/Table/src/BasicTable.vue
... | ... | @@ -184,6 +184,7 @@ |
184 | 184 | getViewColumns, |
185 | 185 | getColumns, |
186 | 186 | setCacheColumnsByField, |
187 | + setCacheColumns, | |
187 | 188 | setColumns, |
188 | 189 | getColumnsRef, |
189 | 190 | getCacheColumns, |
... | ... | @@ -323,6 +324,7 @@ |
323 | 324 | getSize: () => { |
324 | 325 | return unref(getBindValues).size as SizeType; |
325 | 326 | }, |
327 | + setCacheColumns, | |
326 | 328 | }; |
327 | 329 | createTableContext({ ...tableAction, wrapRef, getBindValues }); |
328 | 330 | ... | ... |
src/components/Table/src/components/settings/ColumnSetting.vue
... | ... | @@ -99,7 +99,7 @@ |
99 | 99 | </Tooltip> |
100 | 100 | </template> |
101 | 101 | <script lang="ts"> |
102 | - import type { BasicColumn, ColumnChangeParam } from '../../types/table'; | |
102 | + import type { BasicColumn, BasicTableProps, ColumnChangeParam } from '../../types/table'; | |
103 | 103 | import { |
104 | 104 | defineComponent, |
105 | 105 | ref, |
... | ... | @@ -159,6 +159,10 @@ |
159 | 159 | |
160 | 160 | const defaultRowSelection = omit(table.getRowSelection(), 'selectedRowKeys'); |
161 | 161 | let inited = false; |
162 | + // 是否当前的setColums触发的 | |
163 | + let isSetColumnsFromThis = false; | |
164 | + // 是否当前组件触发的setProps | |
165 | + let isSetPropsFromThis = false; | |
162 | 166 | |
163 | 167 | const cachePlainOptions = ref<Options[]>([]); |
164 | 168 | const plainOptions = ref<Options[] | any>([]); |
... | ... | @@ -172,7 +176,8 @@ |
172 | 176 | checkedList: [], |
173 | 177 | defaultCheckList: [], |
174 | 178 | }); |
175 | - | |
179 | + /** 缓存初始化props */ | |
180 | + let cacheTableProps: Partial<BasicTableProps<any>> = {}; | |
176 | 181 | const checkIndex = ref(false); |
177 | 182 | const checkSelect = ref(false); |
178 | 183 | |
... | ... | @@ -185,7 +190,9 @@ |
185 | 190 | watchEffect(() => { |
186 | 191 | const columns = table.getColumns(); |
187 | 192 | setTimeout(() => { |
188 | - if (columns.length && !state.isInit) { | |
193 | + if (isSetColumnsFromThis) { | |
194 | + isSetColumnsFromThis = false; | |
195 | + } else if (columns.length) { | |
189 | 196 | init(); |
190 | 197 | } |
191 | 198 | }, 0); |
... | ... | @@ -193,6 +200,11 @@ |
193 | 200 | |
194 | 201 | watchEffect(() => { |
195 | 202 | const values = unref(getValues); |
203 | + if (isSetPropsFromThis) { | |
204 | + isSetPropsFromThis = false; | |
205 | + } else { | |
206 | + cacheTableProps = cloneDeep(values); | |
207 | + } | |
196 | 208 | checkIndex.value = !!values.showIndexColumn; |
197 | 209 | checkSelect.value = !!values.rowSelection; |
198 | 210 | }); |
... | ... | @@ -209,8 +221,17 @@ |
209 | 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 | 236 | const checkList = table |
216 | 237 | .getColumns({ ignoreAction: true, ignoreIndex: true }) |
... | ... | @@ -221,31 +242,25 @@ |
221 | 242 | return item.dataIndex || item.title; |
222 | 243 | }) |
223 | 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 | 251 | state.checkedList = checkList; |
252 | + // 是否列展示全选 | |
253 | + state.checkAll = checkList.length === columns.length; | |
254 | + inited = false; | |
255 | + handleVisibleChange(); | |
244 | 256 | } |
245 | 257 | |
246 | 258 | // checkAll change |
247 | 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 | 264 | if (e.target.checked) { |
250 | 265 | state.checkedList = checkList; |
251 | 266 | setColumns(checkList); |
... | ... | @@ -270,6 +285,9 @@ |
270 | 285 | checkedList.sort((prev, next) => { |
271 | 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 | 291 | setColumns(checkedList); |
274 | 292 | } |
275 | 293 | |
... | ... | @@ -277,11 +295,14 @@ |
277 | 295 | let sortableOrder: string[] = []; |
278 | 296 | // reset columns |
279 | 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 | 306 | sortable.sort(sortableOrder); |
286 | 307 | } |
287 | 308 | |
... | ... | @@ -316,12 +337,7 @@ |
316 | 337 | } |
317 | 338 | |
318 | 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 | 343 | // 记录原始order 序列 |
... | ... | @@ -332,6 +348,8 @@ |
332 | 348 | |
333 | 349 | // Control whether the serial number column is displayed |
334 | 350 | function handleIndexCheckChange(e: CheckboxChangeEvent) { |
351 | + isSetPropsFromThis = true; | |
352 | + isSetColumnsFromThis = true; | |
335 | 353 | table.setProps({ |
336 | 354 | showIndexColumn: e.target.checked, |
337 | 355 | }); |
... | ... | @@ -339,6 +357,8 @@ |
339 | 357 | |
340 | 358 | // Control whether the check box is displayed |
341 | 359 | function handleSelectCheckChange(e: CheckboxChangeEvent) { |
360 | + isSetPropsFromThis = true; | |
361 | + isSetColumnsFromThis = true; | |
342 | 362 | table.setProps({ |
343 | 363 | rowSelection: e.target.checked ? defaultRowSelection : undefined, |
344 | 364 | }); |
... | ... | @@ -360,11 +380,14 @@ |
360 | 380 | if (isFixed && !item.width) { |
361 | 381 | item.width = 100; |
362 | 382 | } |
383 | + updateSortOption(item); | |
363 | 384 | table.setCacheColumnsByField?.(item.dataIndex as string, { fixed: isFixed }); |
364 | 385 | setColumns(columns); |
365 | 386 | } |
366 | 387 | |
367 | 388 | function setColumns(columns: BasicColumn[] | string[]) { |
389 | + isSetPropsFromThis = true; | |
390 | + isSetColumnsFromThis = true; | |
368 | 391 | table.setColumns(columns); |
369 | 392 | const data: ColumnChangeParam[] = unref(plainSortOptions).map((col) => { |
370 | 393 | const visible = |
... | ... | @@ -384,6 +407,14 @@ |
384 | 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 | 418 | return { |
388 | 419 | t, |
389 | 420 | ...toRefs(state), | ... | ... |
src/components/Table/src/hooks/useColumns.ts
... | ... | @@ -260,6 +260,10 @@ export function useColumns( |
260 | 260 | function getCacheColumns() { |
261 | 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 | 268 | return { |
265 | 269 | getColumnsRef, |
... | ... | @@ -268,6 +272,7 @@ export function useColumns( |
268 | 272 | setColumns, |
269 | 273 | getViewColumns, |
270 | 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 | 116 | setShowPagination: (show: boolean) => Promise<void>; |
117 | 117 | getShowPagination: () => boolean; |
118 | 118 | setCacheColumnsByField?: (dataIndex: string | undefined, value: BasicColumn) => void; |
119 | + setCacheColumns?: (columns: BasicColumn[]) => void; | |
119 | 120 | } |
120 | 121 | |
121 | 122 | export interface FetchSetting { | ... | ... |