Edit in Vue Grid component
16 Mar 202324 minutes to read
The Grid component has options to dynamically insert, delete and update records.
Editing feature requires a primary key column for CRUD operations.
To define the primary key, set columns.isPrimaryKey
to true in particular column.
You can start the edit action either by double clicking the particular row or by selecting the required row and click on Edit button in the toolbar. Similarly, you can add a new record to grid either by clicking on Add button in the toolbar or on an external button which is bound to invoke the addRecord
method of the grid, Save and Cancel while in edit mode is possible using respective toolbar icon in grid.
Deletion of the record is possible by selecting the required row and click on Delete button in the toolbar.
To use CRUD, inject the Edit
module in the provide section.
To get start quickly with Edit Options, you can check on this video:
<template>
<div id="app">
<ejs-grid :dataSource='data' :editSettings='editSettings' height='315px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign= 'Right' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true }
};
},
provide: {
grid: [Page, Edit]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
- If
columns.isIdentity
is enabled,
then it will be considered as a read-only column when editing and adding a record.- You can disable editing for a particular column, by specifying
columns.allowEditing
to false.
Toolbar with edit option
The grid toolbar has the built-in items to execute Editing actions.You can define this by using the toolbar
property.
<template>
<div id="app">
<ejs-grid :dataSource='data' :editSettings='editSettings' height='270' :toolbar='toolbar'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign= 'Right' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Toolbar, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel']
};
},
provide: {
grid: [Page, Edit, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Disable editing for particular column
You can disable editing for particular columns by using the columns.allowEditing
.
In the following demo, editing is disabled for the CustomerID column.
<template>
<div id="app">
<ejs-grid :dataSource='data' :editSettings='editSettings' :toolbar='toolbar' height='273px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' :validationRules='orderIDRules' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' :allowEditing= 'false' :validationRules='customerIDRules' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign= 'Right' editType= 'numericedit' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' editType= 'dropdownedit' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Toolbar, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Normal' },
toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],
orderIDRules: { required: true },
customerIDRules: { required: true, minLength: 3 }
};
},
provide: {
grid: [Page, Edit, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Disable editing for a particular row or cell
You can disable the editing for a particular row by using the actionBegin
event of Grid based on requestType
as beginEdit
.
In the below demo, the rows which are having the value for ShipCountry
column as “France” is prevented from editing.
<template>
<div id="app">
<ejs-grid :dataSource='data' :editSettings='editSettings' :toolbar='toolbar' height='273px' :actionBegin = 'actionBegin'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign= 'Right' editType= 'numericedit' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' editType= 'dropdownedit' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Toolbar, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
editSettings: { allowEditing: true },
toolbar: ['Edit', 'Update', 'Cancel']
};
},
methods: {
actionBegin(args) {
if (args.requestType === 'beginEdit') {
if (args.rowData.ShipCountry == "France") {
args.cancel = true;
}
}
}
},
provide: {
grid: [Page, Edit, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
For batch mode of editing, you can use cellEdit
event of Grid. In the below demo, the cells which are having the value as “France” is prevented from editing.
<template>
<div id="app">
<ejs-grid :dataSource='data' :editSettings='editSettings' :toolbar='toolbar' height='273px' :cellEdit = 'cellEdit'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign= 'Right' editType= 'numericedit' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' editType= 'dropdownedit' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Toolbar, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
editSettings: { allowEditing: true, mode: 'Batch' },
toolbar: ['Edit', 'Update', 'Cancel']
};
},
methods: {
cellEdit(args) {
if (args.value == "France") {
args.cancel = true;
}
}
},
provide: {
grid: [Page, Edit, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Editing template column
You can edit template column value by defining field
for that particular column.
In the below demo, the ShipCountry
column is rendered with the template.
<template>
<div id="app">
<ejs-grid :dataSource='data' :editSettings='editSettings' :toolbar='toolbar' height='280px' >
<e-columns>
<e-column field='OrderID' headerText='Order ID' :isPrimaryKey='true' textAlign='Right' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign='Center' format='C2' editType='numericedit' width=80></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :template='editTemplate' editType='dropdownedit' width=120></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin,Toolbar, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data: () => {
return {
data: data,
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],
editTemplate: function () {
return {
template: Vue.component('editOption', {
template: '<a href="#"></a>',
data() { return { data: { data: {} } }; }
})
}
},
};
},
provide: {
grid: [Edit, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Troubleshoot editing works only for first row
The Editing functionalities can be performed based upon the primary key value of the selected row.
If primaryKey is not defined in the grid, then edit or delete action take places the first row.
How to make a Grid column always editable
Make the Grid column always editable using the column template feature of the Grid.
In the following example, the textbox is rendered in the Freight column using a column template. The keyup event for the Grid is bound using the created event of the Grid, and the edited changes are saved in the data source using the updateRow method of the Grid.
<template>
<div id="app">
<ejs-grid ref="grid" :dataSource='data' height='315px' :created = 'created'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' width=120></e-column>
<e-column field='OrderDate' headerText='Order Date' width=130 textAlign='Right' format='yMd'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=120></e-column>
<e-column field='Freight' headerText='Receipt Amount' width=120 :template='cTemplate'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, parentsUntil } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
cTemplate: function () {
return { template : Vue.component('columnTemplate',{
template: `<div class="image">
<input :id='id' :value='value' class='custemp' type='text' style='width: 100%'/>
</div>`,
data: function() {
return {
data: {}
}
},
computed: {
id: function() {
return this.data.OrderID;
},
value: function() {
return this.data.Freight;
}
}
})}
}
};
},
methods: {
created(args) {
this.$refs.grid.element.addEventListener('keyup', function (e) { // Bind the keyup event for the grid.
if (e.target.classList.contains('custemp')) { // Based on this condition, you can find whether the target is an input element or not.
var row = parentsUntil(e.target, 'e-row');
var rowIndex = row.rowIndex; // Get the row index.
var uid = row.getAttribute('data-uid');
var rowData = this.$refs.grid.getRowObjectFromUID(uid).data; // Get the row data.
rowData.Freight = e.target.value; // Update the new value for the corresponding column.
this.$refs.grid.updateRow(rowIndex, rowData); // Update the modified value in the row data.
}
});
}
},
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
See Also
- Cascading DropDownList with Grid Editing
- Dialog editing for Vue Grid using WebAPI adaptor
- Is it possible to hide the tool bar or some of the CRUD operations in Vue Grid?
- How to pass a data from parent to child in Vue Grid
- Need to use different key value while performing delete in Vue Grid
- How to give custom Headertext name and validation in ChildData Vue Grid
- Calculate column value based on another column with filtering in Vue Grid