We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Set cell background color dynamically

Thread ID:

Created:

Updated:

Platform:

Replies:

142271 Jan 24,2019 09:53 AM UTC Jun 19,2019 12:30 PM UTC Angular - EJ 2 5
loading
Tags: Grid
Ane
Asked On January 24, 2019 09:53 AM UTC

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

Madhu Sudhanan P [Syncfusion]
Replied On 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 


Ane
Replied On January 24, 2019 11:33 AM UTC

Thank you!

Madhu Sudhanan P [Syncfusion]
Replied On January 24, 2019 11:45 AM UTC

Hi Ane, 
Thanks for the update. We are glad that your requirement has been achieved. 
Regards, 
Madhu 


Jody Mark Petroni
Replied On June 19, 2019 02:46 AM UTC

How do you do this on the Child rows?

Hariharan J V [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon