|
| 1 | +import { Component, Directive, HostListener, ViewChild, ViewEncapsulation} from "@angular/core"; |
| 2 | +import { |
| 3 | + CloseScrollStrategy, |
| 4 | + ConnectedPositioningStrategy, |
| 5 | + HorizontalAlignment, |
| 6 | + IgxDropDownComponent, |
| 7 | + IgxGridComponent, |
| 8 | + VerticalAlignment |
| 9 | +} from "igniteui-angular"; |
| 10 | + |
| 11 | +import { PasteHandler} from "./paste-handler.directive"; |
| 12 | + |
| 13 | +import { LOCAL_DATA } from "./data"; |
| 14 | + |
| 15 | +import { take } from "rxjs/operators"; |
| 16 | + |
| 17 | +@Component({ |
| 18 | + encapsulation: ViewEncapsulation.None, |
| 19 | + selector: "app-grid-paste-sample", |
| 20 | + styleUrls: ["./grid-paste-sample.component.scss"], |
| 21 | + templateUrl: "./grid-paste-sample.component.html" |
| 22 | +}) |
| 23 | +export class GridPasteSampleComponent { |
| 24 | + @ViewChild("grid1", { read: IgxGridComponent }) public grid1: IgxGridComponent; |
| 25 | + @ViewChild(IgxDropDownComponent) public igxDropDown: IgxDropDownComponent; |
| 26 | + public data; |
| 27 | + public comboData = [ |
| 28 | + "Paste data as new records", |
| 29 | + "Paste starting from active cell" |
| 30 | + ]; |
| 31 | + public pasteMode = this.comboData[0]; |
| 32 | + private _positionSettings = { |
| 33 | + horizontalStartPoint: HorizontalAlignment.Left, |
| 34 | + verticalStartPoint: VerticalAlignment.Bottom |
| 35 | + }; |
| 36 | + private _overlaySettings = { |
| 37 | + closeOnOutsideClick: true, |
| 38 | + modal: false, |
| 39 | + positionStrategy: new ConnectedPositioningStrategy(this._positionSettings), |
| 40 | + scrollStrategy: new CloseScrollStrategy() |
| 41 | + }; |
| 42 | + |
| 43 | + constructor() { |
| 44 | + this.data = LOCAL_DATA; |
| 45 | + } |
| 46 | + |
| 47 | + public ngOnInit() { |
| 48 | + this.grid1.verticalScrollContainer.onChunkLoad.pipe().subscribe(() => { |
| 49 | + if (this.grid1.rowList) { |
| 50 | + this.clearStyles(); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public toggleDropDown(eventArgs) { |
| 56 | + this._overlaySettings.positionStrategy.settings.target = eventArgs.target; |
| 57 | + this.igxDropDown.toggle(this._overlaySettings); |
| 58 | + } |
| 59 | + |
| 60 | + public selectionChange(eventArgs) { |
| 61 | + this.pasteMode = eventArgs.newSelection.elementRef.nativeElement.textContent.trim(); |
| 62 | + } |
| 63 | + |
| 64 | + public dataPasted(processedData) { |
| 65 | + if (this.pasteMode === "Paste data as new records") { |
| 66 | + this.addRecords(processedData); |
| 67 | + } else { |
| 68 | + this.updateRecords(processedData); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public addRecords(processedData: any[]) { |
| 73 | + const columns = this.grid1.visibleColumns; |
| 74 | + const pk = this.grid1.primaryKey; |
| 75 | + const addedData = []; |
| 76 | + for (const curentDataRow of processedData) { |
| 77 | + const rowData = {}; |
| 78 | + for (const col of columns) { |
| 79 | + rowData[col.field] = curentDataRow[col.visibleIndex]; |
| 80 | + } |
| 81 | + // generate PK |
| 82 | + rowData[pk] = this.grid1.data.length + 1; |
| 83 | + this.grid1.addRow(rowData); |
| 84 | + addedData.push(rowData); |
| 85 | + this.grid1.cdr.detectChanges(); |
| 86 | + } |
| 87 | + // scroll to last added row |
| 88 | + this.grid1.verticalScrollContainer.scrollTo(this.grid1.data.length); |
| 89 | + |
| 90 | + this.grid1.verticalScrollContainer.onChunkLoad.pipe(take(1)).subscribe(() => { |
| 91 | + this.clearStyles(); |
| 92 | + for (const data of addedData) { |
| 93 | + const row = this.grid1.getRowByKey(data[pk]); |
| 94 | + if (row) { |
| 95 | + row.nativeElement.style["font-style"] = "italic"; |
| 96 | + row.nativeElement.style.color = "gray"; |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + public updateRecords(processedData: any[]) { |
| 102 | + const cell = this.grid1.selectedCells[0]; |
| 103 | + const pk = this.grid1.primaryKey; |
| 104 | + if (!cell) { return; } |
| 105 | + const rowIndex = cell.row.index; |
| 106 | + // const rowPkValue = cell.row.rowData[pk]; |
| 107 | + const cellIndex = cell.column.visibleIndex; |
| 108 | + const columns = this.grid1.visibleColumns; |
| 109 | + let index = 0; |
| 110 | + const updatedRecsPK = []; |
| 111 | + for (const curentDataRow of processedData) { |
| 112 | + const rowData = {}; |
| 113 | + const dataRec = this.grid1.data[rowIndex + index]; |
| 114 | + const rowPkValue = dataRec ? dataRec[pk] : this.grid1.data.length + 1; |
| 115 | + rowData[pk] = rowPkValue; |
| 116 | + for (let j = 0; j < columns.length; j++) { |
| 117 | + let currentCell; |
| 118 | + if (j >= cellIndex) { |
| 119 | + currentCell = curentDataRow.shift(); |
| 120 | + } |
| 121 | + const colKey = columns[j].field; |
| 122 | + rowData[colKey] = currentCell || (!!dataRec ? dataRec[colKey] : null); |
| 123 | + } |
| 124 | + if (!dataRec) { |
| 125 | + // no rec to update, add instead |
| 126 | + rowData[pk] = rowPkValue; |
| 127 | + this.grid1.addRow(rowData); |
| 128 | + continue; |
| 129 | + } |
| 130 | + this.grid1.updateRow(rowData, rowPkValue); |
| 131 | + this.grid1.cdr.detectChanges(); |
| 132 | + updatedRecsPK.push(rowPkValue); |
| 133 | + index++; |
| 134 | + } |
| 135 | + |
| 136 | + this.clearStyles(); |
| 137 | + for (const pkVal of updatedRecsPK) { |
| 138 | + const row = this.grid1.getRowByKey(pkVal); |
| 139 | + if (row) { |
| 140 | + row.nativeElement.style["font-style"] = "italic"; |
| 141 | + row.nativeElement.style.color = "gray"; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + protected clearStyles() { |
| 147 | + for (const row of this.grid1.rowList.toArray()) { |
| 148 | + row.nativeElement.style["font-style"] = ""; |
| 149 | + row.nativeElement.style.color = ""; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments