README
Excel Editor for VUE 2
Vue2 plugin for displaying and editing the array-of-object in Excel style. It supports the following features:
- Excel-like UI
- Real 2-way data binding
- Column filtering
- Column sorting
- Export to Excel/CSV
- Pagination
- Row selection
- Update the cells in all selected rows
- Edit key support: up, down, left, right, page-up, page-down, tab, del, bs, enter, esc
- Ctrl/meta key support: Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-Z, Ctrl-F, Ctrl-G, Ctrl-L
- Column validation
- Cell error tooltip
- Custom column header
- Custom Row style
- Readonly column
- Column visibility
- Column sequence
- Column width adjustment
- Undo update
- Copy & Paste
- Mass import Excel data
Getting started
Get the package:
npm install vue-excel-editor
Register VueExcelEditor in your app entrypoint:
import Vue from 'vue'
import VueExcelEditor from 'vue-excel-editor'
Vue.use(VueExcelEditor)
In your template
<template>
<vue-excel-editor v-model="jsondata">
<vue-excel-column field="user" label="User" />
<vue-excel-column field="name" label="Name" />
<vue-excel-column field="phone" label="Contact" />
<vue-excel-column field="gender" label="Gender" />
<vue-excel-column field="age" label="Age" />
<vue-excel-column field="birth" label="Date Of Birth" />
</vue-excel-editor>
</template>
Outlook

Props List
Prop Component: vue-excel-editor
| Name | Mandatory | Type | Description |
|---|---|---|---|
| v-model | Mandatory | Array | Edited data in Array Of Object |
| page | Optional | Number | Specific page size, default is auto-calculating by screen height |
| no-paging | Optional | Boolean | Disable paging feature, default is false |
| no-num-col | Optional | Boolean | No number column, default is false |
| filter-row | Optional | Boolean | Show fixed filter row, default is false |
| no-footer | Optional | Boolean | No footer row, default is false |
| no-finding | Optional | Boolean | Disable find key (ctrl-f) and finding dialog, default is false |
| no-finding-next | Optional | Boolean | Disable find-next key (ctrl-g), default is false |
| free-select | Optional | Boolean | Select multiple rows without pressing ctrl/meta key |
| autocomplete | Optional | Boolean | Enable autocomplete of all columns, default is false |
| autocomplete-count | Optional | Number | The maximum length of the autocomplete list, default is 50 |
| readonly | Optional | Boolean | Set all columns read only, default is false |
| readonly-style | Optional | Object | The style of the read-only cell |
| height | Optional | String | Define the exact height in px of the component, default is 'auto' |
| width | Optional | String | Define the maximum width in px of the component, default is '100%' |
| row-style | Optional | Function | Conditional row formatting, default is null |
| cell-style | Optional | Function | Conditional cell formatting, default is null |
| header-label | Optional | Function | Func to return the label, parameter are the field label, field object |
| record-label | Optional | Function | Func to return the label, parameters are recordPosition, record object |
| localized-label | Optional | Object | Customize labels and messages for localization purpose |
| n-filter-count | Optional | Number | Number of items to be listed in filter dialog. Default is 200 |
| remember | Optional | Boolean | Remember the setting in localStorage, default is false |
| enterToSouth | Optional | Boolean | Move the cell to bottom instead of right when hits enter |
| allow-add-col | Optional | Boolean | Allow to show the add column button during column resize |
| add-column | Optional | Function | Func to return the column definition when column is adding |
| no-header-edit | Optional | Boolean | Not allow header label editing |
| spellcheck | Optional | Boolean | Turn on spellcheck during editing |
| new-if-bottom | Optional | Boolean | New record if focusing cell reach bottom |
| disable-panel-setting | Optional | Boolean | Hide the setting panel |
| disable-panel-filter | Optional | Boolean | Hide the filter panel |
| no-mouse-scroll | Optional | Boolean | Disable the vertical scrolling by mouse |
Prop Component: vue-excel-column
| Name | Mandatory | Type | Description |
|---|---|---|---|
| field | Mandatory | String | Field name, row object key |
| label | Optional | String | Header label, default is field name |
| type | Optional | String | The column type |
| readonly | Optional | Boolean | Read-only, default is parent prop: readonly |
| init-style | Optional | Object | Cell inital style in css |
| sticky | Optional | Boolean | Fixed column at left of the table, no response on horizontal scrolling |
| invisible | Optional | Boolean | Column visibility, default is false |
| width | Optional | String | Specified column width, default is '100px' |
| change | Optional | Function@ | The function to be triggered when the data of this column changed |
| validate | Optional | Function | The function to validate and return the error message |
| key-field | Optional | Boolean | Specified the key field which is included in keys parameter in @update event |
| allow-keys | Optional | Array, Function | Array of char which allow to input |
| mandatory | Optional | String | If not empty, it is showed as error when it modified as blank, default is '' |
| length-limit | Optional | Number | Not allow to input when the content length reaches the limit |
| autocomplete | Optional | Boolean | Allow autocomplete popup when editing, default is parent prop: autocomplete |
| pos | Optional | Number | Specified column sequence |
| text-transform | Optional | String | Force the input to upppercase or lowercase when editing |
| text-align | Optional | String | Text alignment, default is 'left' |
| options | Optional | Array, Function@ | For type = 'select', define the selectable options in array |
| options | Optional | Object, Function | For type = 'map', define the selectable options in hash |
| summary | Optional | String | Summary: 'sum', 'avg', 'max', 'min'. Default is null |
| sort | Optional | Function | The custom function for sorting the column |
| link | Optional | Function | The function to react to the alt-click on cell text |
| to-text | Optional | Function | The function to convert from object value to edit-text |
| to-value | Optional | Function | The function to convert from edit-text to object value |
@ - Function can return a promise
Column type
| Type | Value | Display text | Justify | Validation | Allow Keys | Allow Null |
|---|---|---|---|---|---|---|
| string | string | string | left | none | all | Y |
| number | numeric | numeric | right | none | -.0123456789 | Y |
| select | array | string | left | value with options | all | Y |
| map | hash | string | left | value with options | all | Y |
| check10 | 1 or 0 | 1 or 0 | center | none | 1 or 0 | Y |
| checkYN | Y or N | Y or N | center | none | ynYN | Y |
| checkTF | T or F | T of F | center | none | tfTF | Y |
| date | yyyy-mm-dd | yyyy-mm-dd | left | valid date | none | Y |
| datetime | yyyy-mm-dd hh:mn | yyyy-mm-dd hh:mn | left | valid datetime | none | Y |
| datetimesec | yyyy-mm-dd hh:mn:ss | yyyy-mm-dd hh:mn:ss | left | valid datetimesec | none | Y |
| datetick | unix timestamp | yyyy-mm-dd | left | valid date | none | Y |
| datetimetick | unix timestamp | yyyy-mm-dd hh:mn | left | valid datetime | none | Y |
| datetimesectick | unix timestamp | yyyy-mm-dd hh:mn:ss | left | valid datetimesec | none | Y |
Hot Key List
| Name | Condition | Description |
|---|---|---|
| Ctrl/Meta A | Table Focus | Select all rows |
| Ctrl/Meta C | Cell Focus | Select the cell text to clipboard |
| Ctrl/Meta V | Cell Focus | Place the clipboard text to cell |
| Ctrl/Meta Z | Table Focus | Undo the last update |
| Ctrl/Meta F | Table Focus | Popup the "Find" dialog |
| Ctrl/Meta G | After Find | Continue to find the text |
| Ctrl/Meta L | Cell Focus | Force to show autocomplete list, or the option list for "select" typed column |
Events List
Event Component: vue-excel-editor
| Name | Arguemnts | Description |
|---|---|---|
| update | updateItemArray | Update cell information |
| delete | deleteItemArray | Delete row information |
| select | selectIdArray, direction | Emit when rows are selected/unselected |
| cell-click | rowPos, colPos | Emit when a cell be clicked before focus |
| cell-focus | {rowPos, colPos, cell, rec} | Emit when a cell got focus |
| page-changed | pageTopPos, pageBottomPos | Emit when the page has changed |
| setting | setting | Emit when setting (column width, invisible state) is changed |
| validate-error | error, row, field | Emit when validation (both field and row level) occured |
Methods List
Method Component: vue-excel-editor
| Name | Arguments | Description |
|---|---|---|
| firstPage | Move to the first page | |
| lastPage | Move to the last page | |
| prevPage | Move to the previous page | |
| nextPage | Move to the next page | |
| moveNorth | Move the cursor cell to upper cell | |
| moveSouth | Move the cursor cell to lower cell | |
| moveWest | Move the cursor cell to previous cell | |
| moveEast | Move the cursor cell to next cell | |
| moveTo | row, col* | Move the cursor cell to cell(row, col) |
| moveToNorthWest | Move the cursor cell to 1st row 1st col | |
| moveToNorthEast | Move the cursor cell to 1st row last col | |
| moveToSouthWest | Move the cursor cell to last row 1st col | |
| moveToSouthEast | Move the cursor cell to last row last col | |
| doFind | text | Find the specified text in whole table and locate the cursor cell |
| doFindNext | Contnue the last find | |
| sort | n, pos | Sort the column specified by pos, n = 1 (ascending) or -1 (descending) |
| newRecord | rec* | Call this to new an empty record, return the rec pointer |
| deleteRecord | rowpos | Delete the record in pos rowpos |
| deleteSelectedRecords | Delete all the selected records | |
| selectRecord | row | Select the row |
| selectRecordByKeys | keys | Select the row by keys hash |
| selectRecordById | id | Select the row by $id |
| unSelectRecord | row | UnSelect the row |
| clearAllSelected | Unselect all selected rows | |
| getSelectedRecords | Get an array of the selected row hash | |
| exportTable | fmt* | Export the filtered table as xlsx/csv |
| importTable | callback* | Import the specified formatted xlsx |
| undoTransaction | Undo the last update | |
| setFilter | name, text | Set the filter text on column name |
| clearFilter | name* | Clear the filter text on column name |
| columnSuppress | Hide the column if all values are null or empty | |
| calSummary | Calculate the summary of all columns | |
| getFieldByName | name | Get the field object by name |
| getFieldByLabel | label | Get the field object by label |
| setRowError | error, row | Set the row validation error |
| setFieldError | error, row, field | Set the row validation error |
* = optional argument
Variable List
Variable Component: vue-excel-editor
| Name | Type | Description |
|---|---|---|
| processing | Boolean | Component is busy or not |
| pageTop | Number | The top row number of the current page |
| pageSize | Number | The number of rows of each page |
| fields | AOO | It contains the column spec create when mount |
| filterColumn | Object | Contains the current filters, developer can access the filter string via this |
| table | AOO | It contains the filtered records |
| selected | Object | Contains all the selected rows, the key is row number and the value is internal $id |
| selectedCount | Number | Number of rows are selected |
| errmsg | Object | Contains all the validation error messages, the key is internal $id plus field name |
| redo | AOA | The buffer of undo, it will be removed after undo or table changed |
AOA = Array of Array, i.e. [[...], [...]]
AOO = Array of Object, i.e. [{...}, {...}]
I suppose you try to read all the above variables only. Do not try to modify any value of the above variables, unless you deeply walk through the codes and know the consequences.
Example
An example to show 5x6 table:
<template>
<vue-excel-editor v-model="jsondata" filter-row>
<vue-excel-column field="user" label="User ID" type="string" width="80px" />
<vue-excel-column field="name" label="Name" type="string" width="150px" />
<vue-excel-column field="phone" label="Contact" type="string" width="130px" />
<vue-excel-column field="gender" label="Gender" type="select" width="50px" :options="['F','M','U']" />
<vue-excel-column field="age" label="Age" type="number" width="70px" />
<vue-excel-column field="birth" label="Date Of Birth" type="date" width="80px" />
</vue-excel-editor>
</template>
Note that the component will read the vue-excel-column when it is created. You may also skip all the column definitions. The control will help you to "guess" the rest:
<template>
<vue-excel-editor v-model="jsondata" filter-row />
</template>
The sample data contains 5 records:
export default {
name: 'app',
data: {
jsondata: [
{user: 'hc', name: 'Harry Cole', phone: '1-415-2345678', gender: 'M', age: 25, birth: '1997-07-01'},
{user: 'sm', name: 'Simon Minolta', phone: '1-123-7675682', gender: 'M', age: 20, birth: '1999-11-12'},
{user: 'ra', name: 'Raymond Atom', phone: '1-456-9981212', gender: 'M', age: 19, birth: '2000-06-11'},
{user: 'ag', name: 'Mary George', phone: '1-556-1245684', gender: 'F', age: 22, birth: '2002-08-01'},
{user: 'kl', name: 'Kenny Linus', phone: '1-891-2345685', gender: 'M', age: 29, birth: '1990-09-01'}
]
}
}
Work with redis for saving
You may capture the @delete and the @update event for saving purpose.
<vue-excel-editor v-model="jsondata" @delete="onDelete" @update="onUpdate">
<vue-excel-column field="user" label="User ID" type="string" width="80px" key-field />
...
</vue-excel-editor>
Specified "key-field" is necessary, it will reflect in rec.keys in the following:
methods: {
onDelete (records) {
records = records.map(rec => ['del', rec.keys.join()])
redis.multi(records).exec()
},
onUpdate (records) {
records = records.map(rec => ['hset', rec.keys.join(), rec.name, rec.newVal])
redis.multi(records).exec()
}
}
New row
Set the reference by ref="..."
<vue-excel-editor ref="grid" v-model="jsondata">
<vue-excel-column field="user" label="User ID" type="string" width="80px" key-field />
...
</vue-excel-editor>
methods: {
newRecord () {
const rec = {
user: 'nm',
name: 'Norman Morris',
phone: '1-222-3333333',
gender: 'M',
age: 28,
birth: '1993-05-16'
}
// Call this to new record
this.$refs.grid.newRecord(rec)
}
}
After the record created, a set of @update events will be fired. If you undo a newRecord transaction, component will generate the corresponding @delete events. In case you do not care about the undo, you may skip this by appending the new record in v-model variable (jsondata array) directly.
Delete row
methods: {
delRecord () {
this.$refs.grid.deleteRecord(0) // delete the 1st record: Harry Cole
}
}
The component will generate the corresponding @delete events. You may also interest in the deleteSelectedRecords() method.
Remember the grid setting
The grid setting such as column width and column label can be saved in the localStorage of client browser by specified "remember" prop:
<template>
<vue-excel-editor v-model="jsondata" remember>
...
</vue-excel-editor>
</template>
You may also capture the @setting event to handle more specifics.
Change the column label
You may specify the column label in vue-excel-column label prop. However, it will persist after mounted. If you want to change the column label after mounted, you may try to update the variable fields. For example
this.$refs.grid.fields.forEach((field) => {
if (field.name === 'col23') field.label = 'Product'
if (field.label === '') field.label = '(' + field.name + ')'
})
this.$forceUpdate() // remember to call vue update to reflesh the display
Change the column invisibility
Same as column label, you may make the column visible/invisible in vue-excel-column label prop. However, it will persist after mounted. If you want to change it after mounted, you may try to update the variable fields. For example
this.$refs.grid.fields.forEach((field) => {
if (field.name === 'col23') field.invisible = false
})
this.$forceUpdate() // remember to call vue update to reflesh the display
Export the content
The following provides the button to export the grid content.
<template>
<button @click="exportAsExcel"> Export Excel </button>
<button @click="exportAsCsv"> Export CSV </button>
<vue-excel-editor ref="grid" ...>
...
</vue-excel-editor>
</template>
methods: {
exportAsExcel () {
const format = 'xlsx'
const exportSelectedOnly = true
const filename = 'test'
this.$refs.grid.exportTable(format, exportSelectedOnly, filename)
}
exportAsCsv () {
const format = 'csv'
const exportSelectedOnly = true
const filename = 'test'
this.$refs.grid.exportTable(format, exportSelectedOnly, filename)
}
}
Note that only xlsx format supports compression.
Do something when user select/unselect the rows
The selected or unselected rows will be passed to the provided trigger method
<template>
<vue-excel-editor v-model="jsondata" @select="onSelect">
...
</vue-excel-editor>
</template>
methods: {
onSelect (selectedRows) {
console.log(selectedRows)
}
}
You may also want to watch the selected records count for displaying the action buttons. For example:
<button v-show="showDeleteAction"> Delete </button>
<button v-show="showSendEmailInvitationAction"> Invite </button>
<button v-show="showSendBirthdayGreetingAction"> Greeting </button>
computed: {
showDeleteAction () {
return this.$refs.grid.selectedCount > 0 // Show if any records selected
},
showSendEmailInvitationAction () {
return this.$refs.grid.selectedCount === 1 // Show if single record is selected
},
showSendBirthdayGreetingAction () {
// Show only if any selected people birthday matched today
if (this.$refs.grid.selectedCount > 0) {
return this.$refs.grid.getSelectedRecords().filter(item => item.birth === today).length > 0
else
return false
}
}
Do something when user change a cell content
You could achieve this by placing the method in change prop
<vue-excel-column field="name" label="Name" type="string" width="150px" :change="onBeforeNameChange" />
<vue-excel-column field="phone" label="Contact" type="string" width="130px" :change="onBeforePhoneChange" />
<vue-excel-column field="birth" label="Date Of Birth" type="date" width="80px" :change="onBeforeBirthChange" />
methods: {
onBeforeNameChange (...args) {
console.log(...args) // show all the arguments: newVal, oldVal, oldRow, field
},
onBeforePhoneChange (newVal) {
// This example demonstrate how to ensure the uniqueness of the phone number
if (this.jsondata.findIndex(row => row.phone === newVal) >= 0)
return false // return false to reject the update
},
onBeforeBirthChange (newVal, oldVal, row) {
row.age = moment().diff(newVal, 'years') // calculate the age if birth is changed
}
}
The change function can return a promise.
<vue-excel-column field="phone" label="Contact" type="string" width="130px" :change="onBeforePhoneChange" />
methods: {
onBeforePhoneChange (newVal) {
return new Promise((done) => {
axios.post('checkPhoneNumber', {
phone: newVal
})
.then(done(true))
.catch((e) => {
console.error(e)
done(false)
})
})
}
}
However, the promise routine may cause your web page has low performance. I suggest you use a validation prop to show the wrong content in a grid with the validation error.
Other Features
<template>
<vue-excel-editor v-model="jsondata" no-paging autocomplete filter-row>
<vue-excel-column field="user" label="User ID" type="string" width="80px" key-field />
<vue-excel-column field="name" label="Name" type="string" width="150px" />
<vue-excel-column field="phone" label="Contact" type="string" width="130px" :validate="validPhoneNum" />
<vue-excel-column field="gender" label="Gender" type="select" width="50px" :options="['F','M','U']" />
<vue-excel-column field="age" label="Age" type="number" width="70px" />
<vue-excel-column field="birth" label="Date Of Birth" type="date" width="80px" />
</vue-excel-editor>
</template>
Filter + Footer Rows
Specified filter-row prop to show filter row
<vue-excel-editor v-model="jsondata" filter-row>
If you don't want to show the footer, specified the no-footer prop
<vue-excel-editor v-model="jsondata" no-footer filter-row>

Filtering
The filtering is one of the focusing features. It supports regular expression and windows wild card syntax.

Component supports the prefx likes <, >, =, >=, <=, <>, ~ (regular expression) and wild-card * and ? symbol. Examples: | Example | Description | | :--- | :--- | | abc | The values contain ABC | | abc* | The values start by ABC | | abc | The values end by ABC | | =abc | The values equal to ABC | | <>abc | The values do not equal to ABC | | >= 100 | The values are greater or equal to 100 | | < 0 | The values are smaller than 0 | | po-185?? | The values start from PO-18 and the 3rd-last char is 5 | | = | The values are empty | | ~. | The values are not empty | | ~.tpx[ ]+ck | The values have TPX and CK text and they have spaces in between | | ~[ ] | The values contain space | | ~^so|ary$ | The values start by SO or end by ARY | | ~[ ]+







