Skip to content

Commit 573fcff

Browse files
committed
feat(docs): Add an export to csv example for QTable
1 parent 23fc00a commit 573fcff

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<template>
2+
<div class="q-pa-md">
3+
<q-table
4+
title="Treats"
5+
:data="data"
6+
:columns="columns"
7+
color="primary"
8+
row-key="name"
9+
>
10+
<template v-slot:top-right>
11+
<q-btn
12+
color="primary"
13+
icon-right="archive"
14+
label="Export to csv"
15+
no-caps
16+
@click="exportTable"
17+
size="sm"
18+
/>
19+
</template>
20+
</q-table>
21+
</div>
22+
</template>
23+
24+
<script>
25+
import { exportFile } from 'quasar'
26+
27+
export default {
28+
methods: {
29+
exportTable () {
30+
// naive encoding to csv format
31+
const content = [ this.columns.map(col => `"${col.label}"`).join(',') ].concat(
32+
this.data.map(row => Object.keys(row).map(key => `"${row[key]}"`).join(','))
33+
).join('\r\n')
34+
35+
const status = exportFile(
36+
'table-export.csv',
37+
content,
38+
'text/csv'
39+
)
40+
41+
if (status !== true) {
42+
this.$q.notify({
43+
message: 'Browser denied file download...',
44+
color: 'negative',
45+
icon: 'warning'
46+
})
47+
}
48+
}
49+
},
50+
51+
data () {
52+
return {
53+
columns: [
54+
{
55+
name: 'desc',
56+
required: true,
57+
label: 'Dessert (100g serving)',
58+
align: 'left',
59+
field: row => row.name,
60+
format: val => `${val}`,
61+
sortable: true
62+
},
63+
{ name: 'calories', align: 'center', label: 'Calories', field: 'calories', sortable: true },
64+
{ name: 'fat', label: 'Fat (g)', field: 'fat', sortable: true },
65+
{ name: 'carbs', label: 'Carbs (g)', field: 'carbs' },
66+
{ name: 'protein', label: 'Protein (g)', field: 'protein' },
67+
{ name: 'sodium', label: 'Sodium (mg)', field: 'sodium' },
68+
{ name: 'calcium', label: 'Calcium (%)', field: 'calcium', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) },
69+
{ name: 'iron', label: 'Iron (%)', field: 'iron', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) }
70+
],
71+
72+
data: [
73+
{
74+
name: 'Frozen Yogurt',
75+
calories: 159,
76+
fat: 6.0,
77+
carbs: 24,
78+
protein: 4.0,
79+
sodium: 87,
80+
calcium: '14%',
81+
iron: '1%'
82+
},
83+
{
84+
name: 'Ice cream sandwich',
85+
calories: 237,
86+
fat: 9.0,
87+
carbs: 37,
88+
protein: 4.3,
89+
sodium: 129,
90+
calcium: '8%',
91+
iron: '1%'
92+
},
93+
{
94+
name: 'Eclair',
95+
calories: 262,
96+
fat: 16.0,
97+
carbs: 23,
98+
protein: 6.0,
99+
sodium: 337,
100+
calcium: '6%',
101+
iron: '7%'
102+
},
103+
{
104+
name: 'Cupcake',
105+
calories: 305,
106+
fat: 3.7,
107+
carbs: 67,
108+
protein: 4.3,
109+
sodium: 413,
110+
calcium: '3%',
111+
iron: '8%'
112+
},
113+
{
114+
name: 'Gingerbread',
115+
calories: 356,
116+
fat: 16.0,
117+
carbs: 49,
118+
protein: 3.9,
119+
sodium: 327,
120+
calcium: '7%',
121+
iron: '16%'
122+
},
123+
{
124+
name: 'Jelly bean',
125+
calories: 375,
126+
fat: 0.0,
127+
carbs: 94,
128+
protein: 0.0,
129+
sodium: 50,
130+
calcium: '0%',
131+
iron: '0%'
132+
},
133+
{
134+
name: 'Lollipop',
135+
calories: 392,
136+
fat: 0.2,
137+
carbs: 98,
138+
protein: 0,
139+
sodium: 38,
140+
calcium: '0%',
141+
iron: '2%'
142+
},
143+
{
144+
name: 'Honeycomb',
145+
calories: 408,
146+
fat: 3.2,
147+
carbs: 87,
148+
protein: 6.5,
149+
sodium: 562,
150+
calcium: '0%',
151+
iron: '45%'
152+
},
153+
{
154+
name: 'Donut',
155+
calories: 452,
156+
fat: 25.0,
157+
carbs: 51,
158+
protein: 4.9,
159+
sodium: 326,
160+
calcium: '2%',
161+
iron: '22%'
162+
},
163+
{
164+
name: 'KitKat',
165+
calories: 518,
166+
fat: 26.0,
167+
carbs: 65,
168+
protein: 7,
169+
sodium: 54,
170+
calcium: '12%',
171+
iron: '6%'
172+
}
173+
]
174+
}
175+
}
176+
}
177+
</script>

docs/src/pages/vue-components/table.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ In the example below, steps have been taken to emulate an ajax call to a server.
288288

289289
<doc-example title="Synchronizing with server" file="QTable/Synchronizing" />
290290

291+
### Exporting data
292+
293+
Below is an example of a naive csv encoding and then exporting table data by using the [exportFile](/quasar-utils/other-utils#Export-file) Quasar util. The browser should trigger a file download. For a more professional approach in regards to encoding we do recommend using [csv-parse](https://csv.js.org/parse/) and [csv-stringify](https://csv.js.org/stringify/) packages.
294+
295+
<doc-example title="Export to csv" file="QTable/ExportCsv" no-edit />
296+
291297
## QTable API
292298
<doc-api file="QTable" />
293299

0 commit comments

Comments
 (0)