diff --git a/src/views/datatable/DataTableDoc.vue b/src/views/datatable/DataTableDoc.vue index cdc661722..e64131b9f 100755 --- a/src/views/datatable/DataTableDoc.vue +++ b/src/views/datatable/DataTableDoc.vue @@ -1122,17 +1122,16 @@ export default { editor defines how the editing is implemented, below example demonstrates two cases. In the first example, simple v-model editors are utilized. This is pretty straightforward in most cases. On the other hand, second example is more advanced to consider validations and ability to revert values with the escape key.
-<h3>Basic Cell Editing</h3>
-<p>Simple editors with v-model.</p>
-<DataTable :value="cars1" editMode="cell">
+<h5>Cell Editing</h5>
+<DataTable :value="cars" editMode="cell" @cell-edit-complete="onCellEditComplete">
<Column field="vin" header="Vin">
<template #editor="slotProps">
- <InputText v-model="slotProps.data[slotProps.column.field]" />
+ <InputText v-model="slotProps.data[slotProps.field]" />
</template>
</Column>
<Column field="year" header="Year">
<template #editor="slotProps">
- <InputText v-model="slotProps.data[slotProps.column.field]" />
+ <InputText v-model="slotProps.data[slotProps.field]" />
</template>
</Column>
<Column field="brand" header="Brand">
@@ -1149,17 +1148,7 @@ export default {
</Column>
<Column field="color" header="Color">
<template #editor="slotProps">
- <InputText v-model="slotProps.data[slotProps.column.field]" />
- </template>
- </Column>
-</DataTable>
-
-<h3>Advanced Cell Editing</h3>
-<p>Custom implementation with validations, dynamic columns and reverting values with the escape key.</p>
-<DataTable :value="cars2" editMode="cell" @cell-edit-complete="onCellEditComplete">
- <Column v-for="col of columns" :field="col.field" :header="col.header" :key="col.field">
- <template #editor="slotProps">
- <InputText :value="slotProps.data[slotProps.column.field]" @input="onCellEdit($event, slotProps)" />
+ <InputText v-model="slotProps.data[slotProps.field]" />
</template>
</Column>
</DataTable>
@@ -1173,11 +1162,7 @@ import Vue from 'vue';
export default {
data() {
return {
- cars1: null,
- cars2: null,
- cars3: null,
- editingCellRows: {},
- columns: null,
+ cars: null,
brands: [
{brand: 'Audi', value: 'Audi'},
{brand: 'BMW', value: 'BMW'},
@@ -1194,45 +1179,27 @@ export default {
carService: null,
created() {
this.carService = new CarService();
-
- this.columns = [
- {field: 'vin', header: 'Vin'},
- {field: 'year', header: 'Year'},
- {field: 'brand', header: 'Brand'},
- {field: 'color', header: 'Color'}
- ];
},
methods: {
onCellEditComplete(event) {
- if (!this.editingCellRows[event.index]) {
- return;
- }
-
- const editingCellValue = this.editingCellRows[event.index][event.field];
+ let { data, newValue, field } = event;
switch (event.field) {
case 'year':
- if (this.isPositiveInteger(editingCellValue))
- Vue.set(this.cars2, event.index, this.editingCellRows[event.index]);
+ if (this.isPositiveInteger(newValue))
+ data[field] = newValue;
else
event.preventDefault();
break;
default:
- if (editingCellValue.trim().length > 0)
- Vue.set(this.cars2, event.index, this.editingCellRows[event.index]);
+ if (newValue.trim().length > 0)
+ data[field] = newValue;
else
event.preventDefault();
break;
}
},
- onCellEdit(newValue, props) {
- if (!this.editingCellRows[props.index]) {
- this.editingCellRows[props.index] = {...props.data};
- }
-
- this.editingCellRows[props.index][props.column.field] = newValue;
- },
isPositiveInteger(val) {
let str = String(val);
str = str.trim();
@@ -1245,8 +1212,7 @@ export default {
}
},
mounted() {
- this.carService.getCarsSmall().then(data => this.cars1 = data);
- this.carService.getCarsSmall().then(data => this.cars2 = data);
+ this.carService.getCarsSmall().then(data => this.cars = data);
}
}
@@ -1256,8 +1222,7 @@ export default {
since editingRows is two-way binding enabled, you may use it to initially display one or more rows in editing more or programmatically toggle row editing.
<h3>Row Editing</h3>
-<DataTable :value="cars" editMode="row" dataKey="vin" v-model:editingRows="editingRows"
- @row-edit-init="onRowEditInit" @row-edit-cancel="onRowEditCancel">
+<DataTable :value="cars" editMode="row" dataKey="vin" v-model:editingRows="editingRows" @row-edit-save="onRowEditSave">
<Column field="vin" header="Vin"></Column>
<Column field="year" header="Year">
<template #editor="slotProps">
@@ -1291,19 +1256,15 @@ export default {
}
},
carService: null,
- originalRows: null,
created() {
this.carService = new CarService();
-
- this.originalRows = {};
},
methods: {
- onRowEditInit(event) {
- this.originalRows[event.index] = {...this.cars3[event.index]};
+ onRowEditSave(event) {
+ let { newData, index } = event;
+
+ this.cars[index] = newData;
},
- onRowEditCancel(event) {
- Vue.set(this.cars3, event.index, this.originalRows[event.index]);
- }
},
mounted() {
this.carService.getCarsSmall().then(data => this.cars = data);
diff --git a/src/views/datatable/DataTableEditDemo.vue b/src/views/datatable/DataTableEditDemo.vue
index fd60ad71f..2bf279d4f 100755
--- a/src/views/datatable/DataTableEditDemo.vue
+++ b/src/views/datatable/DataTableEditDemo.vue
@@ -12,46 +12,12 @@
- Basic Cell Editing
- Simple editors with v-model.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- {{slotProps.option.label}}
-
-
-
-
- {{getStatusLabel(slotProps.data.inventoryStatus)}}
-
-
-
-
-
-
-
-
-
-
-
- Advanced Cell Editing
- Custom implementation with validations, dynamic columns and reverting values with the escape key.
-
+ Cell Editing
+ Validations, dynamic columns and reverting values with the escape key.
+
-
-
+
+
@@ -59,21 +25,20 @@
Row Editing
-
+
-
-
+
+
-
-
+
+
-
-
+
+
{{slotProps.option.label}}
@@ -84,8 +49,8 @@
-
-
+
+
@@ -103,12 +68,10 @@ import ProductService from '../../service/ProductService';
export default {
data() {
return {
- editingCellRows: {},
editingRows: [],
columns: null,
products1: null,
products2: null,
- products3: null,
statuses: [
{label: 'In Stock', value: 'INSTOCK'},
{label: 'Low Stock', value: 'LOWSTOCK'},
@@ -121,46 +84,12 @@ export default {
- Basic Cell Editing
- Simple editors with v-model.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- {{slotProps.option.label}}
-
-
-
-
- {{getStatusLabel(slotProps.data.inventoryStatus)}}
-
-
-
-
-
-
-
-
-
-
-
- Advanced Cell Editing
- Custom implementation with validations, dynamic columns and reverting values with the escape key.
-
+ Cell Editing
+ Validations, dynamic columns and reverting values with the escape key.
+
-
-
+
+
@@ -168,21 +97,20 @@ export default {
Row Editing
-
+
-
-
+
+
-
-
+
+
-
-
+
+
{{slotProps.option.label}}
@@ -193,14 +121,14 @@ export default {
-
-
+
+
-
+
-
+
@@ -604,4 +447,4 @@ export default {
padding-top: 0;
padding-bottom: 0;
}
-
\ No newline at end of file
+