# 一.Table
# index.ts
import Table from './src/table.vue'
import TableColumn from './src/tableColumn'
import type { App } from 'vue'
import type { SFCWithInstall } from '@element-plus/utils/types'
Table.install = (app: App): void => {
app.component(Table.name, Table)
app.component(TableColumn.name, TableColumn)
}
Table.TableColumn = TableColumn
const _Table = Table as any as SFCWithInstall<typeof Table> & {
TableColumn: typeof TableColumn
}
export default _Table
export const ElTable = _Table
export const ElTableColumn = TableColumn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# store
# table
# table-body
# table-column
# table-footer
# table-header
# config.ts
# filter-panel.vue
# h-helper.ts
# layout-observer.ts
# table-layout.ts
# table.vue
# tableColumn.ts
# util.ts
代码
import { hasOwn } from '@vue/shared'
import { createPopper } from '@popperjs/core'
import PopupManager from '@element-plus/utils/popup-manager'
import { getValueByPath } from '@element-plus/utils/util'
import { off, on } from '@element-plus/utils/dom'
import type {
PopperInstance,
IPopperOptions,
} from '@element-plus/components/popper'
import type { Indexable, Nullable } from '@element-plus/utils/types'
import type { TableColumnCtx } from './table-column/defaults'
export const getCell = function (event: Event): HTMLElement {
let cell = event.target as HTMLElement
while (cell && cell.tagName.toUpperCase() !== 'HTML') {
if (cell.tagName.toUpperCase() === 'TD') {
return cell
}
cell = cell.parentNode as HTMLElement
}
return null
}
const isObject = function (obj: unknown): boolean {
return obj !== null && typeof obj === 'object'
}
export const orderBy = function <T>(
array: T[],
sortKey: string,
reverse: string | number,
sortMethod,
sortBy: string | (string | ((a: T, b: T, array?: T[]) => number))[]
) {
if (
!sortKey &&
!sortMethod &&
(!sortBy || (Array.isArray(sortBy) && !sortBy.length))
) {
return array
}
if (typeof reverse === 'string') {
reverse = reverse === 'descending' ? -1 : 1
} else {
reverse = reverse && reverse < 0 ? -1 : 1
}
const getKey = sortMethod
? null
: function (value, index) {
if (sortBy) {
if (!Array.isArray(sortBy)) {
sortBy = [sortBy]
}
return sortBy.map(function (by) {
if (typeof by === 'string') {
return getValueByPath(value, by)
} else {
return by(value, index, array)
}
})
}
if (sortKey !== '$key') {
if (isObject(value) && '$value' in value) value = value.$value
}
return [isObject(value) ? getValueByPath(value, sortKey) : value]
}
const compare = function (a, b) {
if (sortMethod) {
return sortMethod(a.value, b.value)
}
for (let i = 0, len = a.key.length; i < len; i++) {
if (a.key[i] < b.key[i]) {
return -1
}
if (a.key[i] > b.key[i]) {
return 1
}
}
return 0
}
return array
.map(function (value, index) {
return {
value,
index,
key: getKey ? getKey(value, index) : null,
}
})
.sort(function (a, b) {
let order = compare(a, b)
if (!order) {
// make stable https://en.wikipedia.org/wiki/Sorting_algorithm#Stability
order = a.index - b.index
}
return order * +reverse
})
.map((item) => item.value)
}
export const getColumnById = function <T>(
table: {
columns: TableColumnCtx<T>[]
},
columnId: string
): null | TableColumnCtx<T> {
let column = null
table.columns.forEach(function (item) {
if (item.id === columnId) {
column = item
}
})
return column
}
export const getColumnByKey = function <T>(
table: {
columns: TableColumnCtx<T>[]
},
columnKey: string
): TableColumnCtx<T> {
let column = null
for (let i = 0; i < table.columns.length; i++) {
const item = table.columns[i]
if (item.columnKey === columnKey) {
column = item
break
}
}
return column
}
export const getColumnByCell = function <T>(
table: {
columns: TableColumnCtx<T>[]
},
cell: HTMLElement
): null | TableColumnCtx<T> {
const matches = (cell.className || '').match(/el-table_[^\s]+/gm)
if (matches) {
return getColumnById(table, matches[0])
}
return null
}
export const getRowIdentity = <T>(
row: T,
rowKey: string | ((row: T) => any)
): string => {
if (!row) throw new Error('row is required when get row identity')
if (typeof rowKey === 'string') {
if (rowKey.indexOf('.') < 0) {
return row[rowKey] + ''
}
const key = rowKey.split('.')
let current = row
for (let i = 0; i < key.length; i++) {
current = current[key[i]]
}
return current + ''
} else if (typeof rowKey === 'function') {
return rowKey.call(null, row)
}
}
export const getKeysMap = function <T>(
array: T[],
rowKey: string
): Record<string, { row: T; index: number }> {
const arrayMap = {}
;(array || []).forEach((row, index) => {
arrayMap[getRowIdentity(row, rowKey)] = { row, index }
})
return arrayMap
}
export function mergeOptions<T, K>(defaults: T, config: K): T & K {
const options = {} as T & K
let key
for (key in defaults) {
options[key] = defaults[key]
}
for (key in config) {
if (hasOwn(config as unknown as Indexable<any>, key)) {
const value = config[key]
if (typeof value !== 'undefined') {
options[key] = value
}
}
}
return options
}
export function parseWidth(width: number | string): number {
if (width !== undefined) {
width = parseInt(width as string, 10)
if (isNaN(width)) {
width = null
}
}
return +width
}
export function parseMinWidth(minWidth): number {
if (typeof minWidth !== 'undefined') {
minWidth = parseWidth(minWidth)
if (isNaN(minWidth)) {
minWidth = 80
}
}
return minWidth
}
export function parseHeight(height: number | string) {
if (typeof height === 'number') {
return height
}
if (typeof height === 'string') {
if (/^\d+(?:px)?$/.test(height)) {
return parseInt(height, 10)
} else {
return height
}
}
return null
}
// https://github.com/reduxjs/redux/blob/master/src/compose.js
export function compose(...funcs) {
if (funcs.length === 0) {
return (arg) => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce(
(a, b) =>
(...args) =>
a(b(...args))
)
}
export function toggleRowStatus<T>(
statusArr: T[],
row: T,
newVal: boolean
): boolean {
let changed = false
const index = statusArr.indexOf(row)
const included = index !== -1
const addRow = () => {
statusArr.push(row)
changed = true
}
const removeRow = () => {
statusArr.splice(index, 1)
changed = true
}
if (typeof newVal === 'boolean') {
if (newVal && !included) {
addRow()
} else if (!newVal && included) {
removeRow()
}
} else {
if (included) {
removeRow()
} else {
addRow()
}
}
return changed
}
export function walkTreeNode(
root,
cb,
childrenKey = 'children',
lazyKey = 'hasChildren'
) {
const isNil = (array) => !(Array.isArray(array) && array.length)
function _walker(parent, children, level) {
cb(parent, children, level)
children.forEach((item) => {
if (item[lazyKey]) {
cb(item, null, level + 1)
return
}
const children = item[childrenKey]
if (!isNil(children)) {
_walker(item, children, level + 1)
}
})
}
root.forEach((item) => {
if (item[lazyKey]) {
cb(item, null, 0)
return
}
const children = item[childrenKey]
if (!isNil(children)) {
_walker(item, children, 0)
}
})
}
export let removePopper
export function createTablePopper(
trigger: HTMLElement,
popperContent: string,
popperOptions: Partial<IPopperOptions>,
tooltipEffect: string
) {
function renderContent(): HTMLDivElement {
const isLight = tooltipEffect === 'light'
const content = document.createElement('div')
content.className = `el-popper ${isLight ? 'is-light' : 'is-dark'}`
content.innerHTML = popperContent
content.style.zIndex = String(PopupManager.nextZIndex())
document.body.appendChild(content)
return content
}
function renderArrow(): HTMLDivElement {
const arrow = document.createElement('div')
arrow.className = 'el-popper__arrow'
arrow.style.bottom = '-4px'
return arrow
}
function showPopper() {
popperInstance && popperInstance.update()
}
removePopper = function removePopper() {
try {
popperInstance && popperInstance.destroy()
content && document.body.removeChild(content)
off(trigger, 'mouseenter', showPopper)
off(trigger, 'mouseleave', removePopper)
} catch {}
}
let popperInstance: Nullable<PopperInstance> = null
const content = renderContent()
const arrow = renderArrow()
content.appendChild(arrow)
popperInstance = createPopper(trigger, content, {
modifiers: [
{
name: 'offset',
options: {
offset: [0, 8],
},
},
{
name: 'arrow',
options: {
element: arrow,
padding: 10,
},
},
],
...popperOptions,
})
on(trigger, 'mouseenter', showPopper)
on(trigger, 'mouseleave', removePopper)
return popperInstance
}
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
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