Skip to content

Commit 890219c

Browse files
committed
feat: support table sort
1 parent 1819d95 commit 890219c

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

frontend/src/views/chat/chat-block/ChartBlock.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ watch(
618618
padding: 16px;
619619
display: flex;
620620
flex-direction: column;
621+
position: relative;
622+
z-index: 10;
621623
622624
border: 1px solid rgba(222, 224, 227, 1);
623625
border-radius: 12px;

frontend/src/views/chat/component/charts/Table.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
type S2Options,
77
type S2DataConfig,
88
type S2MountContainer,
9+
type SortMethod,
10+
type Node,
911
} from '@antv/s2'
1012
import { debounce, filter } from 'lodash-es'
1113
import { i18n } from '@/i18n'
@@ -49,6 +51,12 @@ export class Table extends BaseChart {
4951
)
5052

5153
const s2DataConfig: S2DataConfig = {
54+
sortParams:
55+
this.axis?.map((a) => {
56+
return {
57+
sortFieldId: a.value,
58+
}
59+
}) ?? [],
5260
fields: {
5361
columns: this.axis?.map((a) => a.value) ?? [],
5462
},
@@ -65,6 +73,79 @@ export class Table extends BaseChart {
6573
const s2Options: S2Options = {
6674
width: 600,
6775
height: 360,
76+
showDefaultHeaderActionIcon: true,
77+
tooltip: {
78+
enable: true,
79+
operation: {
80+
// 开启组内排序
81+
sort: true,
82+
},
83+
colCell: {
84+
content: (cell) => {
85+
const meta = cell.getMeta()
86+
const { spreadsheet: s2 } = meta
87+
88+
if (!meta.isLeaf) {
89+
return null
90+
}
91+
92+
// 创建类似Element Plus下拉菜单的结构
93+
const container = document.createElement('div')
94+
container.className = 'el-dropdown'
95+
container.style.padding = '8px 0'
96+
container.style.minWidth = '100px'
97+
98+
const menuItems = [
99+
{ label: '降序', method: 'desc' as SortMethod, icon: 'el-icon-sort-down' },
100+
{ label: '升序', method: 'asc' as SortMethod, icon: 'el-icon-sort-up' },
101+
{ label: '不排序', method: 'none' as SortMethod, icon: 'el-icon-close' },
102+
]
103+
104+
menuItems.forEach((item) => {
105+
const itemEl = document.createElement('div')
106+
itemEl.className = 'el-dropdown-menu__item'
107+
itemEl.style.display = 'flex'
108+
itemEl.style.alignItems = 'center'
109+
itemEl.style.padding = '8px 16px'
110+
itemEl.style.cursor = 'pointer'
111+
itemEl.style.color = '#606266'
112+
itemEl.style.fontSize = '14px'
113+
114+
// 鼠标悬停效果
115+
itemEl.addEventListener('mouseenter', () => {
116+
itemEl.style.backgroundColor = '#f5f7fa'
117+
itemEl.style.color = '#409eff'
118+
})
119+
itemEl.addEventListener('mouseleave', () => {
120+
itemEl.style.backgroundColor = 'transparent'
121+
itemEl.style.color = '#606266'
122+
})
123+
124+
// 添加图标(如果需要)
125+
if (item.icon) {
126+
const icon = document.createElement('i')
127+
icon.className = item.icon
128+
icon.style.marginRight = '8px'
129+
icon.style.fontSize = '16px'
130+
itemEl.appendChild(icon)
131+
}
132+
133+
const text = document.createTextNode(item.label)
134+
itemEl.appendChild(text)
135+
136+
itemEl.addEventListener('click', (e) => {
137+
e.stopPropagation()
138+
s2.groupSortByMethod(item.method, meta as Node)
139+
// 可以在这里添加关闭tooltip的逻辑
140+
})
141+
142+
container.appendChild(itemEl)
143+
})
144+
145+
return container
146+
},
147+
},
148+
},
68149
placeholder: {
69150
cell: '-',
70151
empty: {
@@ -76,6 +157,7 @@ export class Table extends BaseChart {
76157

77158
if (this.container) {
78159
this.table = new TableSheet(this.container, s2DataConfig, s2Options)
160+
console.log(this.table)
79161
// right click
80162
this.table.on(S2Event.GLOBAL_COPIED, (data) => {
81163
ElMessage.success(t('qa.copied'))
@@ -85,6 +167,9 @@ export class Table extends BaseChart {
85167
event.preventDefault()
86168
})
87169
this.table.on(S2Event.GLOBAL_CONTEXT_MENU, (event) => copyData(event, this.table))
170+
// this.table.on(S2Event.RANGE_SORT, (sortParams) => {
171+
// console.log('sortParams:', sortParams)
172+
// })
88173
}
89174
}
90175

0 commit comments

Comments
 (0)