4.1.2 • Published 3 years ago

vue-csv-import v4.1.2

Weekly downloads
1,541
License
MIT
Repository
github
Last release
3 years ago

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

Latest Version on NPM Software License CircleCI 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:

# npm
npm install vue-csv-import

# Yarn
yarn add vue-csv-import

You can import components individually.

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

Or import all as a plugin.

import {createApp} from "vue";
import App from "./App.vue";
import {VueCsvImportPlugin} from "vue-csv-import";

createApp(App)
    .use(VueCsvImportPlugin)
    .mount("#app");

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

<template>
    <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></vue-csv-map>
    </vue-csv-import>
</template>

Components

VueCsvImport

Primary wrapper component.

<template>
    <vue-csv-import
        v-model="csv"
        :fields="{
            name: {
                required: false,
                label: 'Name'
            },
            age: {
                required: true,
                label: 'Age'
            }
        }"
    >
        <!-- Other Components -->
    </vue-csv-import>
</template>

Props:

PropDefaultDescription
fieldsnull(required) The field names used to map the CSV.
textsee below(optional) Override the default text used in the component.
modelValueN/A(optional) Binds to the mapped CSV object.

Default text

{
    errors: {
        fileRequired: 'A file is required',
        invalidMimeType: "Invalid file type"
    },
    toggleHeaders: 'File has headers',
    submitBtn: 'Submit',
    fieldColumn: 'Field',
    csvColumn: 'Column'
}

Slot Props:

PropDescription
fileThe selected file
errorsCurrent errors
fieldsThe fields object

VueCsvToggleHeaders

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

<template>
    <vue-csv-import>
        ...
        <vue-csv-toggle-headers></vue-csv-toggle-headers>
        ...
    </vue-csv-import>
</template>

Or with custom markup:

<template>
    <vue-csv-import>
        ...
        <vue-csv-toggle-headers v-slot="{hasHeaders, toggle}">
            <button @click.prevent="toggle">Has Headers</button>
        </vue-csv-toggle-headers>
        ...
    </vue-csv-import>
</template>

Props:

PropDefaultDescription
checkboxAttributes{}(optional) Attributes to bind to the checkbox.
labelAttributes{}(optional) Attributes to bind to the label.

Slot Props:

PropDescription
hasHeadersWhether CSV is marked as having headers.
toggleToggle the 'hasHeaders' value.

VueCsvInput

The file field for importing CSV.

<template>
    <vue-csv-import>
        ...
        <vue-csv-input name="file"></vue-csv-input>
        ...
    </vue-csv-import>
</template>

Or with custom markup:

<template>
    <vue-csv-import>
        ...
        <vue-csv-input v-slot="{file, change}">
            ...
        </vue-csv-input>
        ...
    </vue-csv-import>
</template>

Props:

PropDefaultDescription
nameN/A(required) The field names used to map the CSV.
headerstrue(optional) Override the default text used in the component.
parseConfigN/A(optional) Papaparse config object.
validationtrue(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:

PropDescription
fileThe current file object
changeChange the file

VueCsvMap

Component to map the CSV to the specified fields.

<template>
    <vue-csv-import>
        ...
        <vue-csv-map></vue-csv-map>
        ...
    </vue-csv-import>
</template>

Or use slot for custom markup:

<template>
    <vue-csv-import>
        ...
        <vue-csv-map v-slot="{sample, map, fields}">
            ...
        </vue-csv-map>
        ...
    </vue-csv-import>
</template>

Props:

PropDefaultDescription
noTheadfalse(optional) Attributes to bind to the checkbox.
selectAttributes{}(optional) Attributes to bind to the select fields.
autoMatchtrue(optional) Auto-match fields to columns when they share the same name
autoMatchIgnoreCasetrue(optional) Ignore case when auto-matching

Slot Props:

PropDescription
sampleThe first row of the CSV.
mapThe currently mapped fields.
fieldsThe fields.

VueCsvSubmit

Displays a button to post the CSV to specified URL.

<template>
    <vue-csv-import>
        ...
        <vue-csv-submit url="/post/here"></vue-csv-submit>
        ...
    </vue-csv-import>
</template>

Or use slot for custom markup:

<template>
    <vue-csv-import>
        <vue-csv-submit v-slot="{submit, mappedCsv}">
            <button @click.prevent="submit">Submit!!</button>
        </vue-csv-submit>
    </vue-csv-import>
</template>

Props:

PropDefaultDescription
urlN/A(required) Where to post the CSV.

Slot Props:

PropDescription
submitSubmit the CSV (POST)
mappedCsvThe mapped CSV object

VueCsvErrors

Displays any error messages.

<template>
    <vue-csv-import>
        ...
        <vue-csv-errors></vue-csv-errors>
        ...
    </vue-csv-import>
</template>

Or use slot for custom markup:

<template>
    <vue-csv-import>
        ...
        <vue-csv-errors v-slot="{errors}">
            ...
        </vue-csv-errors>
        ...
    </vue-csv-import>
</template>

Slot Props:

PropDescription
errorsObject containing errors

Testing

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

4.1.2

3 years ago

4.1.0

3 years ago

4.1.1

3 years ago

3.3.10

3 years ago

4.0.5

3 years ago

4.0.6

3 years ago

3.3.9

3 years ago

3.3.8

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.3.7

3 years ago

3.3.5

4 years ago

3.3.4

4 years ago

3.3.3

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

2.3.9

5 years ago

2.3.7

5 years ago

2.3.6

5 years ago

2.5.0

5 years ago

2.3.5

5 years ago

2.3.4

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.0.0

6 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

0.0.1

6 years ago