mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-25 02:58:43 +08:00
refactor: 完善ColumnSetting的操作逻辑 (#2745)
This commit is contained in:
parent
6e716c5607
commit
b97d588392
@ -184,6 +184,7 @@
|
|||||||
getViewColumns,
|
getViewColumns,
|
||||||
getColumns,
|
getColumns,
|
||||||
setCacheColumnsByField,
|
setCacheColumnsByField,
|
||||||
|
setCacheColumns,
|
||||||
setColumns,
|
setColumns,
|
||||||
getColumnsRef,
|
getColumnsRef,
|
||||||
getCacheColumns,
|
getCacheColumns,
|
||||||
@ -323,6 +324,7 @@
|
|||||||
getSize: () => {
|
getSize: () => {
|
||||||
return unref(getBindValues).size as SizeType;
|
return unref(getBindValues).size as SizeType;
|
||||||
},
|
},
|
||||||
|
setCacheColumns,
|
||||||
};
|
};
|
||||||
createTableContext({ ...tableAction, wrapRef, getBindValues });
|
createTableContext({ ...tableAction, wrapRef, getBindValues });
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@
|
|||||||
</Tooltip>
|
</Tooltip>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { BasicColumn, ColumnChangeParam } from '../../types/table';
|
import type { BasicColumn, BasicTableProps, ColumnChangeParam } from '../../types/table';
|
||||||
import {
|
import {
|
||||||
defineComponent,
|
defineComponent,
|
||||||
ref,
|
ref,
|
||||||
@ -159,6 +159,10 @@
|
|||||||
|
|
||||||
const defaultRowSelection = omit(table.getRowSelection(), 'selectedRowKeys');
|
const defaultRowSelection = omit(table.getRowSelection(), 'selectedRowKeys');
|
||||||
let inited = false;
|
let inited = false;
|
||||||
|
// 是否当前的setColums触发的
|
||||||
|
let isSetColumnsFromThis = false;
|
||||||
|
// 是否当前组件触发的setProps
|
||||||
|
let isSetPropsFromThis = false;
|
||||||
|
|
||||||
const cachePlainOptions = ref<Options[]>([]);
|
const cachePlainOptions = ref<Options[]>([]);
|
||||||
const plainOptions = ref<Options[] | any>([]);
|
const plainOptions = ref<Options[] | any>([]);
|
||||||
@ -172,7 +176,8 @@
|
|||||||
checkedList: [],
|
checkedList: [],
|
||||||
defaultCheckList: [],
|
defaultCheckList: [],
|
||||||
});
|
});
|
||||||
|
/** 缓存初始化props */
|
||||||
|
let cacheTableProps: Partial<BasicTableProps<any>> = {};
|
||||||
const checkIndex = ref(false);
|
const checkIndex = ref(false);
|
||||||
const checkSelect = ref(false);
|
const checkSelect = ref(false);
|
||||||
|
|
||||||
@ -185,7 +190,9 @@
|
|||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
const columns = table.getColumns();
|
const columns = table.getColumns();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (columns.length && !state.isInit) {
|
if (isSetColumnsFromThis) {
|
||||||
|
isSetColumnsFromThis = false;
|
||||||
|
} else if (columns.length) {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
}, 0);
|
}, 0);
|
||||||
@ -193,6 +200,11 @@
|
|||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
const values = unref(getValues);
|
const values = unref(getValues);
|
||||||
|
if (isSetPropsFromThis) {
|
||||||
|
isSetPropsFromThis = false;
|
||||||
|
} else {
|
||||||
|
cacheTableProps = cloneDeep(values);
|
||||||
|
}
|
||||||
checkIndex.value = !!values.showIndexColumn;
|
checkIndex.value = !!values.showIndexColumn;
|
||||||
checkSelect.value = !!values.rowSelection;
|
checkSelect.value = !!values.rowSelection;
|
||||||
});
|
});
|
||||||
@ -209,8 +221,17 @@
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
async function init(isReset = false) {
|
||||||
const columns = getColumns();
|
// Sortablejs存在bug,不知道在哪个步骤中会向el append了一个childNode,因此这里先清空childNode
|
||||||
|
// 有可能复现上述问题的操作:拖拽一个元素,快速的上下移动,最后放到最后的位置中松手
|
||||||
|
plainOptions.value = [];
|
||||||
|
const columnListEl = unref(columnListRef);
|
||||||
|
if (columnListEl && (columnListEl as any).$el) {
|
||||||
|
const el = (columnListEl as any).$el as Element;
|
||||||
|
Array.from(el.children).forEach((item) => el.removeChild(item));
|
||||||
|
}
|
||||||
|
await nextTick();
|
||||||
|
const columns = isReset ? cloneDeep(cachePlainOptions.value) : getColumns();
|
||||||
|
|
||||||
const checkList = table
|
const checkList = table
|
||||||
.getColumns({ ignoreAction: true, ignoreIndex: true })
|
.getColumns({ ignoreAction: true, ignoreIndex: true })
|
||||||
@ -221,31 +242,25 @@
|
|||||||
return item.dataIndex || item.title;
|
return item.dataIndex || item.title;
|
||||||
})
|
})
|
||||||
.filter(Boolean) as string[];
|
.filter(Boolean) as string[];
|
||||||
|
plainOptions.value = columns;
|
||||||
if (!plainOptions.value.length) {
|
plainSortOptions.value = columns;
|
||||||
plainOptions.value = columns;
|
// 更新缓存配置
|
||||||
plainSortOptions.value = columns;
|
table.setCacheColumns?.(columns);
|
||||||
cachePlainOptions.value = columns;
|
!isReset && (cachePlainOptions.value = cloneDeep(columns));
|
||||||
state.defaultCheckList = checkList;
|
state.defaultCheckList = checkList;
|
||||||
} else {
|
|
||||||
// const fixedColumns = columns.filter((item) =>
|
|
||||||
// Reflect.has(item, 'fixed')
|
|
||||||
// ) as BasicColumn[];
|
|
||||||
|
|
||||||
unref(plainOptions).forEach((item: BasicColumn) => {
|
|
||||||
const findItem = columns.find((col: BasicColumn) => col.dataIndex === item.dataIndex);
|
|
||||||
if (findItem) {
|
|
||||||
item.fixed = findItem.fixed;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
state.isInit = true;
|
|
||||||
state.checkedList = checkList;
|
state.checkedList = checkList;
|
||||||
|
// 是否列展示全选
|
||||||
|
state.checkAll = checkList.length === columns.length;
|
||||||
|
inited = false;
|
||||||
|
handleVisibleChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkAll change
|
// checkAll change
|
||||||
function onCheckAllChange(e: CheckboxChangeEvent) {
|
function onCheckAllChange(e: CheckboxChangeEvent) {
|
||||||
const checkList = plainOptions.value.map((item) => item.value);
|
const checkList = plainSortOptions.value.map((item) => item.value);
|
||||||
|
plainSortOptions.value.forEach(
|
||||||
|
(item) => ((item as BasicColumn).defaultHidden = !e.target.checked),
|
||||||
|
);
|
||||||
if (e.target.checked) {
|
if (e.target.checked) {
|
||||||
state.checkedList = checkList;
|
state.checkedList = checkList;
|
||||||
setColumns(checkList);
|
setColumns(checkList);
|
||||||
@ -270,6 +285,9 @@
|
|||||||
checkedList.sort((prev, next) => {
|
checkedList.sort((prev, next) => {
|
||||||
return sortList.indexOf(prev) - sortList.indexOf(next);
|
return sortList.indexOf(prev) - sortList.indexOf(next);
|
||||||
});
|
});
|
||||||
|
unref(plainSortOptions).forEach((item) => {
|
||||||
|
(item as BasicColumn).defaultHidden = !checkedList.includes(item.value);
|
||||||
|
});
|
||||||
setColumns(checkedList);
|
setColumns(checkedList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,11 +295,14 @@
|
|||||||
let sortableOrder: string[] = [];
|
let sortableOrder: string[] = [];
|
||||||
// reset columns
|
// reset columns
|
||||||
function reset() {
|
function reset() {
|
||||||
state.checkedList = [...state.defaultCheckList];
|
setColumns(cachePlainOptions.value);
|
||||||
state.checkAll = true;
|
init(true);
|
||||||
plainOptions.value = unref(cachePlainOptions);
|
checkIndex.value = !!cacheTableProps.showIndexColumn;
|
||||||
plainSortOptions.value = unref(cachePlainOptions);
|
checkSelect.value = !!cacheTableProps.rowSelection;
|
||||||
setColumns(table.getCacheColumns());
|
table.setProps({
|
||||||
|
showIndexColumn: checkIndex.value,
|
||||||
|
rowSelection: checkSelect.value ? defaultRowSelection : undefined,
|
||||||
|
});
|
||||||
sortable.sort(sortableOrder);
|
sortable.sort(sortableOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,12 +337,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
plainSortOptions.value = columns;
|
plainSortOptions.value = columns;
|
||||||
|
setColumns(columns.filter((item) => state.checkedList.includes(item.value)));
|
||||||
setColumns(
|
|
||||||
columns
|
|
||||||
.map((col: Options) => col.value)
|
|
||||||
.filter((value: string) => state.checkedList.includes(value)),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// 记录原始order 序列
|
// 记录原始order 序列
|
||||||
@ -332,6 +348,8 @@
|
|||||||
|
|
||||||
// Control whether the serial number column is displayed
|
// Control whether the serial number column is displayed
|
||||||
function handleIndexCheckChange(e: CheckboxChangeEvent) {
|
function handleIndexCheckChange(e: CheckboxChangeEvent) {
|
||||||
|
isSetPropsFromThis = true;
|
||||||
|
isSetColumnsFromThis = true;
|
||||||
table.setProps({
|
table.setProps({
|
||||||
showIndexColumn: e.target.checked,
|
showIndexColumn: e.target.checked,
|
||||||
});
|
});
|
||||||
@ -339,6 +357,8 @@
|
|||||||
|
|
||||||
// Control whether the check box is displayed
|
// Control whether the check box is displayed
|
||||||
function handleSelectCheckChange(e: CheckboxChangeEvent) {
|
function handleSelectCheckChange(e: CheckboxChangeEvent) {
|
||||||
|
isSetPropsFromThis = true;
|
||||||
|
isSetColumnsFromThis = true;
|
||||||
table.setProps({
|
table.setProps({
|
||||||
rowSelection: e.target.checked ? defaultRowSelection : undefined,
|
rowSelection: e.target.checked ? defaultRowSelection : undefined,
|
||||||
});
|
});
|
||||||
@ -360,11 +380,14 @@
|
|||||||
if (isFixed && !item.width) {
|
if (isFixed && !item.width) {
|
||||||
item.width = 100;
|
item.width = 100;
|
||||||
}
|
}
|
||||||
|
updateSortOption(item);
|
||||||
table.setCacheColumnsByField?.(item.dataIndex as string, { fixed: isFixed });
|
table.setCacheColumnsByField?.(item.dataIndex as string, { fixed: isFixed });
|
||||||
setColumns(columns);
|
setColumns(columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setColumns(columns: BasicColumn[] | string[]) {
|
function setColumns(columns: BasicColumn[] | string[]) {
|
||||||
|
isSetPropsFromThis = true;
|
||||||
|
isSetColumnsFromThis = true;
|
||||||
table.setColumns(columns);
|
table.setColumns(columns);
|
||||||
const data: ColumnChangeParam[] = unref(plainSortOptions).map((col) => {
|
const data: ColumnChangeParam[] = unref(plainSortOptions).map((col) => {
|
||||||
const visible =
|
const visible =
|
||||||
@ -384,6 +407,14 @@
|
|||||||
: getParentContainer();
|
: getParentContainer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateSortOption(column: BasicColumn) {
|
||||||
|
plainSortOptions.value.forEach((item) => {
|
||||||
|
if (item.value === column.dataIndex) {
|
||||||
|
Object.assign(item, column);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
t,
|
t,
|
||||||
...toRefs(state),
|
...toRefs(state),
|
||||||
|
@ -260,6 +260,10 @@ export function useColumns(
|
|||||||
function getCacheColumns() {
|
function getCacheColumns() {
|
||||||
return cacheColumns;
|
return cacheColumns;
|
||||||
}
|
}
|
||||||
|
function setCacheColumns(columns: BasicColumn[]) {
|
||||||
|
if (!isArray(columns)) return;
|
||||||
|
cacheColumns = columns.filter((item) => !item.flag);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getColumnsRef,
|
getColumnsRef,
|
||||||
@ -268,6 +272,7 @@ export function useColumns(
|
|||||||
setColumns,
|
setColumns,
|
||||||
getViewColumns,
|
getViewColumns,
|
||||||
setCacheColumnsByField,
|
setCacheColumnsByField,
|
||||||
|
setCacheColumns,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,6 +116,7 @@ export interface TableActionType {
|
|||||||
setShowPagination: (show: boolean) => Promise<void>;
|
setShowPagination: (show: boolean) => Promise<void>;
|
||||||
getShowPagination: () => boolean;
|
getShowPagination: () => boolean;
|
||||||
setCacheColumnsByField?: (dataIndex: string | undefined, value: BasicColumn) => void;
|
setCacheColumnsByField?: (dataIndex: string | undefined, value: BasicColumn) => void;
|
||||||
|
setCacheColumns?: (columns: BasicColumn[]) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FetchSetting {
|
export interface FetchSetting {
|
||||||
|
Loading…
Reference in New Issue
Block a user