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 background color for selected row

Thread ID:

Created:

Updated:

Platform:

Replies:

144369 May 2,2019 05:36 PM UTC May 8,2019 06:25 AM UTC Angular - EJ 2 5
loading
Tags: Grid
ttArbor
Asked On May 2, 2019 05:36 PM UTC

Is there a way that I can set background color for selected row template in data grid? 

thank you

Hariharan J V [Syncfusion]
Replied On May 3, 2019 05:12 AM UTC

Hi Arbor, 
 
Greetings from Syncfusion. 
 
We suggest you to use `rowDataBound` event to achieve this requirement, please refer the below code snippets and sample. 
 
[app.component.ts] 
public rowDB(args){ 
      if(args.data.EmployeeID < 6){         
        args.row.querySelector('tr').classList.add('bgcolor'); 
      } 
    } 
 
[app.component.html] 
<ejs-grid #grid [dataSource]='data' (rowDataBound)='rowDB($event)' height=335 width='auto'> 
 
Regards,
Hariharan
 


ttArbor
Replied On May 3, 2019 01:12 PM UTC

sorry maybe my question was not very clear. 

I want to set background color upon row click not pre-selected row.  I should have used active-row instead of selected. 

Can you provide me a solution to achieve this? 

Thank you, 


Hariharan J V [Syncfusion]
Replied On May 7, 2019 08:44 AM UTC

Hi Arbor, 
  
From your query, we found that you want to change the row background color while clicking the particular row. You can achieve this requirement like as following way, 
  
rowTemplate: 
  
<ng-template # rowTemplate let-data> 
        <tr class="parenttr"> 
  
           ... 
  
        </tr> 
    </ng-template> 
  
Click event: 
ngOnInit(): void { 
       this.grid.element.addEventListener('click', function(e){ 
            let element = e.target; 
            let row = parentsUntil((element as Element), 'parenttr'); 
            if(!isNullOrUndefined(row)) { 
            let prevSelectedRows = gridInstance.getContent().querySelectorAll('.bgcolor'); 
            for( let i = 0; i < prevSelectedRows.length; i++){ 
              prevSelectedRows[i].classList.remove('bgcolor'); 
            } 
            row.classList.add('bgcolor'); 
            } 
        }) 
    } 
  
We have modified our sample with this requirement and you can find that modified sample in the below link, 
  
  
In this sample, we have added “parenttr” class to all the Grid rows. So we can easily find the Grid row while clicking any row template elements by using this class with Grid parentsUntil method. 
  
Regards, 
Hariharan 


ttArbor
Replied On May 7, 2019 02:12 PM UTC

works great! thank you

Hariharan J V [Syncfusion]
Replied On May 8, 2019 06:25 AM UTC

Hi Arbor,

Thanks for your update.

We are happy to hear that your problem has been resolved.

Please get back to use if you need any further assitance.

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