yanjinhui
10 天以前 c5de0d98241f8c8349fa38851b77efcfc61e4d26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<script setup lang="ts">
import {
  ElDrawer,
  ElCheckbox,
  ElCheckboxGroup,
  ElText,
  ElRadioButton,
  ElRadioGroup
} from 'element-plus'
import { TableColumn } from '../types'
import { PropType, ref, watch, unref } from 'vue'
import { cloneDeep } from 'lodash-es'
import { DEFAULT_FILTER_COLUMN } from '@/constants'
import { VueDraggable } from 'vue-draggable-plus'
 
const modelValue = defineModel<boolean>()
 
const props = defineProps({
  columns: {
    type: Array as PropType<TableColumn[]>,
    default: () => []
  }
})
 
const emit = defineEmits(['confirm'])
 
const oldColumns = ref<TableColumn[]>()
 
const settingColumns = ref<TableColumn[]>()
 
// 存储不要的列
const hiddenColumns = ref<TableColumn[]>([])
 
const defaultCheckColumns = ref<string[]>([])
const checkColumns = ref<string[]>([])
 
const checkAll = ref(false)
const isIndeterminate = ref(true)
const handleCheckAllChange = (val: boolean) => {
  checkColumns.value = val ? unref(defaultCheckColumns) : []
  isIndeterminate.value = false
}
 
const handleCheckedColumnsChange = (value: string[]) => {
  const checkedCount = value.length
  checkAll.value = checkedCount === unref(defaultCheckColumns)?.length
  isIndeterminate.value = checkedCount > 0 && checkedCount < unref(defaultCheckColumns)?.length
}
 
const confirm = () => {
  const newColumns = cloneDeep(unref(settingColumns))?.map((item) => {
    const fixed = unref(settingColumns)?.find((col) => col.field === item.field)?.fixed
    item.hidden = !unref(checkColumns)?.includes(item.field)
    item.fixed = fixed ? fixed : false
    return item
  })
  emit('confirm', [...unref(hiddenColumns), ...(newColumns || [])])
  modelValue.value = false
}
 
const restore = () => {
  initColumns([...unref(hiddenColumns), ...(unref(oldColumns) || [])], true)
}
 
const initColumns = (columns: TableColumn[], isReStore = false) => {
  const newColumns = columns?.filter((item) => {
    if (!isReStore) {
      item.fixed = item.fixed !== void 0 ? item.fixed : false
    }
    return (item.type && !DEFAULT_FILTER_COLUMN.includes(item.type)) || !item.type
  })
  if (!unref(oldColumns)?.length) {
    oldColumns.value = cloneDeep(newColumns)
  }
  settingColumns.value = cloneDeep(newColumns)
 
  hiddenColumns.value = cloneDeep(
    columns?.filter((item) => item.type && DEFAULT_FILTER_COLUMN.includes(item.type))
  )
 
  defaultCheckColumns.value = unref(settingColumns)?.map((item) => item.field) || []
  checkColumns.value =
    unref(settingColumns)
      ?.filter((item) => !item.hidden)
      ?.map((item) => item.field) || []
 
  if (unref(checkColumns)?.length === unref(defaultCheckColumns)?.length) {
    checkAll.value = true
    isIndeterminate.value = false
  }
}
 
watch(
  () => props.columns,
  (columns) => {
    initColumns(columns)
  },
  {
    immediate: true,
    deep: true
  }
)
</script>
 
<template>
  <ElDrawer v-model="modelValue" title="列设置" size="350px">
    <div>
      <div class="flex items-center justify-between">
        <div class="flex items-center justify-between">
          <ElCheckbox
            v-model="checkAll"
            :indeterminate="isIndeterminate"
            @change="handleCheckAllChange"
          />
          <ElText class="ml-8px!">{{ checkColumns.length }} / {{ settingColumns?.length }}</ElText>
        </div>
        <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 :value="item.field">
                {{ item.label }}
              </ElCheckbox>
              <div class="flex items-center">
                <ElRadioGroup size="small" v-model="item.fixed">
                  <ElRadioButton value="left">
                    <Icon icon="vi-ep:arrow-left" />
                  </ElRadioButton>
                  <ElRadioButton :value="false">
                    <Icon icon="vi-ep:close" />
                  </ElRadioButton>
                  <ElRadioButton value="right">
                    <Icon icon="vi-ep:arrow-right" />
                  </ElRadioButton>
                </ElRadioGroup>
 
                <div class="ml-12px cursor-move handle"><Icon icon="vi-ep:rank" /></div>
              </div>
            </div>
          </ElCheckboxGroup>
        </VueDraggable>
      </div>
    </div>
    <template #footer>
      <div>
        <BaseButton @click="restore">还原</BaseButton>
        <BaseButton type="primary" @click="confirm">确定</BaseButton>
      </div>
    </template>
  </ElDrawer>
</template>