|
| 1 | +import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { |
| 3 | + IgxDialogComponent, |
| 4 | + IgxGridComponent, |
| 5 | + Transaction |
| 6 | +} from 'igniteui-angular'; |
| 7 | +import { DATA } from './../../data/nwindData'; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: 'app-grid-excel-style-editing-sample', |
| 11 | + styleUrls: [`grid-editing-excel-style.component.scss`], |
| 12 | + templateUrl: 'grid-editing-excel-style.component.html' |
| 13 | +}) |
| 14 | +export class GridExcelStyleEditingComponent implements OnInit { |
| 15 | + @ViewChild('grid', { read: IgxGridComponent, static: true }) |
| 16 | + public grid: IgxGridComponent; |
| 17 | + |
| 18 | + public data: any[]; |
| 19 | + |
| 20 | + public ngOnInit(): void { |
| 21 | + this.data = DATA; |
| 22 | + } |
| 23 | + |
| 24 | + constructor(private cdr:ChangeDetectorRef){} |
| 25 | + |
| 26 | + public keydownHandler(event) { |
| 27 | + const key = event.keyCode; |
| 28 | + const grid = this.grid; |
| 29 | + const activeElem = grid.navigation.activeNode; |
| 30 | + |
| 31 | + if ( |
| 32 | + (key >= 48 && key <= 57) || |
| 33 | + (key >= 65 && key <= 90) || |
| 34 | + (key >= 97 && key <= 122) |
| 35 | + ) { |
| 36 | + // Number or Alphabet upper case or Alphabet lower case |
| 37 | + |
| 38 | + const columnName = grid.getColumnByVisibleIndex(activeElem.column).field; |
| 39 | + const cell = grid.getCellByColumn(activeElem.row, columnName); |
| 40 | + if (cell && !grid.crudService.cellInEditMode) { |
| 41 | + grid.crudService.enterEditMode(cell); |
| 42 | + cell.editValue = event.key; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (key == 13) { |
| 47 | + let thisRow = activeElem.row; |
| 48 | + const column = activeElem.column; |
| 49 | + const rowInfo = grid.dataView; |
| 50 | + |
| 51 | + let nextRow = this.getNextEditableRowIndex(thisRow, rowInfo, event.shiftKey); |
| 52 | + |
| 53 | + this.grid.navigateTo(nextRow, column, (obj) => { |
| 54 | + obj.target.activate(); |
| 55 | + this.grid.clearCellSelection(); |
| 56 | + this.cdr.detectChanges(); |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public activeNodeChange() { |
| 62 | + this.grid.clearCellSelection(); |
| 63 | + this.grid.endEdit(); |
| 64 | + } |
| 65 | + |
| 66 | + public getNextEditableRowIndex(currentRowIndex, dataView, previous){ |
| 67 | + if (currentRowIndex < 0 || (currentRowIndex === 0 && previous) || (currentRowIndex >= dataView.length - 1 && !previous)) { |
| 68 | + return currentRowIndex; |
| 69 | + } |
| 70 | + if(previous){ |
| 71 | + return dataView.findLastIndex((rec, index) => index < currentRowIndex && this.isEditableDataRecordAtIndex(index, dataView)); |
| 72 | + } |
| 73 | + return dataView.findIndex((rec, index) => index > currentRowIndex && this.isEditableDataRecordAtIndex(index, dataView)); |
| 74 | + } |
| 75 | + |
| 76 | + private isEditableDataRecordAtIndex(dataViewIndex, dataView) { |
| 77 | + const rec = dataView[dataViewIndex]; |
| 78 | + return !rec.expression && !rec.summaries && !rec.childGridsData && !rec.detailsData |
| 79 | + } |
| 80 | +} |
0 commit comments