vue js tutorial csv import
- Install
papaparse
library using npm:
npm install papaparse
- Import
Papa
object frompapaparse
in your Vue component:
import Papa from 'papaparse';
- Add a file input in your template for CSV file selection:
<template>
<div>
<input type="file" @change="handleFileUpload" />
</div>
</template>
- Add a method to handle file upload and parse CSV data:
methods: {
handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
Papa.parse(file, {
complete: this.handleCSVData,
header: true,
});
}
},
handleCSVData(result) {
// Access CSV data in result.data
console.log(result.data);
},
},
- Customize the
handleCSVData
method to suit your application logic. The parsed CSV data is available inresult.data
.
Note: Adjust the template and methods based on your specific requirements. This example assumes a basic setup for CSV import in a Vue.js component using the papaparse
library.