Skip to content

Commit 015fc5d

Browse files
committed
make export params more flexible
1 parent 9c68b3f commit 015fc5d

File tree

3 files changed

+72
-53
lines changed

3 files changed

+72
-53
lines changed

example/docs/cn.vue

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@
9999

100100
el-card
101101
.desc
102-
p 事件 (https://github.com/njleonzhang/vue-data-tables#event)
102+
a(href='https://github.com/njleonzhang/vue-data-tables#event') 事件
103103
p 多数的事件都是对element-ui el-table事件的proxy
104-
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等功能
105109
data-tables(
106110
:data='tableData1',
111+
:actions-def='getExportActionsDef()',
107112
:col-not-row-click='["special_selection_col"]',
108113
@row-click='rowClick',
109114
@select='handleSelect',
@@ -156,7 +161,9 @@ export default {
156161
157162
data() {
158163
return {
159-
tableData: []
164+
tableData: [],
165+
tableData1: [],
166+
filteredData: []
160167
}
161168
},
162169
@@ -168,67 +175,26 @@ export default {
168175
},
169176
170177
methods: {
171-
/**
172-
* 参数可选
173-
*
174-
* fileName 导出的文件名
175-
*
176-
* filtered
177-
* true 导出筛选、排序后的数据
178-
* false 默认值,导出原数据
179-
*/
180-
exportData(config) {
181-
let dataTable = this.$refs.table1
182-
let table = dataTable.$children.filter(t => t.$el._prevClass.indexOf('el-table') !== -1)[0]
183-
let data = dataTable.data
184-
let columns = this.getColumns(table)
185-
const fields = columns.map(t => t.prop)
186-
const fieldNames = columns.map(t => t.label)
187-
if (config.filtered) {
188-
data = dataTable.tableData
189-
}
190-
CsvExport(data, fields, fieldNames, config.fileName)
191-
},
192-
/**
193-
* 针对多级分类表格,需循环遍历table获取columns
194-
*/
195-
getColumns(tableEl, arr = []) {
196-
if (!tableEl.$children.length) {
197-
return
198-
}
199-
tableEl.$children.forEach((data) => {
200-
if (!data.$children.length && data.prop) {
201-
arr.push(data)
202-
} else {
203-
this.getColumns(data, arr)
204-
}
205-
})
206-
return arr
207-
},
208178
getActionsDef() {
209179
let self = this
210180
return {
211181
width: 5,
212182
def: [{
213-
name: '导出原始数据',
183+
name: 'new',
214184
handler() {
215-
self.exportData({
216-
fileName: '原始数据'
217-
})
185+
self.$message('new clicked')
218186
},
219-
icon: 'upload'
187+
icon: 'plus'
220188
}, {
221-
name: '导出排序和过滤后的数据',
189+
name: 'import',
222190
handler() {
223-
self.exportData({
224-
fileName: '排序和过滤后的数据',
225-
filtered: true
226-
})
191+
self.$message('import clicked')
227192
},
228193
icon: 'upload'
229194
}]
230195
}
231196
},
197+
232198
getCheckFilterDef() {
233199
return {
234200
width: 14,
@@ -269,6 +235,29 @@ export default {
269235
name: 'RUA'
270236
}]
271237
},
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+
272261
rowClick(row) {
273262
this.$message('row clicked')
274263
console.log('row clicked', row)
@@ -284,6 +273,7 @@ export default {
284273
},
285274
handleFilterDataChange(filteredData) {
286275
console.log('handleFilterDataChange', filteredData)
276+
this.filteredData = filteredData
287277
},
288278
getSearchDef() {
289279
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 {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"publish": "node publish/publish.js"
2323
},
2424
"dependencies": {
25-
"json2csv": "^3.7.3",
2625
"vue": "^2.2.1"
2726
},
2827
"devDependencies": {
@@ -52,6 +51,7 @@
5251
"function-bind": "^1.0.2",
5352
"html-webpack-plugin": "^2.8.1",
5453
"http-proxy-middleware": "^0.17.2",
54+
"json2csv": "^3.7.3",
5555
"json-loader": "^0.5.4",
5656
"node-sass": "^4.3.0",
5757
"opn": "^4.0.2",

0 commit comments

Comments
 (0)