Customizing templates in Angular Listview component
27 Apr 202413 minutes to read
The ListView component is designed to customize list items, group title and header title.
Header template
ListView header can be customized with the help of the headerTemplate
property.
To customize header template in your application, set your customized template element inside ng tag directive along with showHeader
property as true
to display the ListView header.
In the following example, we have rendered Listview with customized header which contains search, add and sort buttons.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ListViewModule } from '@syncfusion/ej2-angular-lists'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, ViewChild } from '@angular/core';
@Component({
imports: [
ListViewModule,ButtonModule
],
standalone: true,
selector: 'my-app',
template: `
<div id = 'flat-list'>
<!-- ListView element -->
<ejs-listview id='element' [dataSource]='data' showHeader='true'>
<ng-template #headerTemplate let-data="">
<div class="headerContainer"><span class="fruitHeader">Fruits</span><button ejs-button id="search" iconCss='e-icons e-search-icon' cssClass='e-small e-round' isPrimary='true'></button><button ejs-button id="add" iconCss='e-icons e-add-icon' cssClass='e-small e-round' isPrimary='true'></button><button ejs-button id="sort" iconCss='e-icons e-sort-icon' cssClass='e-small e-round' isPrimary='true'></button></div>
</ng-template>
</ejs-listview>
</div>
`,
})
export class AppComponent {
public data=[
{ text: 'Date', id: '1', imgUrl: './dates.jpg' },
{ text: 'Fig', id: '2', imgUrl: './fig.jpg' },
{ text: 'Apple', id: '3', imgUrl: './apple.png' },
{ text: 'Apricot', id: '4', imgUrl: './apricot.jpg' },
{ text: 'Grape', id: '5', imgUrl: './grape.jpg' },
{ text: 'Strawberry', id: '6', imgUrl: './strawberry.jpg' },
{ text: 'Pineapple', id: '7', imgUrl: './pineapple.jpg' },
{ text: 'Melon', id: '8', imgUrl: './melon.jpg' },
{ text: 'Lemon', id: '9', imgUrl: './lemon.jpg' },
{ text: 'Cherry', id: '10', imgUrl: './cherry.jpg' },
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Template
ListView items can be customized with the help of the template
property.
To customize list items in your application, set your customized template element inside ng tag directive.
We provided the following built-in CSS classes to customize the list-items. Refer to the following table.
CSS class | Description |
---|---|
e-list-template, e-list-wrapper | These classes are used to differentiate normal and template rendering, which are mandatory for template rendering. The e-list-template class should be added to the root of the ListView element and e-list-wrapper class should be added to the template element wrapper. |
e-list-content | This class is used to align list content and it should be added to the content element <div class="e-list-wrapper"> <span class="e-list-content">ListItem</span> </div>
|
e-list-avatar | This class is used for avatar customization. It should be added to the template element wrapper. After adding it, we can customize our element with Avatar classes <div class="e-list-wrapper e-list-avatar "> <span class="e-avatar e-avatar-circle">MR</span> <span class="e-list-content">ListItem</span> </div>
|
e-list-avatar-right | This class is used to align avatar to right side of the list item. It should be added to the template element wrapper. After adding it, we can customize our element with Avatar classes <div class="e-list-wrapper e-list-avatar-right "> <span class="e-list-content">ListItem</span> <span class="e-avatar e-avatar-circle">MR</span> </div>
|
e-list-badge | This class is used for badge customization .It should be added to the template element wrapper. After adding it, we can customize our element with Badge classes <div class="e-list-wrapper e-list-badge "> <span class="e-list-content">ListItem</span> <span class="e-badge e-badge-primary">MR</span> </div>
|
e-list-multi-line | This class is used for multi-line customization. It should be added to the template element wrapper. After adding it, we can customize List item’s header and description <div class="e-list-wrapper e-list-multi-line "> <span class="e-list-content">ListItem</span> </div>
|
e-list-item-header | This class is used to align a list header and it should be added to the header element along with the multi-line class <div class="e-list-wrapper e-list-multi-line "> <span class="e-list-item-header">ListItem Header</span> <span class="e-list-content">ListItem</span> </div>
|
In the following example, we have customized list items with built-in CSS classes.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ListViewModule } from '@syncfusion/ej2-angular-lists'
import { Component } from '@angular/core';
import { CommonModule, NgIf } from '@angular/common';
@Component({
imports: [
ListViewModule, CommonModule, NgIf
],
standalone: true,
selector: 'my-app',
template: `
<div id="sample">
<ejs-listview id='List' [dataSource]='data' headerTitle='Contacts' cssClass='e-list-template' [showHeader]='true' sortOrder='Ascending'>
<ng-template #template let-data="">
<div class="e-list-wrapper e-list-multi-line e-list-avatar">
<span class="e-avatar e-avatar-circle" *ngIf="data.avatar !== ''"></span>
<span class=" e-avatar e-avatar-circle" *ngIf="data.pic !== '' "> </span>
<span class="e-list-item-header"></span>
<span class="e-list-content"></span>
</div>
</ng-template>
</ejs-listview>
</div>
`
})
export class AppComponent {
// Listview datasource with avatar and image source fields
public data?: { [key: string]: Object; }[] = [
{
text: "Jenifer",
contact: "(206) 555-985774",
id: "1",
avatar: "",
pic: "pic01"
},
{ text: "Amenda", contact: "(206) 555-3412", id: "2", avatar: "A", pic: "" },
{
text: "Isabella",
contact: "(206) 555-8122",
id: "4",
avatar: "",
pic: "pic02"
},
{
text: "William ",
contact: "(206) 555-9482",
id: "5",
avatar: "W",
pic: ""
},
{
text: "Jacob",
contact: "(71) 555-4848",
id: "6",
avatar: "",
pic: "pic04"
},
{ text: "Matthew", contact: "(71) 555-7773", id: "7", avatar: "M", pic: "" },
{
text: "Oliver",
contact: "(71) 555-5598",
id: "8",
avatar: "",
pic: "pic03"
},
{
text: "Charlotte",
contact: "(206) 555-1189",
id: "9",
avatar: "C",
pic: ""
}
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Group template
ListView group header can be customized with the help of the groupTemplate
property.
To customize the group template in your application, set your customized template element inside ng tag directive.
In the following example, we have grouped Listview based on the category. The category of each list item should be mapped with groupBy
field of the data. We have also displayed grouped list items count in the group list header.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ListViewModule } from '@syncfusion/ej2-angular-lists'
import { Component} from '@angular/core';
@Component({
imports: [
ListViewModule
],
standalone: true,
selector: 'my-app',
template: `<ejs-listview id='List' [dataSource]='dataSource' cssClass='e-list-template' [fields]='fields'>
<ng-template #template let-dataSource="">
<div class="e-list-wrapper e-list-multi-line e-list-avatar">
<img class="e-avatar e-avatar-circle" src= style="background:#BCBCBC" />
<span class="e-list-item-header"></span>
<span class="e-list-content"></span>
</div>
</ng-template>
<ng-template #groupTemplate let-dataSource="">
<div>
<span className="category"></span>
<span id="count"> Item(s)</span>
</div>
</ng-template>
</ejs-listview>`
})
export class AppComponent {
public dataSource: Object = [
{ Name: 'Nancy', contact:'(206) 555-985774', id: '1', image: 'https://ej2.syncfusion.com/demos/src/grid/images/1.png', category: 'Experience'},
{ Name: 'Janet', contact: '(206) 555-3412', id: '2', image: 'https://ej2.syncfusion.com/demos/src/grid/images/3.png', category: 'Fresher' },
{ Name: 'Margaret', contact:'(206) 555-8122', id:'4', image: 'https://ej2.syncfusion.com/demos/src/grid/images/4.png', category: 'Experience' },
{ Name: 'Andrew ', contact:'(206) 555-9482', id: '5', image: 'https://ej2.syncfusion.com/demos/src/grid/images/2.png', category: 'Experience'},
{ Name: 'Steven', contact:'(71) 555-4848', id: '6', image: 'https://ej2.syncfusion.com/demos/src/grid/images/5.png', category: 'Fresher' },
{ Name: 'Michael', contact:'(71) 555-7773', id: '7', image: 'https://ej2.syncfusion.com/demos/src/grid/images/6.png', category: 'Experience' },
{ Name: 'Robert', contact:'(71) 555-5598', id: '8', image: 'https://ej2.syncfusion.com/demos/src/grid/images/7.png', category: 'Fresher' },
{ Name: 'Laura', contact:'(206) 555-1189', id: '9', image: 'https://ej2.syncfusion.com/demos/src/grid/images/8.png', category: 'Experience' },
];
public fields: Object = { text: 'Name', groupBy: 'category' };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));