refactor: 完善ColumnSetting的操作逻辑

pull/12/head
xingyu 2023-05-10 18:35:47 +08:00
parent a261242279
commit 7f9881fbc4
4 changed files with 76 additions and 32 deletions

View File

@ -160,7 +160,7 @@ function handleTableChange(...args) {
onChange && isFunction(onChange) && onChange.call(undefined, ...args) onChange && isFunction(onChange) && onChange.call(undefined, ...args)
} }
const { getViewColumns, getColumns, setCacheColumnsByField, setColumns, getColumnsRef, getCacheColumns } = useColumns( const { getViewColumns, getColumns, setCacheColumnsByField, setCacheColumns, setColumns, getColumnsRef, getCacheColumns } = useColumns(
getProps, getProps,
getPaginationInfo getPaginationInfo
) )
@ -292,7 +292,8 @@ const tableAction: TableActionType = {
scrollTo, scrollTo,
getSize: () => { getSize: () => {
return unref(getBindValues).size as SizeType return unref(getBindValues).size as SizeType
} },
setCacheColumns
} }
createTableContext({ ...tableAction, wrapRef, getBindValues }) createTableContext({ ...tableAction, wrapRef, getBindValues })

View File

@ -83,7 +83,7 @@
</Tooltip> </Tooltip>
</template> </template>
<script lang="ts" setup name="ColumnSetting"> <script lang="ts" setup name="ColumnSetting">
import type { BasicColumn, ColumnChangeParam } from '../../types/table' import type { BasicColumn, BasicTableProps, ColumnChangeParam } from '../../types/table'
import { ref, reactive, useAttrs, watchEffect, nextTick, unref, computed } from 'vue' import { ref, reactive, useAttrs, watchEffect, nextTick, unref, computed } from 'vue'
import { Tooltip, Popover, Checkbox, Divider } from 'ant-design-vue' import { Tooltip, Popover, Checkbox, Divider } from 'ant-design-vue'
import type { CheckboxChangeEvent } from 'ant-design-vue/lib/checkbox/interface' import type { CheckboxChangeEvent } from 'ant-design-vue/lib/checkbox/interface'
@ -122,6 +122,10 @@ const table = useTableContext()
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>([])
@ -136,6 +140,8 @@ const state = reactive<State>({
defaultCheckList: [] defaultCheckList: []
}) })
/** 缓存初始化props */
let cacheTableProps: Partial<BasicTableProps<any>> = {}
const checkIndex = ref(false) const checkIndex = ref(false)
const checkSelect = ref(false) const checkSelect = ref(false)
@ -148,7 +154,9 @@ const getValues = computed(() => {
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)
@ -156,6 +164,11 @@ watchEffect(() => {
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
}) })
@ -172,8 +185,17 @@ function getColumns() {
return ret return ret
} }
function init() { async function init(isReset = false) {
const columns = getColumns() // Sortablejsbugel appendchildNodechildNode
//
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 })
@ -185,30 +207,24 @@ function init() {
}) })
.filter(Boolean) as string[] .filter(Boolean) as string[]
if (!plainOptions.value.length) { plainOptions.value = columns
plainOptions.value = columns plainSortOptions.value = columns
plainSortOptions.value = columns //
cachePlainOptions.value = columns table.setCacheColumns?.(columns)
state.defaultCheckList = checkList !isReset && (cachePlainOptions.value = cloneDeep(columns))
} else { state.defaultCheckList = checkList
// const fixedColumns = columns.filter((item) => state.checkedList = checkList
// Reflect.has(item, 'fixed') //
// ) as BasicColumn[]; state.checkAll = checkList.length === columns.length
inited = false
unref(plainOptions).forEach((item: BasicColumn) => { handleVisibleChange()
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
} }
// 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)
@ -233,6 +249,9 @@ function onChange(checkedList: string[]) {
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)
} }
@ -240,11 +259,14 @@ let sortable: Sortable
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)
} }
@ -280,7 +302,7 @@ function handleVisibleChange() {
plainSortOptions.value = columns plainSortOptions.value = columns
setColumns(columns.map((col: Options) => col.value).filter((value: string) => state.checkedList.includes(value))) setColumns(columns.filter((item) => state.checkedList.includes(item.value)))
} }
}) })
// order // order
@ -291,6 +313,8 @@ function handleVisibleChange() {
// 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
}) })
@ -298,6 +322,8 @@ function handleIndexCheckChange(e: CheckboxChangeEvent) {
// 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
}) })
@ -317,11 +343,14 @@ function handleColumnFixed(item: BasicColumn, fixed?: 'left' | 'right') {
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 =
@ -335,6 +364,14 @@ function setColumns(columns: BasicColumn[] | string[]) {
function getPopupContainer() { function getPopupContainer() {
return isFunction(attrs.getPopupContainer) ? attrs.getPopupContainer() : getParentContainer() return isFunction(attrs.getPopupContainer) ? attrs.getPopupContainer() : getParentContainer()
} }
function updateSortOption(column: BasicColumn) {
plainSortOptions.value.forEach((item) => {
if (item.value === column.dataIndex) {
Object.assign(item, column)
}
})
}
</script> </script>
<style lang="less"> <style lang="less">
@prefix-cls: ~'@{namespace}-basic-column-setting'; @prefix-cls: ~'@{namespace}-basic-column-setting';

View File

@ -251,6 +251,10 @@ export function useColumns(propsRef: ComputedRef<BasicTableProps>, getPagination
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,
@ -258,7 +262,8 @@ export function useColumns(propsRef: ComputedRef<BasicTableProps>, getPagination
getColumns, getColumns,
setColumns, setColumns,
getViewColumns, getViewColumns,
setCacheColumnsByField setCacheColumnsByField,
setCacheColumns
} }
} }

View File

@ -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 {