项目作者: jgile

项目描述 :
Vue.js component to select a CSV file, map the columns to fields, and post it somewhere.
高级语言: Vue
项目地址: git://github.com/jgile/vue-csv-import.git
创建时间: 2018-04-12T20:52:04Z
项目社区:https://github.com/jgile/vue-csv-import

开源协议:MIT License

下载


Vue.js component to handle CSV uploads with field mapping.

Latest Version on NPM
Software License
npm tests
Scrutinizer Code Quality

This version is for Vue 3. Click here for Vue 2.

VueCsvImport is completely un-styled and customizable. All markup can be replaced and all text can be customized.

Demo


Installation

You can install the package via npm or yarn:

  1. # npm
  2. npm install vue-csv-import
  3. # Yarn
  4. yarn add vue-csv-import

You can import components individually.

  1. import {VueCsvToggleHeaders, VueCsvSubmit, VueCsvMap, VueCsvInput, VueCsvErrors, VueCsvImport} from 'vue-csv-import';

Or import all as a plugin.

  1. import {createApp} from "vue";
  2. import App from "./App.vue";
  3. import {VueCsvImportPlugin} from "vue-csv-import";
  4. createApp(App)
  5. .use(VueCsvImportPlugin)
  6. .mount("#app");

A minimal working example with all components will look something like this:

  1. <template>
  2. <vue-csv-import
  3. v-model="csv"
  4. :fields="{name: {required: false, label: 'Name'}, age: {required: true, label: 'Age'}}"
  5. >
  6. <vue-csv-toggle-headers></vue-csv-toggle-headers>
  7. <vue-csv-errors></vue-csv-errors>
  8. <vue-csv-input></vue-csv-input>
  9. <vue-csv-map></vue-csv-map>
  10. </vue-csv-import>
  11. </template>

Components

VueCsvImport

Primary wrapper component.

  1. <template>
  2. <vue-csv-import
  3. v-model="csv"
  4. :fields="{
  5. name: {
  6. required: false,
  7. label: 'Name'
  8. },
  9. age: {
  10. required: true,
  11. label: 'Age'
  12. }
  13. }"
  14. >
  15. <!-- Other Components -->
  16. </vue-csv-import>
  17. </template>

Props:

Prop Default Description
fields null (required) The field names used to map the CSV.
text see below (optional) Override the default text used in the component.
modelValue N/A (optional) Binds to the mapped CSV object.

Default text

  1. {
  2. errors: {
  3. fileRequired: 'A file is required',
  4. invalidMimeType: "Invalid file type"
  5. },
  6. toggleHeaders: 'File has headers',
  7. submitBtn: 'Submit',
  8. fieldColumn: 'Field',
  9. csvColumn: 'Column'
  10. }

Slot Props:

Prop Description
file The selected file
errors Current errors
fields The fields object

VueCsvToggleHeaders

Allows user to toggle whether the CSV has headers or not.

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-toggle-headers></vue-csv-toggle-headers>
  5. ...
  6. </vue-csv-import>
  7. </template>

Or with custom markup:

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-toggle-headers v-slot="{hasHeaders, toggle}">
  5. <button @click.prevent="toggle">Has Headers</button>
  6. </vue-csv-toggle-headers>
  7. ...
  8. </vue-csv-import>
  9. </template>

Props:

Prop Default Description
checkboxAttributes {} (optional) Attributes to bind to the checkbox.
labelAttributes {} (optional) Attributes to bind to the label.

Slot Props:

Prop Description
hasHeaders Whether CSV is marked as having headers.
toggle Toggle the ‘hasHeaders’ value.

VueCsvInput

The file field for importing CSV.

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-input name="file"></vue-csv-input>
  5. ...
  6. </vue-csv-import>
  7. </template>

Or with custom markup:

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-input v-slot="{file, change}">
  5. ...
  6. </vue-csv-input>
  7. ...
  8. </vue-csv-import>
  9. </template>

Props:

Prop Default Description
name N/A (required) The field names used to map the CSV.
headers true (optional) Override the default text used in the component.
parseConfig N/A (optional) Papaparse config object.
validation true (optional) Use validation or not
fileMimeTypes [“text/csv”, “text/x-csv”, “application/vnd.ms-excel”, “text/plain”] (optional) Accepted CSV file mime types.

Slot Props:

Prop Description
file The current file object
change Change the file

VueCsvMap

Component to map the CSV to the specified fields.

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-map></vue-csv-map>
  5. ...
  6. </vue-csv-import>
  7. </template>

Or use slot for custom markup:

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-map v-slot="{sample, map, fields}">
  5. ...
  6. </vue-csv-map>
  7. ...
  8. </vue-csv-import>
  9. </template>

Props:

Prop Default Description
noThead false (optional) Attributes to bind to the checkbox.
selectAttributes {} (optional) Attributes to bind to the select fields.
autoMatch true (optional) Auto-match fields to columns when they share the same name
autoMatchIgnoreCase true (optional) Ignore case when auto-matching

Slot Props:

Prop Description
sample The first row of the CSV.
map The currently mapped fields.
fields The fields.

VueCsvSubmit

Displays a button to post the CSV to specified URL.

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-submit url="/post/here" :config="{}"></vue-csv-submit>
  5. ...
  6. </vue-csv-import>
  7. </template>

Or use slot for custom markup:

  1. <template>
  2. <vue-csv-import>
  3. <vue-csv-submit v-slot="{submit, mappedCsv}">
  4. <button @click.prevent="submit">Submit!!</button>
  5. </vue-csv-submit>
  6. </vue-csv-import>
  7. </template>

Props:

Prop Default Description
url N/A (required) Where to post the CSV.
config {} (optional) Axios config object.

Slot Props:

Prop Description
submit Submit the CSV (POST)
mappedCsv The mapped CSV object

VueCsvErrors

Displays any error messages.

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-errors></vue-csv-errors>
  5. ...
  6. </vue-csv-import>
  7. </template>

Or use slot for custom markup:

  1. <template>
  2. <vue-csv-import>
  3. ...
  4. <vue-csv-errors v-slot="{errors}">
  5. ...
  6. </vue-csv-errors>
  7. ...
  8. </vue-csv-import>
  9. </template>

Slot Props:

Prop Description
errors Object containing errors

Testing

  1. npm run test

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

If you discover any security related issues, please contact John Gile.

License

The MIT License (MIT). Please see License File for more information.

Credits