Skip to content

Commit fa9d066

Browse files
authored
Merge pull request #15 from njleonzhang/SimonZhangITer-dev
add csv export
2 parents 5b09739 + 015fc5d commit fa9d066

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

example/docs/cn.vue

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
:actions-def='getActionsDef()',
2525
:row-action-def='getRowActionsDef()',
2626
action-col-width='200'
27-
@row-click='rowClick')
27+
@row-click='rowClick'
28+
ref='table1')
2829
el-table-column(prop='flow_no', label='服务编号', sortable='custom')
2930
el-table-column(prop='content', label='服务内容', sortable='custom')
3031
el-table-column(prop='create_time', label='服务时间', sortable='custom')
@@ -98,11 +99,16 @@
9899

99100
el-card
100101
.desc
101-
p 事件 (https://github.com/njleonzhang/vue-data-tables#event)
102+
a(href='https://github.com/njleonzhang/vue-data-tables#event') 事件
102103
p 多数的事件都是对element-ui el-table事件的proxy
103-
p filtered-data事件用于传递过滤后数据,配合alasql(https://github.com/agershun/alasql)等库使用可以实现导出excel等功能
104+
p filtered-data事件用于传递过滤后数据,配合
105+
a(href='https://github.com/zemirco/json2csv') json2csv
106+
span 、
107+
a(href='https://github.com/agershun/alasql') alasql
108+
span 等库使用可以实现导出excel等功能
104109
data-tables(
105110
:data='tableData1',
111+
:actions-def='getExportActionsDef()',
106112
:col-not-row-click='["special_selection_col"]',
107113
@row-click='rowClick',
108114
@select='handleSelect',
@@ -145,6 +151,7 @@
145151
<script>
146152
import DataTables from '../../src/index.js'
147153
import {cn} from '../mock'
154+
import CsvExport from '../utils/CsvExport'
148155
149156
export default {
150157
name: 'app',
@@ -154,7 +161,9 @@ export default {
154161
155162
data() {
156163
return {
157-
tableData: []
164+
tableData: [],
165+
tableData1: [],
166+
filteredData: []
158167
}
159168
},
160169
@@ -185,6 +194,7 @@ export default {
185194
}]
186195
}
187196
},
197+
188198
getCheckFilterDef() {
189199
return {
190200
width: 14,
@@ -225,6 +235,29 @@ export default {
225235
name: 'RUA'
226236
}]
227237
},
238+
239+
getExportActionsDef() {
240+
let columns = ['room_no', 'cellphone', 'flow_no', 'state']
241+
let columnNames = ['房号', '电话号码', '订单号', '状态']
242+
243+
return {
244+
width: 19,
245+
def: [{
246+
name: 'export all',
247+
handler: () => {
248+
CsvExport(this.tableData1, columns, columnNames, '所有数据')
249+
},
250+
icon: 'plus'
251+
}, {
252+
name: 'export filtered',
253+
handler: () => {
254+
CsvExport(this.filteredData, columns, columnNames, '过滤后的数据')
255+
},
256+
icon: 'upload'
257+
}]
258+
}
259+
},
260+
228261
rowClick(row) {
229262
this.$message('row clicked')
230263
console.log('row clicked', row)
@@ -240,6 +273,7 @@ export default {
240273
},
241274
handleFilterDataChange(filteredData) {
242275
console.log('handleFilterDataChange', filteredData)
276+
this.filteredData = filteredData
243277
},
244278
getSearchDef() {
245279
return {

example/docs/en.vue

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@
103103
p most of the events is just a proxy for element-ui el-table.
104104
p filtered-data is special, which pass the filted data out the component.
105105
p you can export these data to excel by 3rd-party libs, such as:
106+
a(href='https://github.com/zemirco/json2csv') json2csv
107+
span 、
106108
a(href='https://github.com/agershun/alasql') alasql
107109

108110
data-tables(
109111
:data='tableData1',
110-
:col-not-row-click='["special_selection_col"]',
112+
:actions-def='getExportActionsDef()',
111113
@row-click='rowClick',
112114
@select='handleSelect',
113115
@select-all='handleAllSelect',
@@ -152,6 +154,7 @@
152154
<script>
153155
import DataTables from '../../src/index.js'
154156
import {en} from '../mock'
157+
import CsvExport from '../utils/CsvExport'
155158
156159
export default {
157160
name: 'app',
@@ -161,7 +164,9 @@ export default {
161164
162165
data() {
163166
return {
164-
tableData: []
167+
tableData: [],
168+
tableData1: [],
169+
filteredData: []
165170
}
166171
},
167172
@@ -232,6 +237,29 @@ export default {
232237
name: 'RUA'
233238
}]
234239
},
240+
241+
getExportActionsDef() {
242+
let columns = ['room_no', 'cellphone', 'flow_no', 'state']
243+
let columnNames = ['room NO.', 'Tel.', 'order No.', 'state']
244+
245+
return {
246+
width: 19,
247+
def: [{
248+
name: 'export all',
249+
handler: () => {
250+
CsvExport(this.tableData1, columns, columnNames, 'all')
251+
},
252+
icon: 'plus'
253+
}, {
254+
name: 'export filtered',
255+
handler: () => {
256+
CsvExport(this.filteredData, columns, columnNames, 'filtered')
257+
},
258+
icon: 'upload'
259+
}]
260+
}
261+
},
262+
235263
rowClick(row) {
236264
this.$message('row clicked')
237265
console.log('row clicked', row)
@@ -247,6 +275,7 @@ export default {
247275
},
248276
handleFilterDataChange(filteredData) {
249277
console.log('handleFilterDataChange', filteredData)
278+
this.filteredData = filteredData
250279
},
251280
getSearchDef() {
252281
return {

example/utils/CsvExport.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json2csv from 'json2csv'
2+
3+
export default (data, fields, fieldNames, fileName) => {
4+
try {
5+
var result = json2csv({
6+
data: data,
7+
fields: fields,
8+
fieldNames: fieldNames
9+
})
10+
var csvContent = 'data:text/csvcharset=GBK,\uFEFF' + result
11+
var encodedUri = encodeURI(csvContent)
12+
var link = document.createElement('a')
13+
link.setAttribute('href', encodedUri)
14+
link.setAttribute('download', `${(fileName || 'file')}.csv`)
15+
document.body.appendChild(link)
16+
link.click()
17+
document.body.removeChild(link)
18+
} catch (err) {
19+
console.error(err)
20+
}
21+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"function-bind": "^1.0.2",
5252
"html-webpack-plugin": "^2.8.1",
5353
"http-proxy-middleware": "^0.17.2",
54+
"json2csv": "^3.7.3",
5455
"json-loader": "^0.5.4",
5556
"node-sass": "^4.3.0",
5657
"opn": "^4.0.2",
@@ -73,4 +74,4 @@
7374
"node": ">= 4.0.0",
7475
"npm": ">= 3.0.0"
7576
}
76-
}
77+
}

0 commit comments

Comments
 (0)