Skip to content

Commit b1e15d7

Browse files
committed
(#69) Import & Export Csv, Excel Files
1 parent a77e76f commit b1e15d7

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

src/UIs/angular/src/app/products/list-products/list-products.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
Export as Pdf
77
</button>
88
&nbsp;
9+
<button type="button" class="btn btn-secondary" (click)="exportAsCsv()">
10+
Export as Csv
11+
</button>
12+
&nbsp;
913
<a
1014
class="btn btn-primary"
1115
routerLinkActive="active"

src/UIs/angular/src/app/products/list-products/list-products.component.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,18 @@ export class ListProductsComponent implements OnInit {
9999
error: (err) => (this.errorMessage = err),
100100
});
101101
}
102+
103+
exportAsCsv() {
104+
this.productService.exportAsCsv().subscribe({
105+
next: (rs) => {
106+
const url = window.URL.createObjectURL(rs);
107+
const element = document.createElement("a");
108+
element.href = url;
109+
element.download = "Products.csv";
110+
document.body.appendChild(element);
111+
element.click();
112+
},
113+
error: (err) => (this.errorMessage = err),
114+
});
115+
}
102116
}

src/UIs/angular/src/app/products/product.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export class ProductService {
5757
.pipe(catchError(this.handleError));
5858
}
5959

60+
exportAsCsv() {
61+
return this.http
62+
.get(this.productUrl + "/ExportAsCsv", { responseType: "blob" })
63+
.pipe(catchError(this.handleError));
64+
}
65+
6066
private handleError(err: HttpErrorResponse) {
6167
// in a real world app, we may send the server to some remote logging infrastructure
6268
// instead of just logging it to the console

src/UIs/reactjs/src/containers/Products/ListProducts/ListProducts.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ class ListProducts extends Component<any, any> {
5555
element.click();
5656
};
5757

58+
exportAsCsv = async () => {
59+
const rs = await axios.get("/ExportAsCsv", { responseType: "blob" });
60+
const url = window.URL.createObjectURL(rs.data);
61+
const element = document.createElement("a");
62+
element.href = url;
63+
element.download = "Products.csv";
64+
document.body.appendChild(element);
65+
element.click();
66+
};
67+
5868
deleteProduct = (product) => {
5969
this.setState({ showDeleteModal: true, deletingProduct: product });
6070
};
@@ -229,6 +239,14 @@ class ListProducts extends Component<any, any> {
229239
Export as Pdf
230240
</button>
231241
&nbsp;
242+
<button
243+
type="button"
244+
className="btn btn-secondary"
245+
onClick={this.exportAsCsv}
246+
>
247+
Export as Csv
248+
</button>
249+
&nbsp;
232250
<NavLink className="btn btn-primary" to="/products/add">
233251
Add Product
234252
</NavLink>

src/UIs/vuejs/src/views/products/List.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
Export as Pdf
88
</button>
99
&nbsp;
10+
<button type="button" class="btn btn-secondary" @click="exportAsCsv">
11+
Export as Csv
12+
</button>
13+
&nbsp;
1014
<router-link class="btn btn-primary" to="/products/add"
1115
>Add Product</router-link
1216
>
@@ -214,6 +218,16 @@ export default Vue.extend({
214218
document.body.appendChild(element);
215219
element.click();
216220
});
221+
},
222+
exportAsCsv() {
223+
axios.get("/ExportAsCsv", { responseType: "blob" }).then(rs => {
224+
const url = window.URL.createObjectURL(rs.data);
225+
const element = document.createElement("a");
226+
element.href = url;
227+
element.download = "Products.csv";
228+
document.body.appendChild(element);
229+
element.click();
230+
});
217231
}
218232
},
219233
components: {

0 commit comments

Comments
 (0)