Skip to content

Commit 07c588d

Browse files
Use row classes when applying custom styling to pasted data. (#941)
* Use row classes when applying custom styling to pasted data. * Minor fixes. * Minor fix 2. * fix: ensure correct row addition and update in paste functionality --------- Co-authored-by: Martin Dragnev <mddragnev@gmail.com>
1 parent 5c58585 commit 07c588d

File tree

5 files changed

+51
-82
lines changed

5 files changed

+51
-82
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//begin eventHandler
2+
igRegisterScript("WebGridEditedRowClassesHandler", () => {
3+
return {
4+
edited: (row) => updatedRecsPK.indexOf(row.data[grid.primaryKey]) !== -1
5+
};
6+
}, true);
7+
//end eventHandler
8+
9+
let requiredStyles = `
10+
<!--begin styles-->
11+
.edited {
12+
font-style: italic;
13+
color: gray;
14+
}
15+
<!--end styles-->
16+
`;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//begin imports
2+
import { IgcRowType } from "igniteui-webcomponents-grids/grids";
3+
import { IgcGridComponent } from 'igniteui-webcomponents-grids/grids';
4+
//end imports
5+
6+
import { CodeGenHelper } from 'igniteui-webcomponents-core';
7+
8+
export class WebGridEditedRowClassesHandler {
9+
//begin eventHandler
10+
public webGridEditedRowClassesHandler = {
11+
edited: (row: IgcRowType) => {
12+
const grid = CodeGenHelper.getDescription<IgcGridComponent>("content") as any;
13+
return this.updatedRecsPK.indexOf(row.data[grid.primaryKey]) !== -1;
14+
}
15+
};
16+
//end eventHandler
17+
18+
public requiredStyles = `
19+
<!--begin styles-->
20+
.edited {
21+
font-style: italic;
22+
color: gray;
23+
}
24+
<!--end styles-->
25+
`;
26+
}

code-gen-library/WebGridPasteFromExcel/Blazor.js

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function onWebGridPasteFromExcelKeyDown(eventArgs) {
1616
var txtArea;
1717
var textArea = getTextArea();
1818
var pasteMode = "Paste starting from active cell";
19+
var updatedRecsPK = [];
1920

2021
function getTextArea() {
2122
if(!txtArea) {
@@ -61,7 +62,6 @@ function onPaste(eventArgs) {
6162
const grid = document.getElementById("grid");
6263
const columns = grid.visibleColumns;
6364
const pk = grid.primaryKey;
64-
const addedData = [];
6565
for (const curentDataRow of processedData) {
6666
const rowData = {};
6767
for (const col of columns) {
@@ -70,22 +70,12 @@ function onPaste(eventArgs) {
7070
}
7171
// generate PK
7272
rowData[pk] = grid.data.length + 1;
73+
updatedRecsPK.push(rowData[pk]);
7374
grid.addRow(rowData);
74-
addedData.push(rowData);
7575
}
7676
// scroll to last added row
7777
grid.navigateTo(grid.data.length - 1, 0, () => {
78-
clearStyles();
79-
for (const data of addedData) {
80-
const row = grid.getRowByKey(data[pk]);
81-
if (row) {
82-
const rowNative = getNative(row);
83-
if (rowNative) {
84-
rowNative.style["font-style"] = "italic";
85-
rowNative.style.color = "gray";
86-
}
87-
}
88-
}
78+
grid.cdr.detectChanges();
8979
});
9080
}
9181
function updateRecords(processedData) {
@@ -97,7 +87,6 @@ function onPaste(eventArgs) {
9787
const columns = grid.visibleColumns;
9888
const cellIndex = grid.visibleColumns.indexOf(cell.column);
9989
let index = 0;
100-
const updatedRecsPK = [];
10190
for (const curentDataRow of processedData) {
10291
const rowData = {};
10392
const dataRec = grid.data[rowIndex + index];
@@ -117,36 +106,10 @@ function onPaste(eventArgs) {
117106
grid.addRow(rowData);
118107
continue;
119108
}
120-
grid.updateRow(rowData, rowPkValue);
121109
updatedRecsPK.push(rowPkValue);
110+
grid.updateRow(rowData, rowPkValue);
122111
index++;
123112
}
124-
125-
clearStyles();
126-
for (const pkVal of updatedRecsPK) {
127-
const row = grid.getRowByKey(pkVal);
128-
if (row) {
129-
const rowNative = getNative(row);
130-
if (rowNative) {
131-
rowNative.style["font-style"] = "italic";
132-
rowNative.style.color = "gray";
133-
}
134-
}
135-
}
136-
}
137-
138-
function clearStyles() {
139-
const rows = [...(document.getElementsByTagName("igx-grid-row"))];
140-
for (const rowNative of rows) {
141-
rowNative.style["font-style"] = "";
142-
rowNative.style.color = "";
143-
}
144-
}
145-
146-
function getNative(row) {
147-
const rows = [...document.getElementsByTagName("igx-grid-row")];
148-
const dataInd = row.index.toString();
149-
return rows.find(x => x.attributes["data-rowindex"] .value === dataInd);
150113
}
151114

152115
function processData(data) {

code-gen-library/WebGridPasteFromExcel/Web.ts

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class WebGridPasteFromExcel {
2323

2424
private txtArea: any;
2525
public pasteMode = "Paste starting from active cell";
26+
public updatedRecsPK: any[] = [];
2627

2728
public get textArea() {
2829
if(!this.txtArea) {
@@ -68,7 +69,6 @@ export class WebGridPasteFromExcel {
6869
const grid = CodeGenHelper.getDescription<IgcGridComponent>("content") as any;
6970
const columns = grid.visibleColumns;
7071
const pk = grid.primaryKey;
71-
const addedData: any[] = [];
7272
for (const curentDataRow of processedData) {
7373
const rowData = {} as any;
7474
for (const col of columns) {
@@ -77,22 +77,12 @@ export class WebGridPasteFromExcel {
7777
}
7878
// generate PK
7979
rowData[pk] = grid.data.length + 1;
80+
this.updatedRecsPK.push(rowData[pk]);
8081
grid.addRow(rowData);
81-
addedData.push(rowData);
8282
}
8383
// scroll to last added row
8484
grid.navigateTo(grid.data.length - 1, 0, () => {
85-
this.clearStyles();
86-
for (const data of addedData) {
87-
const row = grid.getRowByKey(data[pk]);
88-
if (row) {
89-
const rowNative = this.getNative(row) as any;
90-
if (rowNative) {
91-
rowNative.style["font-style"] = "italic";
92-
rowNative.style.color = "gray";
93-
}
94-
}
95-
}
85+
grid.cdr.detectChanges();
9686
});
9787
}
9888
public updateRecords(processedData: any[]) {
@@ -104,7 +94,6 @@ export class WebGridPasteFromExcel {
10494
const columns = grid.visibleColumns;
10595
const cellIndex = grid.visibleColumns.indexOf(cell.column);
10696
let index = 0;
107-
const updatedRecsPK: any[] = [];
10897
for (const curentDataRow of processedData) {
10998
const rowData = {} as any;
11099
const dataRec = grid.data[rowIndex + index];
@@ -124,36 +113,10 @@ export class WebGridPasteFromExcel {
124113
grid.addRow(rowData);
125114
continue;
126115
}
116+
this.updatedRecsPK.push(rowPkValue);
127117
grid.updateRow(rowData, rowPkValue);
128-
updatedRecsPK.push(rowPkValue);
129118
index++;
130119
}
131-
132-
this.clearStyles();
133-
for (const pkVal of updatedRecsPK) {
134-
const row = grid.getRowByKey(pkVal);
135-
if (row) {
136-
const rowNative = this.getNative(row) as any;
137-
if (rowNative) {
138-
rowNative.style["font-style"] = "italic";
139-
rowNative.style.color = "gray";
140-
}
141-
}
142-
}
143-
}
144-
145-
protected clearStyles() {
146-
const rows = [...(document.getElementsByTagName("igx-grid-row") as any)];
147-
for (const rowNative of rows) {
148-
rowNative.style["font-style"] = "";
149-
rowNative.style.color = "";
150-
}
151-
}
152-
153-
protected getNative(row: any) {
154-
const rows = [...(document.getElementsByTagName("igx-grid-row") as any)];
155-
const dataInd = row.index.toString();
156-
return rows.find(x => (x.attributes as any)["data-rowindex"] .value === dataInd);
157120
}
158121

159122
public processData(data: any) {

samples/grids/grid/paste.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"type": "WebGrid",
2828
"dataRef": "InvoicesData",
2929
"renderedRef": "WebGridPasteFromExcel",
30+
"rowClassesRef": "WebGridEditedRowClassesHandler",
3031
"autoGenerate": false,
3132
"name": "grid",
3233
"id": "grid",

0 commit comments

Comments
 (0)