-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
363 lines (352 loc) · 9.43 KB
/
index.ts
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
/* eslint-disable no-unused-vars */
import Vue, { CreateElement } from 'vue'
import XEUtils from 'xe-utils/ctor'
import {
VXETable,
ColumnConfig,
ColumnEditRenderOptions,
ColumnEditRenderParams,
ColumnCellRenderOptions,
ColumnCellRenderParams,
MenuLinkParams
} from 'vxe-table/lib/vxe-table'
/* eslint-enable no-unused-vars */
const excelEditConfig = {
trigger: 'dblclick',
mode: 'cell',
showIcon: false,
showStatus: false
}
const excelContextMenu = {
header: {
options: [
[
{
code: 'exportAll',
name: '隐藏列'
},
{
code: 'exportAll',
name: '取消所有隐藏'
}
]
]
},
body: {
options: [
[
{
code: 'clip',
name: '剪贴(Ctrl+X)'
},
{
code: 'copy',
name: '复制(Ctrl+C)'
},
{
code: 'paste',
name: '粘贴(Ctrl+V)'
}
],
[
{
code: 'insert',
name: '插入'
},
{
code: 'remove',
name: '删除'
},
{
code: 'clearData',
name: '清除内容(Del)'
}
],
[
{
code: 'filter',
name: '筛选',
children: [
{
code: 'clearFilter',
name: '清除筛选'
},
{
code: 'filterSelect',
name: '按所选单元格的值筛选'
}
]
},
{
code: 'sort',
name: '排序',
children: [
{
code: 'clearSort',
name: '清除排序'
},
{
code: 'sortAsc',
name: '升序'
},
{
code: 'sortDesc',
name: '倒序'
}
]
}
],
[
{
code: 'exportAll',
name: '导出数据.csv'
}
]
]
}
}
function registerComponent (params: any) {
const _Vue: typeof Vue = params.Vue
const Table: any = params.Table
const Excel: { [key: string]: any } = {
name: 'VxeExcel',
props: {
columns: Array
},
data () {
return {
excelStore: {
uploadRows: []
}
}
},
computed: {
tableProps (this: any) {
const { $props, editConfig, sortConfig, filterConfig } = this
return XEUtils.assign({}, $props, {
border: true,
resizable: true,
showOverflow: null,
contextMenu: excelContextMenu,
mouseConfig: { selected: true, checked: true },
keyboardConfig: { isArrow: true, isDel: true, isEnter: true, isTab: true, isCut: true, isEdit: true },
editConfig: Object.assign({}, excelEditConfig, editConfig),
sortConfig: Object.assign({ showIcon: false }, sortConfig),
filterConfig: Object.assign({ showIcon: false }, filterConfig),
optimization: {
scrollX: {
gt: 100
},
scrollY: {
gt: 200
}
}
})
}
},
watch: {
columns (this: any, value: ColumnConfig[]) {
this.loadColumn(value)
}
},
mounted (this: any) {
const { columns } = this
if (columns && columns.length) {
this.loadColumn(this.columns)
}
},
render (this: any, h: CreateElement) {
const { $slots, $listeners, tableProps } = this
return h('vxe-table', {
class: 'vxe-excel',
props: tableProps,
on: XEUtils.assign({}, $listeners, {
'context-menu-click': this.contextMenuClickEvent
}),
ref: 'xTable'
}, $slots.default)
},
methods: {
contextMenuClickEvent (this: any, params: MenuLinkParams, evnt: any) {
const { menu, row, column } = params
const $table = this.$refs.xTable
const { property } = column
switch (menu.code) {
case 'clip':
$table.handleCopyed(true, evnt)
break
case 'copy':
$table.handleCopyed(false, evnt)
break
case 'paste':
$table.handlePaste(evnt)
break
case 'insert':
$table.insertAt({}, row)
break
case 'remove':
$table.remove(row)
break
case 'clearData':
$table.clearData(row, property)
break
case 'clearFilter':
$table.clearFilter(column)
break
case 'filterSelect':
$table.setFilter(column, [
{ data: XEUtils.get(row, property), checked: true }
])
$table.updateData()
$table.clearIndexChecked()
$table.clearHeaderChecked()
$table.clearChecked()
$table.clearSelected()
$table.clearCopyed()
break
case 'clearSort':
$table.clearSort()
break
case 'sortAsc':
$table.sort(property, 'asc')
break
case 'sortDesc':
$table.sort(property, 'desc')
break
case 'exportAll':
$table.exportData({ isHeader: false })
break
}
}
}
}
// 继承 Table
XEUtils.assign(Excel.props, Table.props)
XEUtils.each(Table.methods, (cb: Function, name: string) => {
Excel.methods[name] = function (this: any) {
return this.$refs.xTable[name].apply(this.$refs.xTable, arguments)
}
})
_Vue.component(Excel.name, Excel)
}
const rowHeight: number = 24
interface posRangeData {
text: string;
start: number;
end: number;
}
function getCursorPosition (textarea: any): posRangeData {
const rangeData: posRangeData = { text: '', start: 0, end: 0 }
if (textarea.setSelectionRange) {
rangeData.start = textarea.selectionStart
rangeData.end = textarea.selectionEnd
}
return rangeData
}
function setCursorPosition (textarea: any, rangeData: posRangeData) {
if (textarea.setSelectionRange) {
textarea.focus()
textarea.setSelectionRange(rangeData.start, rangeData.end)
}
}
/**
* 渲染函数
*/
const renderMap = {
cell: {
autofocus: 'textarea',
renderEdit (h: CreateElement, editRender: ColumnEditRenderOptions, params: ColumnEditRenderParams) {
const { $table, row } = params
const $excel: any = $table.$parent
const { excelStore } = $excel
const { uploadRows } = excelStore
const column: any = params.column
const model: { value: any, update: boolean } = column.model
return [
h('div', {
class: 'vxe-textarea vxe-excel-cell',
style: {
height: `${column.renderHeight}px`
}
}, [
h('textarea', {
class: 'vxe-textarea--inner',
style: {
width: `${column.renderWidth}px`
},
domProps: {
value: model.value
},
on: {
input (evnt: any) {
const inpElem = evnt.target
model.update = true
model.value = inpElem.value
if (inpElem.scrollHeight > inpElem.offsetHeight) {
if (uploadRows.indexOf(row) === -1) {
inpElem.style.width = `${inpElem.offsetWidth + 20}px`
} else {
inpElem.style.height = `${inpElem.scrollHeight}px`
}
}
},
change () {
if (uploadRows.indexOf(row) === -1) {
uploadRows.push(row)
}
},
keydown (evnt: any) {
const inpElem = evnt.target
if (evnt.altKey && evnt.keyCode === 13) {
evnt.preventDefault()
evnt.stopPropagation()
const rangeData = getCursorPosition(inpElem)
let pos = rangeData.end
let cellValue = inpElem.value
cellValue = `${cellValue.slice(0, pos)}\n${cellValue.slice(pos, cellValue.length)}`
inpElem.value = cellValue
model.update = true
model.value = cellValue
inpElem.style.height = `${(Math.floor(inpElem.offsetHeight / rowHeight) + 1) * rowHeight}px`
setTimeout(() => {
rangeData.start = rangeData.end = ++pos
setCursorPosition(inpElem, rangeData)
})
}
}
}
})
])
]
},
renderCell (h: CreateElement, editRender: ColumnCellRenderOptions, params: ColumnCellRenderParams) {
const { row, column } = params
return [
h('span', {
domProps: {
innerHTML: XEUtils.escape(XEUtils.get(row, column.property)).replace(/\n/g, '<br>')
}
})
]
}
}
}
/**
* 基于 vxe-table 表格的增强插件,实现简单的 EXCEL 表格
*/
export const VXETablePluginExcel = {
install (xtable: typeof VXETable) {
const { renderer, v } = xtable
if (v !== 'v2') {
throw new Error('[vxe-table-plugin-excel] V2 version is required.')
}
// 添加到渲染器
renderer.mixin(renderMap)
// 注册组件
registerComponent(xtable)
}
}
if (typeof window !== 'undefined' && window.VXETable) {
window.VXETable.use(VXETablePluginExcel)
}
export default VXETablePluginExcel