Skip to content

Commit f73bbd9

Browse files
committed
Adding 'Export to Csv' button
1 parent 8391656 commit f73bbd9

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ cols: [{data: 'price', label: 'Price', filter: 'currency'}]
3939
## About
4040

4141
- full support to ajax requests not implemented yet;
42-
- this component is in development;
42+
- this component is in development;
43+
- any sugestions will be helpful;

src/components/DataTable.vue

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,27 @@
1616
</tr>
1717
</tbody>
1818
</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>
1928
</div>
2029
</template>
2130

2231
<script>
2332
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+
},
2540
data () {
2641
return {
2742
}
@@ -33,7 +48,6 @@ export default {
3348
datatable.destroy()
3449
this.initDataTable(newValue)
3550
}
36-
else console.log('data is not an array')
3751
}
3852
},
3953
methods: {
@@ -58,6 +72,22 @@ export default {
5872
5973
6074
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+
6191
formatData(rows, columns) {
6292
if(rows instanceof Array && columns instanceof Array) {
6393
rows.forEach((row) => {
@@ -113,7 +143,7 @@ export default {
113143
ordering: true,
114144
order: [[1, 'desc']],
115145
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'
117147
},
118148
columns: this.columns,
119149
data: this.rows,

0 commit comments

Comments
 (0)