Search results

Frozen Rows and Columns in Angular TreeGrid component

29 Nov 2022 / 5 minutes to read

Frozen rows and columns

Frozen rows and columns provides an option to make rows and columns always visible in the top and left side of the tree grid while scrolling.

In this demo, the frozenColumns is set as ‘2’ and the frozenRows is set as ‘3’. Hence, the left two columns and top three rows are frozen.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { FreezeService, TreeGridComponent } from '@syncfusion/ej2-angular-treegrid';
import { sampleData } from './datasource';

@Component({
    selector: 'app-container',
    template: `<ejs-treegrid [dataSource]='data' childMapping='subtasks' [treeColumnIndex]='1' height='310' [frozenColumns]='2' [frozenRows]='3' [allowSelection]='false'>
        <e-columns>
            <e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
            <e-column field='taskName' headerText='Task Name' width='230'></e-column>
            <e-column field='startDate' headerText='Start Date' width='120' format='yMd' textAlign='Right'></e-column>
            <e-column field='endDate' headerText='End Date' width='120' format='yMd' textAlign='Right'></e-column>
            <e-column field='duration' headerText='Duration' width='110' textAlign='Right'></e-column>
            <e-column field='progress' headerText='Progress' width='110' textAlign='Right'></e-column>
            <e-column field='priority' headerText='Priority' width='110'></e-column>
            <e-column field='approved' headerText='Approved' textAlign='Left' width='110'></e-column>
        </e-columns>
    </ejs-treegrid>`,
providers: [FreezeService]
})
export class AppComponent implements OnInit {

    public data: Object[];

    ngOnInit(): void {
        this.data = sampleData;
    }
}
Copied to clipboard
import { NgModule,ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TreeGridModule } from '@syncfusion/ej2-angular-treegrid';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TreeGridModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Freeze particular columns

You can use isFrozen property to freeze selected columns in tree grid.

In this demo, the columns with field name taskName and startDate is frozen using the isFrozen property.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { FreezeService, TreeGridComponent } from '@syncfusion/ej2-angular-treegrid';
import { sampleData } from './datasource';

@Component({
    selector: 'app-container',
    template: `<ejs-treegrid [dataSource]='data' childMapping='subtasks' height='310' [allowSelection]='false'>
        <e-columns>
            <e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
            <e-column field='taskName' headerText='Task Name' width='200' isFrozen= 'true'></e-column>
            <e-column field='startDate' headerText='Start Date' isFrozen= 'true' width='150' format='yMd' textAlign='Right'></e-column>
            <e-column field='endDate' headerText='End Date' width='150' format='yMd' textAlign='Right'></e-column>
            <e-column field='duration' headerText='Duration' width='110' textAlign='Right'></e-column>
            <e-column field='progress' headerText='Progress' width='110' textAlign='Right'></e-column>
            <e-column field='priority' headerText='Priority' width='110'></e-column>
            <e-column field='approved' headerText='Approved' textAlign='Left' width='110'></e-column>
        </e-columns>
    </ejs-treegrid>`,
providers: [FreezeService]
})
export class AppComponent implements OnInit {

    public data: Object[];

    ngOnInit(): void {
        this.data = sampleData;
    }
}
Copied to clipboard
import { NgModule,ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TreeGridModule, ToolbarService, SelectionService, EditService } from '@syncfusion/ej2-angular-treegrid';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TreeGridModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ToolbarService, 
        SelectionService,
        EditService]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Freeze direction

You can freeze the tree grid columns on the left or right side by using the column.freeze property and the remaining columns will be movable. The grid will automatically move the columns to the left or right position based on the column.freeze value.

Types of the column.freeze directions:

  • Left: Allows you to freeze the columns at the left.
  • Right: Allows you to freeze the columns at the right.

In this demo, the Task Name column is frozen at the left and the Priority column is frozen at the right side of the content table.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { FreezeService, TreeGridComponent } from '@syncfusion/ej2-angular-treegrid';
import { sampleData } from './datasource';

@Component({
selector: 'app-container',
template: `<ejs-treegrid [dataSource]='data' childMapping='subtasks' height='310' [treeColumnIndex]='1' [allowSelection]='false'>
    <e-columns>
        <e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
        <e-column field='taskName' headerText='Task Name' width='200' freeze='Left'></e-column>
        <e-column field='startDate' headerText='Start Date' width='150' format='yMd' textAlign='Right'></e-column>
        <e-column field='endDate' headerText='End Date' width='150' format='yMd' textAlign='Right'></e-column>
        <e-column field='duration' headerText='Duration' width='110' textAlign='Right'></e-column>
        <e-column field='progress' headerText='Progress' width='110' textAlign='Right'></e-column>
        <e-column field='priority' headerText='Priority' width='110' freeze='Right'></e-column>
        <e-column field='approved' headerText='Approved' textAlign='Left' width='110'></e-column>
    </e-columns>
</ejs-treegrid>`,
providers: [FreezeService]
})
export class AppComponent implements OnInit {

public data: Object[];

ngOnInit(): void {
    this.data = sampleData;
}
}
Copied to clipboard
import { NgModule,ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TreeGridModule, ToolbarService, SelectionService, EditService } from '@syncfusion/ej2-angular-treegrid';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TreeGridModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ToolbarService, 
        SelectionService,
        EditService]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Limitations of frozen tree grid

The following features are not supported in frozen rows and columns:

  • Row Template
  • Detail Template
  • Cell Editing

Limitations of freeze direction

This feature has the following limitations, along with the above mentioned frozen tree grid limitations.

  • Infinite scroll cache mode.
  • Freeze direction in the stacked header is not compatible with column reordering.

Add validation rule for frozen tree grid

In a frozen column-enabled tree grid, the content will be separated into frozen and movable parts. The following code is used to dynamically add validation to input fields in the movable part. In the actionComplete event args, find the movableform instance as an argument. Here, add the validation rules dynamically.

Copied to clipboard
  actionComplete: (args: DialogEditEventArgs) => {
    if ((args.requestType === 'beginEdit' || args.requestType === 'add')) {
        // Add Validation Rules
        args.movableForm.ej2_instances[0].addRules('duration', { max: 200 }); // Here, 'duration' is the column name.
    }
}

Validation rules for the ‘duration’ and ‘taskID’ columns can be added in the following sample.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { TreeGrid, Selection, Edit, Toolbar, DialogEditEventArgs, EditSettingsModel, FreezeService } from '@syncfusion/ej2-angular-treegrid';
import { Component, OnInit } from '@angular/core';
import { sampleData } from './datasource';

@Component({
selector: 'app-container',
template: `<ejs-treegrid [dataSource]='data' childMapping='subtasks' height='310' [treeColumnIndex]='0' [toolbar]='toolbar' [editSettings]='editSettings' allowSelection='false' enableHover='false' (actionComplete)="actionComplete($event)">
    <e-columns>
        <e-column field='taskID' headerText='Task ID' [validationRules]='validationrules' width='90' textAlign='Right'></e-column>
        <e-column field='taskName' headerText='Task Name' width='200' freeze='Left'></e-column>
        <e-column field='duration' headerText='Duration' [validationRules]='validationrules' width='110' textAlign='Right'></e-column>
        <e-column field='startDate' headerText='Start Date' width='150' format='yMd' textAlign='Right'></e-column>
        <e-column field='endDate' headerText='End Date' width='150' format='yMd' textAlign='Right'></e-column>
        <e-column field='progress' headerText='Progress' width='110' textAlign='Right'></e-column>
        <e-column field='priority' headerText='Priority' width='110' freeze='Right'></e-column>
        <e-column field='approved' headerText='Approved' textAlign='Left' width='110'></e-column>
    </e-columns>
</ejs-treegrid>`,
providers: [FreezeService]
})
export class AppComponent implements OnInit {

public data: object[];
public toolbar: string[];
public editSettings: EditSettingsModel;
public validationrules: Object;

ngOnInit(): void {
    this.data = sampleData;
    this.toolbar = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
    this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Row' };
    this.validationrules = { required: true };
}

actionComplete(args) {
    if (args.requestType === 'beginEdit' || args.requestType === 'add') {
        // Add Validation Rules
        args.movableForm.ej2_instances[0].addRules('duration', { max: 200 });
        args.movableForm.ej2_instances[0].addRules('taskID', { max: 20 });
    }
}
}
Copied to clipboard
import { NgModule,ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TreeGridModule, ToolbarService, SelectionService, EditService } from '@syncfusion/ej2-angular-treegrid';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TreeGridModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [ToolbarService, 
        SelectionService,
        EditService]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

This is applicable when a frozen column is enabled and the edit mode is set as ”Row” in the tree grid. You can refer to Syncfusion Angular Tree Grid feature tour page for its groundbreaking feature representations. Also, explore Syncfusion Angular Tree Grid example to know how to present and manipulate data.