Text Formatting In Kendo UI Grid With Angular

In this blog, we will learn how to add custom data in a specified formatted text in Kendo Grid using Angular 6.
 
There are some descriptions of format sections:
  1. Check all in the header. 
  2. Anchor Tag in the column.
  3. Normal Text.
  4. Add Custom Class and Style.
  5. Date Format.
  6. If else condition in the column.
  7. Tooltip.
  8. Text box filter outside of grid.
I have an API which returns a bunch of data as JSON. And I have attached API data into my grid with the help of the below code.
  1. this._qService.getList(_officeID).subscribe(  
  2. data => {  
  3.    this.gridData = data  
  4. });  
In my UI part, I have a kendo grid.
  1. <kendo-grid [data]="gridData" [pageSize]="state.take"  
  2. [skip]="state.skip" [sort]="state.sort" [filter]="state.filter" [sortable]="true" [pageable]="true"  
  3. filterable="menu" (dataStateChange)="dataStateChange($event)" [selectable]="{enabled: true, checkboxOnly: true}"  
  4. [kendoGridSelectBy]="'ID'" [selectedKeys]="mySelection" (selectedKeysChange)="onSelectedKeysChange($event)">  
Now, let us start.
 
Check all in the header.
  1. <kendo-grid-checkbox-column width="40">  
  2.    <ng-template kendoGridHeaderTemplate>  
  3.       <input class="k-checkbox" id="selectAllCheckboxId" kendoGridSelectAllCheckbox [state]="selectAllState"  
  4.       (selectAllChange)="onSelectAllChange($event)" />  
  5.       <label *ngIf="!filter" class="k-checkbox-label" for="selectAllCheckboxId"></label>  
  6.    </ng-template>  
  7. </kendo-grid-checkbox-column>  
Anchor Tag in column.
  1. <kendo-grid-column field="Name" title="Applicant Name">  
  2.    <ng-template kendoGridCellTemplate let-dataItem="dataItem">  
  3.       <a href="customUrl" target="_blank">{{dataItem.Name}}</a>  
  4.    </ng-template>  
  5. </kendo-grid-column>  
If you want to show normal text in column (without any format)
  1. <kendo-grid-column field="Name" title="Name">  
  2. </kendo-grid-column>   
Add custom style and color code ifrom API:
  1. <kendo-grid-column field="Status" title="Status">  
  2.    <ng-template kendoGridCellTemplate let-dataItem="dataItem">  
  3.       <span [ngStyle]="{'color': dataItem.StatusColor}"> {{dataItem.Status}} </span>  
  4.    </ng-template>  
  5. </kendo-grid-column>  
  6. with static color:  
  7. <ng-template kendoGridCellTemplate let-dataItem="dataItem">  
  8.    <a [ngStyle]="{'color': dataItem.Comments? '#009900':'#333'}">{{data form ts file}}</a>  
  9. </ng-template>  
Date Format
  1. <kendo-grid-column field="Log" title="Date" filter="date" format="{0:d}">  
  2. </kendo-grid-column>   
If condition inside column
  1. <kendo-grid-column title="Feedback">  
  2. <ng-template kendoGridCellTemplate let-dataItem="dataItem">  
  3. <a *ngIf="dataItem.Keyword=='CMP'" href="{{data form ts file}}" target="_blank">Download</a>  
  4. </ng-template>  
  5. </kendo-grid-column>  
If you want to add tooltip on grid then use this code just outside of Kendo Grid
  1. <ng-template #template let-anchor>  
  2.    <span>{{ anchor.nativeElement.innerText }}</span>  
  3. </ng-template>  
  4. <div kendoTooltip  
  5.    showOn="none"  
  6.    [tooltipTemplate]="template"  
  7.    filter=".k-grid td"  
  8.    (mouseover)="showTooltip($event)">  
  9.    //// your kendo Grid should be here..  
  10. </div>  
Text box filter outside of grid
  1. <input type="text" [(ngModel)]="filter" (ngModelChange)="onFilterchange()" placeholder="Search any text from Kendo Grid" />   
In TS file
  1. private onFilterchange() {  
  2.    this.filter = this.filter.toLowerCase();  
  3.    this.mySelection = [];  
  4.    this.selectAllState = 'unchecked';  
  5.    let _gridSource = this.gridSource;  
  6.    this.gridData = process(_gridSource.filter(r => r.Name.toLowerCase().includes(this.filter) || r.Subject.includes(this.filter) || r.LastName.toLowerCase().includes(this.filter) || r.FistName.toLowerCase().includes(this.filter) || r.ContactEmail.toLowerCase().includes(this.filter) || r.ContactNumber.includes(this.filter) || r.MiddleName.toLowerCase().includes(this.filter)), this.state);  
  7. //}  
  8. }