- Home
- Forum
- Angular - EJ 2
- Set cell background color dynamically
Set cell background color dynamically
Hello,
I have a grid that only shows four columns of a five columns Object Array. Depending on the value of the fifth column's cell I want to put second column's cell to one color or to a different one. Here it is what I have done until now.
<ejs-grid #grid [dataSource]='data' [enableHover]='false' [allowSelection]='false' (queryCellInfo)='customiseCell($event)'>
<e-columns>
<e-column field='Element' headerText='ELEMENT' textAlign='Right' width=90></e-column>
<e-column field='Value' headerText='VALUE' textAlign='Right' width=90></e-column>
<e-column field='Description' headerText='DESCRIPTION' textAlign='Right' width=90></e-column>
<e-column field='Reason' headerText='REASON' textAlign='Right' width=90></e-column>
</e-columns>
</ejs-grid>
ngOnInit() {
this.data = [
{ Element: 'GnNrE IEPE02', Value: '35', Description: 'AIOO POWER', Reason: '-', Status: 0 },
{ Element: 'GnNE IEPE03', Value: '55', Description: 'AIOI CURRENT', Reason: 'Vibration', Status: 1},
{ Element: 'GnNre_A IEPE04', Value: '88', Description: 'FCIOO SPEED', Reason: '-', Status: 2}
];
}
customiseCell(args: QueryCellInfoEventArgs) {
if (args.column.field === 'Value') {
if (args.data['Status'] === 0) {
args.cell.classList.add('background-color', 'green');
} else if (args.data['Status'] === 1) {
args.cell.classList.add('background-color', 'yellow');
} else {
args.cell.classList.add('background-color', 'red');
}
}
}
Thank you in advance
SIGN IN To post a reply.
5 Replies
MS
Madhu Sudhanan P
Syncfusion Team
January 24, 2019 10:26 AM UTC
Hi Ane,
Thanks for contacting Syncfusion support.
In the code example given you have added style in the classList property of the cell hence the style was not applied. Now we have created three CSS rules to hold the background color and added the class names in the queryCellInfo event as follows.
|
@Component({
selector: 'control-content',
templateUrl: 'default.html',
styles: [`
.background-green{
background-color: green;
}
.background-yellow {
background-color: yellow;
}
.background-red{
background-color: red;
}
`]
})
export class DefaultComponent implements OnInit {
.....
customiseCell(args: any) {
if (args.column.field === 'Value') {
if (args.data['Status'] === 0) {
args.cell.classList.add('background-green');
} else if (args.data['Status'] === 1) {
args.cell.classList.add('background-yellow');
} else {
args.cell.classList.add('background-red');
}
}
}
}
|
Regards,
Madhu Sudhanan P
AN
Ane
January 24, 2019 11:33 AM UTC
Thank you!
MS
Madhu Sudhanan P
Syncfusion Team
January 24, 2019 11:45 AM UTC
Hi Ane,
Thanks for the update. We are glad that your requirement has been achieved.
Regards,
Madhu
JM
Jody Mark Petroni
June 19, 2019 02:46 AM UTC
How do you do this on the Child rows?
HJ
Hariharan J V
Syncfusion Team
June 19, 2019 12:30 PM UTC
Hi Jody,
Thanks for your update.
we have validated your requirement. You can bind querycellInfo event to the childGrid also and apply the styles.We have prepared a sample for your reference. Please find the below code snippet and sample for your reference.
|
App.component.html
<ejs-grid #Grid id='Grid' [dataSource]='parentData' (queryCellInfo)='customiseCell($event)' [childGrid]='childGrid'>
. . .
</ejs-grid>
App.component.css
.background-green{
background-color: green;
}
.background-yellow {
background-color: yellow;
}
.background-red{
background-color: red;
}
App.component.ts
import { Component, OnInit } from '@angular/core';
import { employeeData, orderDatas, customerData } from './data';
import { DetailRowService,QueryCellInfoEventArgs } from '@syncfusion/ej2-angular-grids';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css'] ,
providers: [DetailRowService],
})
export class AppComponent {
public parentData: Object[];
public childGrid: any;
public secondChildGrid: any;
ngOnInit(): void {
this.parentData = employeeData;
this.childGrid = {
dataSource: orderDatas,
queryString: 'EmployeeID',
allowPaging: true,
queryCellInfo: this.childGridQuerycell,
pageSettings: {pageSize: 6, pageCount: 5},
columns: [
{ field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120 },
{ field: 'ShipCity', headerText: 'Ship City', width: 120 },
{ field: 'Freight', headerText: 'Freight', width: 120 },
{ field: 'ShipName', headerText: 'Ship Name', width: 150 }
],
};
}
customiseCell (args: QueryCellInfoEventArgs) {
if (args.column.field === 'FirstName') {
if (args.data['EmployeeID'] % 2 === 0) {
args.cell.classList.add('background-green');
} else {
args.cell.classList.add('background-red');
}
}
}
childGridQuerycell (args: QueryCellInfoEventArgs) {
if (args.column.field === 'Freight') {
if (args.data['OrderID'] % 2 === 0) {
args.cell.classList.add('background-green'); //here you can apply styles for childgrid cells
} else {
args.cell.classList.add('background-red');
}
}
}
}
|
Please get back to us, if you need further assistance.
Regards,
Hariharan
SIGN IN To post a reply.
- 5 Replies
- 4 Participants
-
AN Ane
- Jan 24, 2019 09:53 AM UTC
- Jun 19, 2019 12:30 PM UTC