DynamicColumnDialog.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <template>
  2. <cacp-dialog
  3. v-model="dialogVisible"
  4. :title="title"
  5. :width="width"
  6. :resizable="false"
  7. @closed="onDialogClosed"
  8. >
  9. <div class="dynamic-column-dialog">
  10. <!-- 搜索框 -->
  11. <div class="column-search" v-if="showSearch">
  12. <el-input
  13. v-model="searchText"
  14. placeholder="搜索列名"
  15. clearable
  16. :prefix-icon="Search"
  17. @keyup.enter="handleSearch"
  18. />
  19. </div>
  20. <!-- 操作按钮 -->
  21. <div class="column-operations">
  22. <el-button type="text" @click="selectAll" :disabled="!columns.length">全选</el-button>
  23. <el-button type="text" @click="clearAll" :disabled="!tempSelectedKeys.length">清空</el-button>
  24. <el-button type="text" @click="resetToDefault">恢复默认</el-button>
  25. <el-button type="text" @click="toggleSelectAll" v-if="showToggleAll">
  26. {{ isAllSelected ? '取消全选' : '全选当前' }}
  27. </el-button>
  28. </div>
  29. <!-- 列选择区域 -->
  30. <div class="column-select-area">
  31. <el-checkbox-group v-model="tempSelectedKeysModel">
  32. <div
  33. v-for="group in filteredColumnGroups"
  34. :key="group.key"
  35. class="column-group"
  36. >
  37. <div class="group-header" v-if="group.title && group.columns.length > 0">
  38. <el-checkbox
  39. :indeterminate="group.indeterminate"
  40. :model-value="group.allChecked"
  41. @change="toggleGroup(group.key, $event)"
  42. >
  43. <span class="group-title">{{ group.title }}</span>
  44. <!-- <span class="group-count">({{ group.selectedCount }}/{{ group.columns.length }})</span>-->
  45. </el-checkbox>
  46. </div>
  47. <div class="group-items" v-if="group.columns.length > 0">
  48. <el-checkbox
  49. v-for="column in group.columns"
  50. :key="column.key"
  51. :label="column.key"
  52. :title="column.tooltip || column.label"
  53. :disabled="column.disabled"
  54. >
  55. <span class="column-label">{{ column.label }}</span>
  56. <!-- <span v-if="column.width" class="column-width">({{ column.width }})</span>-->
  57. <el-tag v-if="column.defaultVisible" size="small" type="success" class="default-tag">
  58. 默认
  59. </el-tag>
  60. </el-checkbox>
  61. </div>
  62. </div>
  63. </el-checkbox-group>
  64. <!-- 无数据提示 -->
  65. <div v-if="!hasFilteredColumns" class="no-data">
  66. <el-empty description="未找到匹配的列" :image-size="80" />
  67. </div>
  68. </div>
  69. <!-- 底部统计信息 -->
  70. <div class="column-statistics">
  71. 已选择 {{ tempSelectedKeys.length }}/{{ columns.length }} 列
  72. </div>
  73. </div>
  74. <!-- 底部按钮 -->
  75. <template #footer>
  76. <span class="dialog-footer">
  77. <el-button @click="handleCancel">取消</el-button>
  78. <el-button type="primary" @click="handleConfirm" :disabled="!tempSelectedKeys.length">
  79. 确定
  80. </el-button>
  81. </span>
  82. </template>
  83. </cacp-dialog>
  84. </template>
  85. <script setup lang="ts">
  86. import { computed, ref, watch, nextTick } from 'vue'
  87. import { Search } from '@element-plus/icons-vue'
  88. import { type CheckboxValueType, ElMessage } from 'element-plus'
  89. export interface ColumnConfig {
  90. key: string
  91. label: string
  92. width?: string | number
  93. tooltip?: string
  94. groupKey?: string
  95. groupTitle?: string
  96. defaultVisible?: boolean
  97. property?: string
  98. sortable?: boolean
  99. isDict?: boolean
  100. dictKey?: string
  101. isDate?: boolean
  102. formatter?: (value: any) => string
  103. showOverflowTooltip?: boolean
  104. disabled?: boolean
  105. }
  106. export interface ColumnGroup {
  107. key: string
  108. title?: string
  109. columns: ColumnConfig[]
  110. indeterminate: boolean
  111. allChecked: boolean
  112. selectedCount: number
  113. }
  114. interface Props {
  115. modelValue: boolean
  116. title?: string
  117. width?: string | number
  118. columns: ColumnConfig[]
  119. selectedKeys: string[]
  120. showSearch?: boolean
  121. showToggleAll?: boolean
  122. autoSave?: boolean
  123. }
  124. interface Emits {
  125. (e: 'update:modelValue', value: boolean): void
  126. (e: 'update:selectedKeys', keys: string[]): void
  127. (e: 'confirm', keys: string[]): void
  128. (e: 'cancel'): void
  129. }
  130. const props = withDefaults(defineProps<Props>(), {
  131. title: '选择显示的列',
  132. width: '600px',
  133. showSearch: true,
  134. showToggleAll: false,
  135. autoSave: false
  136. })
  137. const emit = defineEmits<Emits>()
  138. // 搜索文本
  139. const searchText = ref('')
  140. // 临时选中的列键值
  141. const tempSelectedKeys = ref<string[]>([])
  142. // 使用 computed 包装用于 v-model
  143. const tempSelectedKeysModel = computed({
  144. get: () => tempSelectedKeys.value,
  145. set: (value: string[]) => {
  146. tempSelectedKeys.value = value
  147. }
  148. })
  149. // 对话框显示状态
  150. const dialogVisible = computed({
  151. get: () => props.modelValue,
  152. set: (value) => emit('update:modelValue', value)
  153. })
  154. // 计算是否全选
  155. const isAllSelected = computed(() =>
  156. tempSelectedKeys.value.length === props.columns.length && props.columns.length > 0
  157. )
  158. // 是否有过滤后的列
  159. const hasFilteredColumns = computed(() => {
  160. return filteredColumnGroups.value.some(group => group.columns.length > 0)
  161. })
  162. // 分组后的列配置
  163. const columnGroups = computed(() => {
  164. const groups = new Map<string, ColumnGroup>()
  165. // 初始化分组
  166. props.columns.forEach(column => {
  167. const groupKey = column.groupKey || 'default'
  168. if (!groups.has(groupKey)) {
  169. groups.set(groupKey, {
  170. key: groupKey,
  171. title: column.groupTitle || '',
  172. columns: [],
  173. indeterminate: false,
  174. allChecked: false,
  175. selectedCount: 0
  176. })
  177. }
  178. groups.get(groupKey)!.columns.push(column)
  179. })
  180. // 计算每个分组的选中状态
  181. groups.forEach(group => {
  182. const selectedCount = group.columns.filter(col =>
  183. tempSelectedKeys.value.includes(col.key)
  184. ).length
  185. group.selectedCount = selectedCount
  186. group.allChecked = selectedCount === group.columns.length
  187. group.indeterminate = selectedCount > 0 && selectedCount < group.columns.length
  188. })
  189. return Array.from(groups.values())
  190. })
  191. // 过滤后的列分组(根据搜索文本)
  192. const filteredColumnGroups = computed(() => {
  193. if (!searchText.value.trim()) return columnGroups.value
  194. const searchLower = searchText.value.toLowerCase().trim()
  195. return columnGroups.value.map(group => ({
  196. ...group,
  197. columns: group.columns.filter(column =>
  198. column.label.toLowerCase().includes(searchLower) ||
  199. column.key.toLowerCase().includes(searchLower) ||
  200. (column.tooltip && column.tooltip.toLowerCase().includes(searchLower))
  201. )
  202. })).filter(group => group.columns.length > 0)
  203. })
  204. // 初始化临时选中的列
  205. watch(
  206. () => props.selectedKeys,
  207. (newVal) => {
  208. tempSelectedKeys.value = [...newVal]
  209. },
  210. { immediate: true }
  211. )
  212. // 监视对话框打开,重置搜索
  213. watch(
  214. () => props.modelValue,
  215. (newVal) => {
  216. if (newVal) {
  217. nextTick(() => {
  218. searchText.value = ''
  219. })
  220. }
  221. }
  222. )
  223. // 全选
  224. const selectAll = () => {
  225. tempSelectedKeys.value = props.columns.map(col => col.key)
  226. }
  227. // 清空
  228. const clearAll = () => {
  229. tempSelectedKeys.value = []
  230. }
  231. // 恢复默认
  232. const resetToDefault = () => {
  233. const defaultKeys = props.columns
  234. .filter(col => col.defaultVisible)
  235. .map(col => col.key)
  236. tempSelectedKeys.value = defaultKeys
  237. if (props.autoSave) {
  238. emit('update:selectedKeys', defaultKeys)
  239. }
  240. }
  241. // 切换全选/取消全选
  242. const toggleSelectAll = () => {
  243. if (isAllSelected.value) {
  244. clearAll()
  245. } else {
  246. selectAll()
  247. }
  248. }
  249. // 切换分组选中状态
  250. const toggleGroup = (groupKey: string, checked: CheckboxValueType) => {
  251. const group = columnGroups.value.find(g => g.key === groupKey)
  252. if (!group) return
  253. const groupColumnKeys = group.columns.map(col => col.key)
  254. // 将 CheckboxValueType 转换为 boolean
  255. const isChecked = !!checked
  256. if (isChecked) {
  257. // 添加分组中所有列
  258. const newKeys = [...tempSelectedKeys.value]
  259. groupColumnKeys.forEach(key => {
  260. if (!newKeys.includes(key)) {
  261. newKeys.push(key)
  262. }
  263. })
  264. tempSelectedKeys.value = newKeys
  265. } else {
  266. // 移除分组中所有列
  267. tempSelectedKeys.value = tempSelectedKeys.value.filter(
  268. key => !groupColumnKeys.includes(key)
  269. )
  270. }
  271. }
  272. // 处理搜索
  273. const handleSearch = () => {
  274. // 搜索逻辑已经在 filteredColumnGroups 中实现
  275. }
  276. // 对话框关闭
  277. const onDialogClosed = () => {
  278. searchText.value = ''
  279. }
  280. // 取消
  281. const handleCancel = () => {
  282. // 恢复原始选中状态
  283. tempSelectedKeys.value = [...props.selectedKeys]
  284. emit('cancel')
  285. dialogVisible.value = false
  286. }
  287. // 确认
  288. const handleConfirm = () => {
  289. if (tempSelectedKeys.value.length === 0) {
  290. ElMessage.warning('请至少选择一列')
  291. return
  292. }
  293. emit('update:selectedKeys', [...tempSelectedKeys.value])
  294. emit('confirm', [...tempSelectedKeys.value])
  295. dialogVisible.value = false
  296. }
  297. // 键盘快捷键
  298. const handleKeydown = (e: KeyboardEvent) => {
  299. if (e.key === 'Escape') {
  300. handleCancel()
  301. } else if (e.key === 'Enter' && e.ctrlKey) {
  302. handleConfirm()
  303. }
  304. }
  305. // 添加键盘事件监听
  306. if (typeof window !== 'undefined') {
  307. window.addEventListener('keydown', handleKeydown)
  308. }
  309. </script>
  310. <style scoped lang="scss">
  311. .dynamic-column-dialog {
  312. display: flex;
  313. flex-direction: column;
  314. //height: 100%;
  315. max-height: 60vh;
  316. overflow-y: auto;
  317. padding-right: 10px;
  318. .column-search {
  319. margin-bottom: 16px;
  320. :deep(.el-input__wrapper) {
  321. border-radius: 6px;
  322. }
  323. }
  324. .column-operations {
  325. display: flex;
  326. flex-wrap: wrap;
  327. gap: 12px;
  328. margin-bottom: 16px;
  329. padding-bottom: 12px;
  330. border-bottom: 1px solid var(--el-border-color-light);
  331. .el-button {
  332. padding: 0;
  333. height: auto;
  334. font-size: 14px;
  335. &:disabled {
  336. opacity: 0.5;
  337. cursor: not-allowed;
  338. }
  339. }
  340. }
  341. .column-select-area {
  342. flex: 1;
  343. overflow-y: auto;
  344. padding-right: 4px;
  345. .column-group {
  346. margin-bottom: 20px;
  347. &:last-child {
  348. margin-bottom: 0;
  349. }
  350. .group-header {
  351. margin-bottom: 12px;
  352. padding: 8px 0;
  353. background-color: var(--el-fill-color-light);
  354. border-radius: 6px;
  355. .el-checkbox {
  356. width: 100%;
  357. padding: 0 12px;
  358. :deep(.el-checkbox__label) {
  359. display: flex;
  360. align-items: center;
  361. justify-content: space-between;
  362. width: 100%;
  363. }
  364. .group-title {
  365. font-weight: 600;
  366. color: var(--el-text-color-primary);
  367. }
  368. .group-count {
  369. color: var(--el-text-color-secondary);
  370. font-size: 12px;
  371. font-weight: normal;
  372. }
  373. }
  374. }
  375. .group-items {
  376. display: grid;
  377. grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  378. gap: 12px;
  379. padding: 0 4px;
  380. .el-checkbox {
  381. margin: 0;
  382. padding: 8px 12px;
  383. border: 1px solid var(--el-border-color-light);
  384. border-radius: 6px;
  385. transition: all 0.3s;
  386. &:hover {
  387. border-color: var(--el-color-primary);
  388. background-color: var(--el-color-primary-light-9);
  389. }
  390. &:deep(.el-checkbox__label) {
  391. display: flex;
  392. align-items: center;
  393. flex: 1;
  394. min-width: 0;
  395. }
  396. .column-label {
  397. flex: 1;
  398. margin-right: 8px;
  399. overflow: hidden;
  400. text-overflow: ellipsis;
  401. white-space: nowrap;
  402. }
  403. .column-width {
  404. color: var(--el-text-color-secondary);
  405. font-size: 12px;
  406. flex-shrink: 0;
  407. }
  408. .default-tag {
  409. margin-left: 8px;
  410. flex-shrink: 0;
  411. }
  412. }
  413. }
  414. }
  415. .no-data {
  416. display: flex;
  417. justify-content: center;
  418. align-items: center;
  419. height: 200px;
  420. }
  421. }
  422. .column-statistics {
  423. margin-top: 16px;
  424. padding-top: 12px;
  425. border-top: 1px solid var(--el-border-color-light);
  426. font-size: 14px;
  427. color: var(--el-text-color-secondary);
  428. text-align: center;
  429. }
  430. }
  431. .dialog-footer {
  432. display: flex;
  433. justify-content: flex-end;
  434. gap: 12px;
  435. }
  436. </style>