-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.vue
207 lines (190 loc) · 5.69 KB
/
App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<template>
<DxDataGrid
:ref="gridRef"
:data-source="dataSource"
:allow-column-resizing="true"
@toolbar-preparing="toolbarCopy"
>
<DxColumn data-field="ID" :width="200" />
<DxColumn data-field="FirstName" />
<DxColumn data-field="LastName" />
<DxColumn data-field="HireDate" />
<DxColumn data-field="Residence" :group-index="0" />
<DxColumn data-field="IsTested" />
<DxColumn type="buttons" :buttons="buttons" />
<DxEditing
mode="popup"
:allow-updating="true"
:allow-deleting="true"
:allow-adding="true"
:use-icons="true"
/>
<DxGroupPanel :visible="true" />
<DxFilterRow :visible="true" />
<DxSummary>
<DxGroupItem
column="LastName"
summary-type="count"
name="Count"
:align-by-column="true"
/>
<DxGroupItem column="LastName" summary-type="count" name="Count" />
<DxGroupItem
column="IsTested"
summary-type="count"
:show-in-group-footer="true"
name="Count"
:align-by-column="true"
/>
<DxTotalItem
column="ID"
summary-type="count"
display-format="No. of employees: {0}'"
name="No. of employees"
/>
<DxTotalItem column="HireDate" summary-type="count" name="Dates" />
</DxSummary>
</DxDataGrid>
</template>
<script>
import DxDataGrid, {
DxEditing,
DxColumn,
DxGroupPanel,
DxGroupItem,
DxTotalItem,
DxFilterRow,
DxSummary,
} from "devextreme-vue/data-grid";
import DataSource from "devextreme/data/data_source";
import ArrayStore from "devextreme/data/array_store";
import { exportDataGrid } from "devextreme/excel_exporter";
import notify from "devextreme/ui/notify";
import * as ExcelJS from "exceljs";
import { data } from "./data";
const gridRef = "gridRef";
const dataSource = new DataSource({
store: new ArrayStore({
data: data,
key: "ID",
}),
});
export default {
name: "App",
components: {
DxDataGrid,
DxEditing,
DxColumn,
DxGroupPanel,
DxGroupItem,
DxTotalItem,
DxFilterRow,
DxSummary,
},
data() {
return {
dataSource: dataSource,
gridRef,
buttons: [
"edit",
"delete",
{
hint: "Copy row",
icon: "copy",
onClick: this.rowCopy,
},
],
};
},
methods: {
rowCopy(e) {
let data = e.row.data;
let str = "";
for (let prop in data) {
if (data[prop] !== undefined) {
str += `${data[prop]}\t`;
}
}
navigator.clipboard.writeText(str).then(() => {
notify("Row data copied to clipboard.", "success", 500);
}, () => {
notify("Row data was not copied. There are insufficient permissions for this action.", "error", 500);
});
},
toolbarCopy(e) {
e.toolbarOptions.items.push({
widget: "dxButton",
location: "after",
options: {
hint: "Copy via Export",
icon: "unselectall",
onClick: this.copyViaExcelExport
}
});
},
copyViaExcelExport(e) {
let workbook = new ExcelJS.Workbook();
let sheet = workbook.addWorksheet("dummy");
let str = "";
let col = this.grid.getVisibleColumns();
// keep exportable columns and get the last-most column
col = col.filter((x) => x.dataField !== undefined && x.allowExporting === true);
let lastColumn = col[col.length - 1].dataField;
exportDataGrid({
component: this.grid,
worksheet: sheet,
customizeCell: function (options) {
let { gridCell } = options;
let field = gridCell.column.dataField;
switch (gridCell.rowType) {
// export header row
case "header":
str += `${gridCell.column.caption}\t`;
break;
// export data row
case "data":
str += `${gridCell.value}\t`;
break;
// export group row
case "group":
if (gridCell.value)
str += `${field}: ${gridCell.value} `;
if (gridCell.groupSummaryItems !== undefined && gridCell.groupSummaryItems.length >= 1) {
gridCell.groupSummaryItems.forEach(x => {
str += ` ${x.name}: ${x.value} `;
});
}
str += `\t`;
break;
// export groupFooter & totalFooter. Create a separate switch case if you need different actions (ie different spacing)
case "groupFooter":
case "totalFooter":
str += (gridCell.value === undefined ? `\t` : `${gridCell.totalSummaryItemName}: ${gridCell.value}\t`);
break;
default:
console.log("Unknown row type detected. Please check possible DataGrid breaking changes regarding rowType", gridCell);
break;
}
if (field === lastColumn) {
str += `\r\n`;
}
}
}).then(() => {
console.log(str);
navigator.clipboard.writeText(str).then(() => {
notify("Grid data copied to clipboard.", "success", 500);
}, () => {
notify("Grid data was not copied. There are insufficient permissions for this action.", "error", 500);
});
});
}
},
computed: {
grid: function () {
return this.$refs[gridRef].instance;
},
},
};
</script>
<style>
</style>