- Home
- Forum
- Angular - EJ 2
- Grid grouping: Set background color for the grouped headers
Grid grouping: Set background color for the grouped headers
Hello,
I want to set different background color for the grouped columns, like this image.
I tried using headerCellInfo event, but it sets color for grid column headers only. I didn't find any other event in the documentation.
Also in the documentation "https://ej2.syncfusion.com/angular/documentation/grid/how-to/customize-column-styles/" is not showing any content.
Please help.
Thank you,
Nikitha R
SIGN IN To post a reply.
3 Replies
RS
Rajapandiyan Settu
Syncfusion Team
March 19, 2020 12:20 PM UTC
Hi Nikitha,
Greetings from syncfusion support.
Query : I want to set different background color for the grouped columns,
From your query we could see that you need to style the each level of grouped rows with different colors. We have achieved your requirement by dynamically adding a custom class ("customclass" + "_" + j ( j refers grouped columns length) )to the grouped row cells in the Grid’s dataBound event. By using these custom class we have set the different background color. Please refer the below code example and sample for more information.
|
App.component.html
<ejs-grid #grid [dataSource]='data' [allowPaging]='true' [allowGrouping]='true'
[groupSettings]='groupOptions' (dataBound)="dataBound($event)">
<e-columns>
------------
</e-columns>
</ejs-grid>
<style>
// level 1 grouped row
.customclass_0{
background : lightgreen;
}
// level 2 grouped row
.customclass_1{
background : yellow;
}
// level 3 grouped row
.customclass_2{
background : lightblue;
}
// add the custom class as you need
.customclass_3{
----
}
------
</style>
App.component.ts
export class AppComponent implements OnInit {
@ViewChild('grid',{static:true}) public grid: GridComponent;
public data: object[];
public groupOptions: GroupSettingsModel;
ngOnInit(): void {
this.data = hierarchyOrderdata;
this.groupOptions = { columns: ['OrderID','CustomerID', 'ShipCity'] };
}
dataBound(args) {
var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0];
// get the grouped columns
var groupcol = grid.groupModule.groupSettings.columns;
//get the rows
var rows = grid.getRows();
var grouprows = grid.element.getElementsByClassName("e-recordplusexpand ")
for (var i = 0; i < rows.length; i++) {
for (var j = 0; j < groupcol.length; j++) {
// dynamically adding a custom class up to the grouped columns length
rows[i].getElementsByClassName("e-indentcell")[j].classList.add("customclass" + "_" + j);
// j refers the grouped columns length
}
}
for (var i = 0; i < grouprows.length; i++) {
for (var j = 0; j < groupcol.length; j++) {
if (grouprows[i].closest('tr').getElementsByClassName("e-indentcell").length == j) {
// dynamically adding a custom class up to the grouped columns length
grouprows[i].classList.add("customclass" + "_" + j);
var cells = grouprows[i].closest('tr').children
for (var k = 0; k < cells.length; k++) {
if (!cells[k].classList.contains('e-indentcell')) {
// dynamically adding a custom class up to the grouped columns length
cells[k].classList.add("customclass" + "_" + j)
}
}
var groupindent = grouprows[i].closest('tr').getElementsByClassName("e-indentcell");
for (var k = 0; k < groupindent.length; k++) {
// dynamically adding a custom class up to the grouped columns length
groupindent[k].classList.add("customclass" + "_" + k)
}
}
}
}
}
}
|
Please get back to us if you need further assistance on this.
Regards,
Rajapandiyan S.
NI
Nikitha
March 23, 2020 06:55 AM UTC
Hello Rajapandiyan,
Thank you for replying.
It worked perfectly.
RS
Rajapandiyan Settu
Syncfusion Team
March 24, 2020 04:34 AM UTC
Hi Nikitha,
We are glad that the provided solution is resolved your requirement.
Please get back to us if you need further assistance on this.
Regards,
Rajapandiyan S.
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
NI Nikitha
- Mar 18, 2020 09:18 AM UTC
- Mar 24, 2020 04:34 AM UTC