forked from wenzhixin/bootstrap-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-table-export.js
More file actions
192 lines (167 loc) · 5.29 KB
/
Copy pathbootstrap-table-export.js
File metadata and controls
192 lines (167 loc) · 5.29 KB
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
/**
* @author zhixin wen <wenzhixin2010@gmail.com>
* extensions: https://github.com/kayalshri/tableExport.jquery.plugin
*/
($ => {
const Utils = $.fn.bootstrapTable.utils
const bootstrap = {
3: {
icons: {
export: 'glyphicon-export icon-share'
},
html: {
dropmenu: '<ul class="dropdown-menu" role="menu"></ul>',
dropitem: '<li role="menuitem" data-type="%s"><a href="javascript:">%s</a></li>'
}
},
4: {
icons: {
export: 'fa-download'
},
html: {
dropmenu: '<div class="dropdown-menu dropdown-menu-right"></div>',
dropitem: '<a class="dropdown-item" data-type="%s" href="javascript:">%s</a>'
}
}
}[Utils.bootstrapVersion]
const TYPE_NAME = {
json: 'JSON',
xml: 'XML',
png: 'PNG',
csv: 'CSV',
txt: 'TXT',
sql: 'SQL',
doc: 'MS-Word',
excel: 'MS-Excel',
xlsx: 'MS-Excel (OpenXML)',
powerpoint: 'MS-Powerpoint',
pdf: 'PDF'
}
$.extend($.fn.bootstrapTable.defaults, {
showExport: false,
exportDataType: 'basic', // basic, all, selected
exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
exportOptions: {},
exportFooter: false
})
$.extend($.fn.bootstrapTable.defaults.icons, {
export: bootstrap.icons.export
})
$.extend($.fn.bootstrapTable.locales, {
formatExport () {
return 'Export data'
}
})
$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
$.BootstrapTable = class extends $.BootstrapTable {
initToolbar () {
const o = this.options
this.showToolbar = this.showToolbar || o.showExport
super.initToolbar()
if (!this.options.showExport) {
return
}
const $btnGroup = this.$toolbar.find('>.btn-group')
let $export = $btnGroup.find('div.export')
if ($export.length) {
return
}
$export = $(`
<div class="export btn-group">
<button class="btn btn-${o.buttonsClass} btn-${o.iconSize} dropdown-toggle"
aria-label="export type"
title="${o.formatExport()}"
data-toggle="dropdown"
type="button">
<i class="${o.iconsPrefix} ${o.icons.export}"></i>
<span class="caret"></span>
</button>
${bootstrap.html.dropmenu}
</div>
`).appendTo($btnGroup)
const $menu = $export.find('.dropdown-menu')
let exportTypes = o.exportTypes
if (typeof exportTypes === 'string') {
const types = exportTypes.slice(1, -1).replace(/ /g, '').split(',')
exportTypes = types.map(t => t.slice(1, -1))
}
for (let type of exportTypes) {
if (TYPE_NAME.hasOwnProperty(type)) {
$menu.append(Utils.sprintf(bootstrap.html.dropitem, type, TYPE_NAME[type]))
}
}
$menu.find('>li, >a').click(e => {
const type = $(e.currentTarget).data('type')
const doExport = () => {
const data = this.getData()
if (o.exportFooter) {
const $footerRow = this.$tableFooter.find('tr').first()
const footerData = {}
const footerHtml = []
$.each($footerRow.children(), function (index, footerCell) {
var footerCellHtml = $(footerCell).children('.th-inner').first().html()
footerData[this.columns[index].field] = footerCellHtml === ' ' ? null : footerCellHtml
// grab footer cell text into cell index-based array
footerHtml.push(footerCellHtml)
})
this.append(footerData)
var $lastTableRow = this.$body.children().last()
$.each($lastTableRow.children(), function (index, lastTableRowCell) {
$(lastTableRowCell).html(footerHtml[index])
})
}
this.$el.tableExport($.extend({}, o.exportOptions, {
type: type,
escape: false
}))
if (o.exportFooter) {
this.load(data)
}
}
const stateField = this.header.stateField
if (o.exportDataType === 'all' && o.pagination) {
const eventName = o.sidePagination === 'server'
? 'post-body.bs.table' : 'page-change.bs.table'
this.$el.one(eventName, () => {
if (stateField) {
this.hideColumn(stateField)
}
doExport()
this.togglePagination()
})
this.togglePagination()
} else if (o.exportDataType === 'selected') {
let data = this.getData()
let selectedData = this.getSelections()
if (!selectedData.length) {
return
}
if (o.sidePagination === 'server') {
data = {
total: o.totalRows,
[this.options.dataField]: data
}
selectedData = {
total: selectedData.length,
[this.options.dataField]: selectedData
}
}
this.load(selectedData)
if (stateField) {
this.hideColumn(stateField)
}
doExport()
this.load(data)
} else {
if (stateField) {
this.hideColumn(stateField)
}
doExport()
}
if (stateField) {
this.showColumn(stateField)
}
})
}
}
})(jQuery)