16
16
</tr >
17
17
</tbody >
18
18
</table >
19
+ <button
20
+ :style =" enableExportCsv ? '' : 'display: none;'"
21
+ :disabled =" !this.rows.length > 0"
22
+ class =" btn btn-xs btn-success pull-right"
23
+ type =" button"
24
+ @click =" exportCsv()" >
25
+ <i class =" fa fa-file-excel-o" ></i >
26
+ Exportar para CSV
27
+ </button >
19
28
</div >
20
29
</template >
21
30
22
31
<script >
23
32
export default {
24
- props: [' id' , ' columns' , ' rows' ],
33
+ // props: ['id', 'columns', 'rows', 'enableExportCsv'],
34
+ props: {
35
+ id: {default: ' table' },
36
+ columns: {default: []},
37
+ rows: {default: []},
38
+ enableExportCsv: {default: true },
39
+ },
25
40
data () {
26
41
return {
27
42
}
@@ -33,7 +48,6 @@ export default {
33
48
datatable .destroy ()
34
49
this .initDataTable (newValue)
35
50
}
36
- else console .log (' data is not an array' )
37
51
}
38
52
},
39
53
methods: {
@@ -58,6 +72,22 @@ export default {
58
72
59
73
60
74
75
+ exportCsv () {
76
+ var csv = this .$papa .unparse (this .rows )
77
+ // console.log(location)
78
+ // location.href =
79
+
80
+ var link = document .createElement (' a' )
81
+ link .href = ' data:application/download,' + encodeURIComponent (csv)
82
+ link .download = ' lista.csv'
83
+
84
+ document .body .appendChild (link)
85
+ link .click ()
86
+ document .body .removeChild (link)
87
+ },
88
+
89
+
90
+
61
91
formatData (rows , columns ) {
62
92
if (rows instanceof Array && columns instanceof Array ) {
63
93
rows .forEach ((row ) => {
@@ -113,7 +143,7 @@ export default {
113
143
ordering: true ,
114
144
order: [[1 , ' desc' ]],
115
145
language: {
116
- url: ' //cdn.datatables.net/plug-ins/1.10.13/i18n/English .json'
146
+ url: ' //cdn.datatables.net/plug-ins/1.10.13/i18n/Portuguese-Brasil .json'
117
147
},
118
148
columns: this .columns ,
119
149
data: this .rows ,
0 commit comments