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. Image for the cookie policy date

set background color for selected row

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

thank you

5 Replies

HJ Hariharan J V Syncfusion Team 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
 



TT ttArbor 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, 



HJ Hariharan J V Syncfusion Team 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 



TT ttArbor May 7, 2019 02:12 PM UTC

works great! thank you


HJ Hariharan J V Syncfusion Team 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


Loader.
Live Chat Icon For mobile
Up arrow icon