refactor: 新增列设置

This commit is contained in:
kailong321200875 2023-12-19 19:57:45 +08:00
parent 47c9b0bd4d
commit 7314065c90
2 changed files with 61 additions and 46 deletions

View File

@ -11,6 +11,7 @@ import { TableColumn } from '../types'
import { PropType, ref, watch, unref } from 'vue' import { PropType, ref, watch, unref } from 'vue'
import { cloneDeep } from 'lodash-es' import { cloneDeep } from 'lodash-es'
import { DEFAULT_FILTER_COLUMN } from '@/constants' import { DEFAULT_FILTER_COLUMN } from '@/constants'
import { VueDraggable } from 'vue-draggable-plus'
const modelValue = defineModel<boolean>() const modelValue = defineModel<boolean>()
@ -50,7 +51,7 @@ const confirm = () => {
const newColumns = cloneDeep(unref(settingColumns))?.map((item) => { const newColumns = cloneDeep(unref(settingColumns))?.map((item) => {
const fixed = unref(settingColumns)?.find((col) => col.field === item.field)?.fixed const fixed = unref(settingColumns)?.find((col) => col.field === item.field)?.fixed
item.hidden = !!!unref(checkColumns)?.includes(item.field) item.hidden = !!!unref(checkColumns)?.includes(item.field)
item.fixed = fixed !== void 0 ? fixed : undefined item.fixed = fixed ? fixed : undefined
return item return item
}) })
emit('confirm', [...unref(hiddenColumns), ...(newColumns || [])]) emit('confirm', [...unref(hiddenColumns), ...(newColumns || [])])
@ -58,21 +59,20 @@ const confirm = () => {
} }
const restore = () => { const restore = () => {
initColumns([...unref(hiddenColumns), ...(unref(oldColumns) || [])]) initColumns([...unref(hiddenColumns), ...(unref(oldColumns) || [])], true)
} }
const initColumns = (columns: TableColumn[]) => { const initColumns = (columns: TableColumn[], isReStore = false) => {
const newColumns = cloneDeep( const newColumns = columns?.filter((item) => {
columns?.filter((item) => { if (!isReStore) {
item.fixed = item.fixed !== void 0 ? item.fixed : undefined item.fixed = item.fixed !== void 0 ? item.fixed : undefined
return (item.type && !DEFAULT_FILTER_COLUMN.includes(item.type)) || !item.type }
}) return (item.type && !DEFAULT_FILTER_COLUMN.includes(item.type)) || !item.type
) })
console.log(newColumns)
if (!unref(oldColumns)) { if (!unref(oldColumns)) {
oldColumns.value = newColumns oldColumns.value = cloneDeep(newColumns)
} }
settingColumns.value = newColumns settingColumns.value = cloneDeep(newColumns)
hiddenColumns.value = cloneDeep( hiddenColumns.value = cloneDeep(
columns?.filter((item) => item.type && DEFAULT_FILTER_COLUMN.includes(item.type)) columns?.filter((item) => item.type && DEFAULT_FILTER_COLUMN.includes(item.type))
@ -93,7 +93,6 @@ const initColumns = (columns: TableColumn[]) => {
watch( watch(
() => props.columns, () => props.columns,
(columns) => { (columns) => {
console.log(columns)
initColumns(columns) initColumns(columns)
}, },
{ {
@ -104,40 +103,57 @@ watch(
<template> <template>
<ElDrawer v-model="modelValue" title="列设置" size="350px"> <ElDrawer v-model="modelValue" title="列设置" size="350px">
<div class="flex items-center justify-between"> <div>
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
<ElCheckbox <div class="flex items-center justify-between">
v-model="checkAll" <ElCheckbox
:indeterminate="isIndeterminate" v-model="checkAll"
@change="handleCheckAllChange" :indeterminate="isIndeterminate"
/> @change="handleCheckAllChange"
<ElText class="ml-8px!">{{ checkColumns.length }} / {{ settingColumns?.length }}</ElText> />
</div> <ElText class="ml-8px!">{{ checkColumns.length }} / {{ settingColumns?.length }}</ElText>
<ElText>固定 / 排序</ElText>
</div>
<div v-if="settingColumns?.length">
<ElCheckboxGroup v-model="checkColumns" @change="handleCheckedColumnsChange">
<div
v-for="item in settingColumns"
:key="item.field"
class="flex items-center justify-between mt-12px"
>
<ElCheckbox :label="item.field">
{{ item.label }}
</ElCheckbox>
<ElRadioGroup size="small" v-model="item.fixed">
<ElRadioButton label="left">
<Icon icon="ep:arrow-left" />
</ElRadioButton>
<ElRadioButton :label="undefined">
<Icon icon="ep:close" />
</ElRadioButton>
<ElRadioButton label="right">
<Icon icon="ep:arrow-right" />
</ElRadioButton>
</ElRadioGroup>
</div> </div>
</ElCheckboxGroup> <ElText>固定 / 排序</ElText>
</div>
<div v-if="settingColumns?.length">
<VueDraggable
v-model="settingColumns"
target=".el-checkbox-group"
handle=".handle"
:animation="150"
>
<ElCheckboxGroup
ref="draggableWrap"
v-model="checkColumns"
@change="handleCheckedColumnsChange"
>
<div
v-for="item in settingColumns"
:key="item.field"
class="flex items-center justify-between mt-12px"
>
<ElCheckbox :label="item.field">
{{ item.label }}
</ElCheckbox>
<div class="flex items-center">
<ElRadioGroup size="small" v-model="item.fixed">
<ElRadioButton label="left">
<Icon icon="ep:arrow-left" />
</ElRadioButton>
<ElRadioButton :label="undefined">
<Icon icon="ep:close" />
</ElRadioButton>
<ElRadioButton label="right">
<Icon icon="ep:arrow-right" />
</ElRadioButton>
</ElRadioGroup>
<div class="ml-12px cursor-move handle"><Icon icon="ep:rank" /></div>
</div>
</div>
</ElCheckboxGroup>
</VueDraggable>
</div>
</div> </div>
<template #footer> <template #footer>
<div> <div>

View File

@ -56,8 +56,7 @@ const columns = reactive<TableColumn[]>([
{ {
field: 'index', field: 'index',
label: t('tableDemo.index'), label: t('tableDemo.index'),
type: 'index', type: 'index'
hidden: true
}, },
{ {
field: 'title', field: 'title',