|
1 | 1 | import { BaseChart, type ChartAxis, type ChartData } from '@/views/chat/component/BaseChart.ts' |
2 | 2 | import { |
3 | 3 | copyToClipboard, |
4 | | - type Node, |
5 | 4 | type S2DataConfig, |
6 | 5 | S2Event, |
7 | 6 | type S2MountContainer, |
@@ -71,106 +70,51 @@ export class Table extends BaseChart { |
71 | 70 | data: this.data, |
72 | 71 | } |
73 | 72 |
|
| 73 | + const sortState: Record<string, string> = {} |
| 74 | + |
| 75 | + const handleSortClick = (params: any) => { |
| 76 | + const { meta } = params |
| 77 | + const s2 = meta.spreadsheet |
| 78 | + if (s2 && meta.isLeaf) { |
| 79 | + const fieldId = meta.field |
| 80 | + const currentMethod = sortState[fieldId] || 'none' |
| 81 | + const sortOrder = ['none', 'desc', 'asc'] |
| 82 | + const nextMethod = sortOrder[(sortOrder.indexOf(currentMethod) + 1) % sortOrder.length] |
| 83 | + sortState[fieldId] = nextMethod |
| 84 | + s2.groupSortByMethod(nextMethod === 'none' ? 'none' : (nextMethod as SortMethod), meta) |
| 85 | + s2.render() |
| 86 | + } |
| 87 | + } |
| 88 | + |
74 | 89 | const s2Options: S2Options = { |
75 | 90 | width: 600, |
76 | 91 | height: 360, |
77 | | - showDefaultHeaderActionIcon: true, |
| 92 | + showDefaultHeaderActionIcon: false, |
| 93 | + headerActionIcons: [ |
| 94 | + { |
| 95 | + icons: ['GlobalDesc'], |
| 96 | + belongsCell: 'colCell', |
| 97 | + displayCondition: (node: any) => node.isLeaf && sortState[node.field] === 'desc', |
| 98 | + onClick: handleSortClick, |
| 99 | + }, |
| 100 | + { |
| 101 | + icons: ['GlobalAsc'], |
| 102 | + belongsCell: 'colCell', |
| 103 | + displayCondition: (node: any) => node.isLeaf && sortState[node.field] === 'asc', |
| 104 | + onClick: handleSortClick, |
| 105 | + }, |
| 106 | + { |
| 107 | + icons: ['SortDown'], |
| 108 | + belongsCell: 'colCell', |
| 109 | + displayCondition: (node: any) => |
| 110 | + node.isLeaf && (!sortState[node.field] || sortState[node.field] === 'none'), |
| 111 | + onClick: handleSortClick, |
| 112 | + }, |
| 113 | + ], |
78 | 114 | tooltip: { |
79 | 115 | operation: { |
80 | | - // 开启组内排序 |
81 | 116 | sort: true, |
82 | 117 | }, |
83 | | - dataCell: { |
84 | | - enable: true, |
85 | | - content: (cell) => { |
86 | | - const meta = cell.getMeta() |
87 | | - const container = document.createElement('div') |
88 | | - container.style.padding = '8px 0' |
89 | | - container.style.minWidth = '100px' |
90 | | - container.style.maxWidth = '400px' |
91 | | - container.style.display = 'flex' |
92 | | - container.style.alignItems = 'center' |
93 | | - container.style.padding = '8px 16px' |
94 | | - container.style.cursor = 'pointer' |
95 | | - container.style.color = '#606266' |
96 | | - container.style.fontSize = '14px' |
97 | | - container.style.whiteSpace = 'pre-wrap' |
98 | | - |
99 | | - const text = document.createTextNode(meta.fieldValue) |
100 | | - container.appendChild(text) |
101 | | - |
102 | | - return container |
103 | | - }, |
104 | | - }, |
105 | | - colCell: { |
106 | | - enable: true, |
107 | | - content: (cell) => { |
108 | | - const meta = cell.getMeta() |
109 | | - const { spreadsheet: s2 } = meta |
110 | | - if (!meta.isLeaf) { |
111 | | - return null |
112 | | - } |
113 | | - |
114 | | - // 创建类似Element Plus下拉菜单的结构 |
115 | | - const container = document.createElement('div') |
116 | | - container.className = 'el-dropdown' |
117 | | - container.style.padding = '8px 0' |
118 | | - container.style.minWidth = '100px' |
119 | | - |
120 | | - const menuItems = [ |
121 | | - { |
122 | | - label: t('chat.sort_desc'), |
123 | | - method: 'desc' as SortMethod, |
124 | | - icon: 'el-icon-sort-down', |
125 | | - }, |
126 | | - { label: t('chat.sort_asc'), method: 'asc' as SortMethod, icon: 'el-icon-sort-up' }, |
127 | | - { label: t('chat.sort_none'), method: 'none' as SortMethod, icon: 'el-icon-close' }, |
128 | | - ] |
129 | | - |
130 | | - menuItems.forEach((item) => { |
131 | | - const itemEl = document.createElement('div') |
132 | | - itemEl.className = 'el-dropdown-menu__item' |
133 | | - itemEl.style.display = 'flex' |
134 | | - itemEl.style.alignItems = 'center' |
135 | | - itemEl.style.padding = '8px 16px' |
136 | | - itemEl.style.cursor = 'pointer' |
137 | | - itemEl.style.color = '#606266' |
138 | | - itemEl.style.fontSize = '14px' |
139 | | - |
140 | | - // 鼠标悬停效果 |
141 | | - itemEl.addEventListener('mouseenter', () => { |
142 | | - itemEl.style.backgroundColor = '#f5f7fa' |
143 | | - itemEl.style.color = '#409eff' |
144 | | - }) |
145 | | - itemEl.addEventListener('mouseleave', () => { |
146 | | - itemEl.style.backgroundColor = 'transparent' |
147 | | - itemEl.style.color = '#606266' |
148 | | - }) |
149 | | - |
150 | | - // 添加图标(如果需要) |
151 | | - if (item.icon) { |
152 | | - const icon = document.createElement('i') |
153 | | - icon.className = item.icon |
154 | | - icon.style.marginRight = '8px' |
155 | | - icon.style.fontSize = '16px' |
156 | | - itemEl.appendChild(icon) |
157 | | - } |
158 | | - |
159 | | - const text = document.createTextNode(item.label) |
160 | | - itemEl.appendChild(text) |
161 | | - |
162 | | - itemEl.addEventListener('click', (e) => { |
163 | | - e.stopPropagation() |
164 | | - s2.groupSortByMethod(item.method, meta as Node) |
165 | | - // 可以在这里添加关闭tooltip的逻辑 |
166 | | - }) |
167 | | - |
168 | | - container.appendChild(itemEl) |
169 | | - }) |
170 | | - |
171 | | - return container |
172 | | - }, |
173 | | - }, |
174 | 118 | }, |
175 | 119 | // 如果有省略号, 复制到的是完整文本 |
176 | 120 | interaction: { |
|
0 commit comments