import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit, ViewEncapsulation } from'@angular/core';import { Subscription } from'rxjs';import { DikeDateColumnDef, DikeGridColumnDef, DikeGridDataSourceInput, DikeTextColumnDef, DikeGroupColumnDef } from'@dikesoft/angular-data-grid';import { DikeGridProperties } from'app/core/config/dike-grid.properties';import { Employee } from'app/mock-api/common/employees/data.model';import { SampleData } from'app/services/sample-data.service';import { DikeGridConfig } from'app/services/dike-grid.config.service';@Component({ selector:'column-grouping', templateUrl:'./column-grouping.component.html', styleUrls: ['./column-grouping.component.scss'], encapsulation:ViewEncapsulation.None, changeDetection:ChangeDetectionStrategy.OnPush})exportclassColumnGroupingComponentimplementsOnInit,OnDestroy { dkgDataSource:DikeGridDataSourceInput<Employee>; gridProperties:DikeGridProperties;private changeGridPropertiesSubscription:Subscription=Subscription.EMPTY;constructor(private cdr:ChangeDetectorRef,private gridConfig:DikeGridConfig,private sampleData:SampleData) { }onColumnDefInstance(columnDef:DikeGridColumnDef<Employee>):void {// Define the Email column:constemailColumn=newDikeTextColumnDef<Employee>('email','Email');emailColumn.width =250;// Define the Hire Date column:consthireDateColumn=newDikeDateColumnDef<Employee>('hireDate','Hire Date');hireDateColumn.width =120;// Define the Employee Id column:constemployeeIdColumn=newDikeTextColumnDef<Employee>('employeeId','Employee Id');employeeIdColumn.width =350;// Lastly, define the Employee Info column group:constemployeeInfo=newDikeGroupColumnDef('employeeInfoGroup','Employee Info');employeeInfo.order =2;// Add columns to the column group:employeeInfo.children = [ employeeIdColumn, emailColumn, hireDateColumn ];// Then, add the colum group to the DikeGridComponent instance:columnDef.addColumns(employeeInfo); }ngOnInit():void {this.dkgDataSource =this.sampleData.getEmployees(1000);// Listening to any config property change:this.setChangeGridPropertiesSubscription(); }ngOnDestroy():void {this.changeGridPropertiesSubscription.unsubscribe(); }privatesetChangeGridPropertiesSubscription():void {this.changeGridPropertiesSubscription.unsubscribe();this.changeGridPropertiesSubscription =this.gridConfig.configChange.subscribe((props:DikeGridProperties) => {this.gridProperties = props;this.cdr.markForCheck(); }); }}
As you can see in the previous definition, we create the column groups in the HTML definition and TypeScript code, as we define any column.
You must not provide the dataType property because column groups can hold any number of columns, even column groups.
Be aware when you create a column group in the TypeScritpt code, the children property is null.
Group Status
Any group could be open or closed. The property displayStatus stores the group status.
If you want a group could open or close, its child columns must tell the grid which columns are visible in closed status.
Let us define another column under the Employee Info group. This column will be visible when the Employee Info column group is closed, displaying the Total Sales.
<dike-grid>...</dike-grid>
<ng-template #totalSales let-value="fieldValue">
<p>{{ value | currency }}</p>
</ng-template>
onColumnDefInstance(columnDef: DikeGridColumnDef<Employee>): void { // Define the Total Sales column:consttotalSalesColumn=newDikeNumericColumnDef<Employee>('totalSales','Total Sales');totalSalesColumn.width =150;totalSalesColumn.displayTemplate =from(Promise.resolve().then(() =>this.totalSalesTpl));totalSalesColumn.displayOn ='closed';// Lastly, define the Employee Info group:constemployeeInfo=newDikeGroupColumnDef('employeeInfoGroup','Employee Info');employeeInfo.order =2;// Add columns to the column group:employeeInfo.children = [ employeeIdColumn, emailColumn, hireDateColumn, totalSalesColumn ];// Then, add the colum group to the DikeGridComponent instance:columnDef.addColumns(employeeInfo);}
In the previous code, we have defined the following:
Firstly, we determined the template for displaying the total sales field.
Then, we create the actual column definition for Total Sales. The displayOn property is equal to closed, meaning that the user will see this value when the Employee Info group is closed.
The groups that can open or close will have a right or left arrow, indicating that they can change their status.
If you do not specify the displayOn property, the default value will be open.
Initial Group Status
By default, the initial status of a group is open. However, you can change this value by providing the closed value to the displayStatus property.
Let us change the initial status for the Employee Info group to a closed value.
column-grouping.component.ts
// Lastly, define the Employee Info group:constemployeeInfo=newDikeGroupColumnDef('employeeInfoGroup','Employee Info');employeeInfo.order =2;employeeInfo.displayStatus ='closed';
When you launch the HTML containing the DikeGrid definition, the Employee Info group will appear closed.
If you set a group as closed and none of the child columns has the displayOn property to a closed value, the group status will be open.
If you set a group as open and all the child columns have the displayOn property to a closed value, the group status will be open.
Group Status Event
When a column group changes its status, the DikeGrid instance emits an event. The name of the event is columnGroupStatusChange.
Let us listen to this group status event from the DikeGrid instance directly.
You can listen to this event from the DikeGridColumnDef service through the columnGroupStatusChange property.
Open the Personal Info group and see the event information in the dev console.
Column Chooser Menu
When you navigate to the Column Chooser Menu and open the Employee Info group, you will see a circle with an accent color next to the Total Sales column.
The circle next to the Total Sales column indicates that the column is visible when the parent group is closed.
Summary
In this section, we explored column groups creation and its status. Then, we listen to the event sent when this status has changed and spot a column displayed in a closed position.