Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS import and parse CSV (frontend only)

Tags:

vue.js

can you please share a working example of parsing CSV file from client?

I can't seem to find any working example at all ...

I have been playing around with vue-papa-parser and vue-csv-import but can make it work :(

Here is my component file (using the latter package):

    <template lang="html">
     <div class="container">
       <h1 class="h2">CSV Upload</h1>
       <div class="dragndrop text-muted d-flex flex-column justify-content-center align-items-center border border-secondary">
    
          <vue-csv-import
              v-model="csv"
              :fields="{name: {required: false, label: 'Name'}, age: {required: true, label: 'Age'}}"
          >
              <vue-csv-toggle-headers></vue-csv-toggle-headers>
              <vue-csv-errors></vue-csv-errors>
              <vue-csv-input></vue-csv-input>
              <vue-csv-map :auto-match="false"></vue-csv-map>
          </vue-csv-import>
       </div>
     </div>
    </template>
    
    <script>
      import { VueCsvImport } from 'vue-csv-import';
    
      export default {
        name: "CSVUpload",
        components: {
          VueCsvImport
        },
        data() {
          return {
            csv: null
          }
        },
        methods: {
        }
      }
    </script>
    
    <style lang="css" scoped>
    .dragndrop {
      background-color: #f5f5f5;
      height: 50vh;
      width: 80%;
      margin: 2.5em auto;
    }
    </style>

but on the console I am getting this:

[Vue warn]: Error in render: "TypeError: Cannot read property 'VueCsvImportData' of undefined"

found in

---> <VueCsvImport>
       <CallRecordUpload> at src/components/admin/Upload/Upload.vue
         <Home> at src/components/Home.vue
           <App> at src/App.vue
             <Root>

and thus I cannot even see any INPUT TYPE="FILE" to try to upload ... there is something wrong with the vue-csv-import :(

any thoughts?

EDIT: SOLVED

I had to install lower version (v4 is for vue3 only!)

npm install [email protected] --save

works for me great!

thx @marsnebulasoup for the tip !!

like image 888
Mr.P Avatar asked Dec 18 '20 19:12

Mr.P


People also ask

How do I post a CSV file in Vue JS?

Vue.js component to select a CSV file, map the columns to fields, and post it somewhere. If present, the component will post the mapped values to this url.

How to handle CSV uploads with field mapping in Vue?

Vue.js component to handle CSV uploads with field mapping 1 vue-csv-import. Vue.js component to select a CSV file, map the columns to fields, and post it somewhere. 2 Installation 3 Usage. If present, the component will post the mapped values to this url.

How do I parse a CSV file with papaparse?

The parsed piece of data is simply a flag to let the component know that Papaparse has parsed all of our CSV. We will use this to determine when to render the information. Next, check out your handleFileUpload ( event ) method ( See Uploading Files with VueJS and Axios ). Right after we set the file, add a call to this.parseFile () :

How to parse a CSV file with a predefined header?

You might want to use most popular CSV parser which is PapaParse. this library has various configuration options and one of them is 'Header row' which is exact solution you need. to use the predefined header you can supply header argument as true to have all the data parsed as key-value pairs.


1 Answers

bad version .. it automatically uses the latest version 4 (which is for vue3 only .. for vue2 I had to use 2.3.4 and it worked great :) )

see the changelog

like image 163
Mr.P Avatar answered Sep 29 '22 19:09

Mr.P