-
Notifications
You must be signed in to change notification settings - Fork 1k
/
header.js
380 lines (370 loc) · 13.5 KB
/
header.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import XEUtils from 'xe-utils/methods/xe-utils'
import { UtilTools, DomTools } from '../../tools'
const getAllColumns = (columns) => {
const result = []
columns.forEach((column) => {
if (column.visible) {
if (column.children && column.children.length && column.children.some(column => column.visible)) {
result.push(column)
result.push.apply(result, getAllColumns(column.children))
} else {
result.push(column)
}
}
})
return result
}
const convertToRows = (originColumns) => {
let maxLevel = 1
const traverse = (column, parent) => {
if (parent) {
column.level = parent.level + 1
if (maxLevel < column.level) {
maxLevel = column.level
}
}
if (column.children && column.children.length && column.children.some(column => column.visible)) {
let colSpan = 0
column.children.forEach((subColumn) => {
if (subColumn.visible) {
traverse(subColumn, column)
colSpan += subColumn.colSpan
}
})
column.colSpan = colSpan
} else {
column.colSpan = 1
}
}
originColumns.forEach((column) => {
column.level = 1
traverse(column)
})
const rows = []
for (let i = 0; i < maxLevel; i++) {
rows.push([])
}
const allColumns = getAllColumns(originColumns)
allColumns.forEach((column) => {
if (column.children && column.children.length && column.children.some(column => column.visible)) {
column.rowSpan = 1
} else {
column.rowSpan = maxLevel - column.level + 1
}
rows[column.level - 1].push(column)
})
return rows
}
export default {
name: 'VxeTableHeader',
props: {
tableData: Array,
tableColumn: Array,
visibleColumn: Array,
collectColumn: Array,
fixedColumn: Array,
size: String,
fixedType: String,
isGroup: Boolean
},
data () {
return {
headerColumn: []
}
},
watch: {
tableColumn () {
this.uploadColumn()
}
},
created () {
this.uploadColumn()
},
mounted () {
let { $parent: $xetable, $el, $refs, fixedType } = this
let { elemStore } = $xetable
let prefix = `${fixedType || 'main'}-header-`
elemStore[`${prefix}wrapper`] = $el
elemStore[`${prefix}table`] = $refs.table
elemStore[`${prefix}colgroup`] = $refs.colgroup
elemStore[`${prefix}list`] = $refs.thead
elemStore[`${prefix}xSpace`] = $refs.xSpace
elemStore[`${prefix}repair`] = $refs.repair
},
render (h) {
let {
_e,
$parent: $xetable,
fixedType,
headerColumn,
tableColumn,
fixedColumn
} = this
let {
$listeners: tableListeners,
id,
resizable,
border,
columnKey,
headerRowClassName,
headerCellClassName,
headerRowStyle,
headerCellStyle,
showHeaderOverflow: allColumnHeaderOverflow,
headerAlign: allHeaderAlign,
align: allAlign,
highlightCurrentColumn,
mouseConfig,
mouseOpts,
scrollXLoad,
overflowX,
scrollbarWidth,
getColumnIndex,
sortOpts
} = $xetable
let isMouseSelected = mouseConfig && mouseOpts.selected
// 在 v3.0 中废弃 mouse-config.checked
let isMouseChecked = mouseConfig && (mouseOpts.range || mouseOpts.checked)
// 横向滚动渲染
if (scrollXLoad) {
if (fixedType) {
tableColumn = fixedColumn
}
}
return h('div', {
class: ['vxe-table--header-wrapper', fixedType ? `fixed-${fixedType}--wrapper` : 'body--wrapper'],
attrs: {
'data-tid': id
}
}, [
fixedType ? _e() : h('div', {
class: 'vxe-body--x-space',
ref: 'xSpace'
}),
h('table', {
class: 'vxe-table--header',
attrs: {
'data-tid': id,
cellspacing: 0,
cellpadding: 0,
border: 0
},
ref: 'table'
}, [
/**
* 列宽
*/
h('colgroup', {
ref: 'colgroup'
}, tableColumn.map((column, columnIndex) => {
let isColGroup = column.children && column.children.length
return h('col', {
attrs: {
name: column.id
},
key: columnKey || isColGroup ? column.id : columnIndex
})
}).concat(scrollbarWidth ? [
h('col', {
attrs: {
name: 'col_gutter'
}
})
] : [])),
/**
* 头部
*/
h('thead', {
ref: 'thead'
}, headerColumn.map((cols, $rowIndex) => {
return h('tr', {
class: ['vxe-header--row', headerRowClassName ? XEUtils.isFunction(headerRowClassName) ? headerRowClassName({ $table: $xetable, $rowIndex, fixed: fixedType }) : headerRowClassName : ''],
style: headerRowStyle ? (XEUtils.isFunction(headerRowStyle) ? headerRowStyle({ $table: $xetable, $rowIndex, fixed: fixedType }) : headerRowStyle) : null
}, cols.map((column, $columnIndex) => {
let { showHeaderOverflow, headerAlign, align, headerClassName } = column
let isColGroup = column.children && column.children.length
let fixedHiddenColumn = fixedType ? column.fixed !== fixedType && !isColGroup : column.fixed && overflowX
let headOverflow = XEUtils.isUndefined(showHeaderOverflow) || XEUtils.isNull(showHeaderOverflow) ? allColumnHeaderOverflow : showHeaderOverflow
let headAlign = headerAlign || align || allHeaderAlign || allAlign
let showEllipsis = headOverflow === 'ellipsis'
let showTitle = headOverflow === 'title'
let showTooltip = headOverflow === true || headOverflow === 'tooltip'
let hasEllipsis = showTitle || showTooltip || showEllipsis
let thOns = {}
let hasFilter = column.filters && column.filters.some(item => item.checked)
// 确保任何情况下 columnIndex 都精准指向真实列索引
let columnIndex = getColumnIndex(column)
let params = { $table: $xetable, $rowIndex, column, columnIndex, $columnIndex, fixed: fixedType, isHidden: fixedHiddenColumn, hasFilter }
// 虚拟滚动不支持动态高度
if (scrollXLoad && !hasEllipsis) {
showEllipsis = hasEllipsis = true
}
if (showTitle || showTooltip) {
thOns.mouseenter = evnt => {
if ($xetable._isResize) {
return
}
if (showTitle) {
DomTools.updateCellTitle(evnt)
} else if (showTooltip) {
$xetable.triggerHeaderTooltipEvent(evnt, { $table: $xetable, $rowIndex, column, columnIndex, $columnIndex, fixed: fixedType })
}
}
}
if (showTooltip) {
thOns.mouseleave = evnt => {
if ($xetable._isResize) {
return
}
if (showTooltip) {
$xetable.handleTargetLeaveEvent(evnt)
}
}
}
if (highlightCurrentColumn || tableListeners['header-cell-click'] || isMouseChecked || sortOpts.trigger === 'cell') {
thOns.click = evnt => $xetable.triggerHeaderCellClickEvent(evnt, { $table: $xetable, $rowIndex, column, columnIndex, $columnIndex, fixed: fixedType, cell: evnt.currentTarget })
}
if (tableListeners['header-cell-dblclick']) {
thOns.dblclick = evnt => UtilTools.emitEvent($xetable, 'header-cell-dblclick', [{ $table: $xetable, $rowIndex, column, columnIndex, $columnIndex, fixed: fixedType, cell: evnt.currentTarget }, evnt])
}
// 按下事件处理
if (isMouseSelected || isMouseChecked) {
thOns.mousedown = evnt => $xetable.triggerHeaderCellMousedownEvent(evnt, { $table: $xetable, $rowIndex, column, columnIndex, $columnIndex, fixed: fixedType, cell: evnt.currentTarget })
}
let type = column.type === 'seq' || column.type === 'index' ? 'seq' : column.type
return h('th', {
class: ['vxe-header--column', column.id, {
[`col--${headAlign}`]: headAlign,
[`col--${type}`]: type,
'col--last': $columnIndex === cols.length - 1,
'col--fixed': column.fixed,
'col--group': isColGroup,
'col--ellipsis': hasEllipsis,
'fixed--hidden': fixedHiddenColumn,
'is--sortable': column.sortable,
'is--filter': column.filters,
'filter--active': hasFilter
}, UtilTools.getClass(headerClassName, params), UtilTools.getClass(headerCellClassName, params)],
attrs: {
'data-colid': column.id,
colspan: column.colSpan,
rowspan: column.rowSpan
},
style: headerCellStyle ? (XEUtils.isFunction(headerCellStyle) ? headerCellStyle(params) : headerCellStyle) : null,
on: thOns,
key: columnKey || isColGroup ? column.id : columnIndex
}, [
h('div', {
class: ['vxe-cell', {
'c--title': showTitle,
'c--tooltip': showTooltip,
'c--ellipsis': showEllipsis
}]
}, column.renderHeader(h, params)),
/**
* 列宽拖动
*/
!fixedHiddenColumn && !isColGroup && (XEUtils.isBoolean(column.resizable) ? column.resizable : resizable) ? h('div', {
class: ['vxe-resizable', {
'is--line': !border
}],
on: {
mousedown: evnt => this.resizeMousedown(evnt, params)
}
}) : null
])
}).concat(scrollbarWidth ? [
h('th', {
class: 'col--gutter'
})
] : []))
}))
]),
/**
* 其他
*/
h('div', {
class: 'vxe-table--repair',
ref: 'repair'
})
])
},
methods: {
uploadColumn () {
this.headerColumn = this.isGroup ? convertToRows(this.collectColumn) : [this.$parent.scrollXLoad && this.fixedType ? this.fixedColumn : this.tableColumn]
},
resizeMousedown (evnt, params) {
let { column } = params
let { $parent: $xetable, $el, fixedType } = this
let { tableBody, leftContainer, rightContainer, resizeBar: resizeBarElem } = $xetable.$refs
let { target: dragBtnElem, clientX: dragClientX } = evnt
let cell = dragBtnElem.parentNode
let dragLeft = 0
let minInterval = 36 // 列之间的最小间距
let tableBodyElem = tableBody.$el
let pos = DomTools.getOffsetPos(dragBtnElem, $el)
let dragBtnWidth = dragBtnElem.clientWidth
let dragMinLeft = pos.left - cell.clientWidth + dragBtnWidth + minInterval
let dragPosLeft = pos.left + Math.floor(dragBtnWidth / 2)
let domMousemove = document.onmousemove
let domMouseup = document.onmouseup
let isLeftFixed = fixedType === 'left'
let isRightFixed = fixedType === 'right'
// 计算左右侧固定列偏移量
let fixedOffsetWidth = 0
if (isLeftFixed || isRightFixed) {
let siblingProp = isLeftFixed ? 'nextElementSibling' : 'previousElementSibling'
let tempCellElem = cell[siblingProp]
while (tempCellElem) {
if (DomTools.hasClass(tempCellElem, 'fixed--hidden')) {
break
} else if (!DomTools.hasClass(tempCellElem, 'col--group')) {
fixedOffsetWidth += tempCellElem.offsetWidth
}
tempCellElem = tempCellElem[siblingProp]
}
if (isRightFixed && rightContainer) {
dragPosLeft = rightContainer.offsetLeft + fixedOffsetWidth
}
}
// 处理拖动事件
let updateEvent = function (evnt) {
evnt.stopPropagation()
evnt.preventDefault()
let offsetX = evnt.clientX - dragClientX
let left = dragPosLeft + offsetX
let scrollLeft = fixedType ? 0 : tableBodyElem.scrollLeft
if (isLeftFixed) {
// 左固定列(不允许超过右侧固定列、不允许超过右边距)
left = Math.min(left, (rightContainer ? rightContainer.offsetLeft : tableBodyElem.clientWidth) - fixedOffsetWidth - minInterval)
} else if (isRightFixed) {
// 右侧固定列(不允许超过左侧固定列、不允许超过左边距)
dragMinLeft = (leftContainer ? leftContainer.clientWidth : 0) + fixedOffsetWidth + minInterval
left = Math.min(left, dragPosLeft + cell.clientWidth - minInterval)
}
dragLeft = Math.max(left, dragMinLeft)
resizeBarElem.style.left = `${dragLeft - scrollLeft}px`
}
$xetable._isResize = true
DomTools.addClass($xetable.$el, 'c--resize')
resizeBarElem.style.display = 'block'
document.onmousemove = updateEvent
document.onmouseup = function (evnt) {
document.onmousemove = domMousemove
document.onmouseup = domMouseup
column.resizeWidth = column.renderWidth + (isRightFixed ? dragPosLeft - dragLeft : dragLeft - dragPosLeft)
resizeBarElem.style.display = 'none'
$xetable._isResize = false
$xetable._lastResizeTime = Date.now()
$xetable.analyColumnWidth()
$xetable.recalculate(true)
DomTools.removeClass($xetable.$el, 'c--resize')
if ($xetable.$toolbar) {
$xetable.$toolbar.updateResizable()
}
UtilTools.emitEvent($xetable, 'resizable-change', [params])
}
updateEvent(evnt)
}
}
}