diff --git a/src/components/datatable/BodyCell.vue b/src/components/datatable/BodyCell.vue index d22ed4aef..fe9068547 100644 --- a/src/components/datatable/BodyCell.vue +++ b/src/components/datatable/BodyCell.vue @@ -88,7 +88,7 @@ export default { if (!this.documentEditListener) { this.documentEditListener = (event) => { if (this.isOutsideClicked(event)) { - this.switchCellToViewMode(); + this.completeEdit(event, 'outside'); } }; @@ -108,27 +108,39 @@ export default { isOutsideClicked(event) { return !this.$el.contains(event.target) && !this.$el.isSameNode(event.target); }, - onClick() { + onClick(event) { if (this.isEditable() && !this.editing) { this.editing = true; - this.bindDocumentEditListener(); + this.$emit('edit-init', {originalEvent: event, data: this.rowData, field: this.column.field, index: this.index}); + } + }, + completeEdit(event, type) { + let editEvent = {originalEvent: event, data: this.rowData, field: this.column.field, index: this.index, type: type, preventDefault: () => event.preventDefault()}; + this.$emit('edit-complete', editEvent); + + if (!event.defaultPrevented) { + this.switchCellToViewMode(); } }, onKeyDown(event) { switch (event.which) { case 13: + this.completeEdit(event, 'enter'); + break; + case 27: this.switchCellToViewMode(); + this.$emit('edit-cancel', {originalEvent: event, data: this.rowData, field: this.column.field, index: this.index}); break; case 9: + this.completeEdit(event, 'tab'); + if (event.shiftKey) this.moveToPreviousCell(event); else this.moveToNextCell(event); - - this.switchCellToViewMode(); break; } }, @@ -202,6 +214,9 @@ export default { else { return null; } + }, + isEditingCellValid() { + return (DomHandler.find(this.$el, '.p-invalid').length === 0); } }, computed: { diff --git a/src/components/datatable/DataTable.d.ts b/src/components/datatable/DataTable.d.ts index 556c30533..7a240cea4 100644 --- a/src/components/datatable/DataTable.d.ts +++ b/src/components/datatable/DataTable.d.ts @@ -44,6 +44,7 @@ export declare class DataTable extends Vue { expandedRowGroups?: any[]; stateStorage?: string; stateKey?: string; + editMode?: string; $emit(eventName: 'page', event: Event): this; $emit(eventName: 'sort', event: Event): this; $emit(eventName: 'filter', event: Event): this; @@ -56,6 +57,9 @@ export declare class DataTable extends Vue { $emit(eventName: 'row-collapse', event: Event): this; $emit(eventName: 'rowgroup-expand', event: Event): this; $emit(eventName: 'rowgroup-collapse', event: Event): this; + $emit(eventName: 'edit-init', event: Event): this; + $emit(eventName: 'edit-complete', event: Event): this; + $emit(eventName: 'edit-cancel', event: Event): this; $slots: { header: VNode[]; paginatorLeft: VNode[]; diff --git a/src/components/datatable/DataTable.vue b/src/components/datatable/DataTable.vue index 856c5cfba..7e1dd6983 100644 --- a/src/components/datatable/DataTable.vue +++ b/src/components/datatable/DataTable.vue @@ -71,7 +71,8 @@ + :rowspan="rowGroupMode === 'rowspan' ? calculateRowGroupSize(dataToRender, col, index) : null" + @edit-init="onEditInit" @edit-complete="onEditComplete" @edit-cancel="onEditCancel" /> @@ -303,6 +304,10 @@ export default { stateKey: { type: String, default: null + }, + editMode: { + type: String, + default: null } }, data() { @@ -1557,6 +1562,15 @@ export default { let headers = DomHandler.find(this.$refs.table, '.p-datatable-thead > tr > th'); headers.forEach((header, index) => header.style.width = widths[index] + 'px'); } + }, + onEditInit(event) { + this.$emit('edit-init', event); + }, + onEditComplete(event) { + this.$emit('edit-complete', event); + }, + onEditCancel(event) { + this.$emit('edit-cancel', event); } }, computed: { diff --git a/src/views/datatable/DataTableEditDemo.vue b/src/views/datatable/DataTableEditDemo.vue index 0df752033..24dcee76f 100644 --- a/src/views/datatable/DataTableEditDemo.vue +++ b/src/views/datatable/DataTableEditDemo.vue @@ -10,8 +10,9 @@
-

Cell Editing

- +

Basic Cell Editing

+

Simple editors with v-model.

+ -