Having trouble getting help?
Contact Support
Contact Support
Load list items in child list dynamically in Angular ListView component
22 Jan 202510 minutes to read
To load list items in child list dynamically, push the new list item data into the existing dataSource
using the select
event.
Refer to the following steps to load list item into the child list:
-
Initially, render the ListView with the required data source.
-
Bind the
select
event that triggers selecting list item in the ListView component. By using the select event, you can push the new list item to the child list of the data source on specifying its item index. Item index can be obtained from theSelectEventArgs
of the select event.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ListViewModule } from '@syncfusion/ej2-angular-lists'
import { Component, ViewEncapsulation } from '@angular/core';
import { SelectEventArgs } from '@syncfusion/ej2-lists';
@Component({
imports: [
ListViewModule
],
standalone: true,
selector: 'my-app',
template: `<ejs-listview id='listview' [dataSource]='dataSource' [template]='wintemplate' headerTitle='Folders' [showHeader]='true' [showIcon]='true' [fields]='fields' (select)='onSelect($event)'></ejs-listview>
`,
styles: [`
#listview {
display: block;
max-width: 400px;
margin: auto;
border: 1px solid #dddddd;
border-radius: 3px;
}
#listview.e-listview .e-list-icon {
height: 24px;
width: 30px;
}
.folder, .file {
background: url('http://ej2.syncfusion.com/demos/src/listview/images/file_icons.png') no-repeat;
background-size: 300%;
}
.folder{
background-position: -5px -461px;
}
.file {
background-position: -5px -151px;
}
.list {
color:deeppink !important;
}
`],
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
public dataSource: { [key: string]: Object }[] = [
{
id: '01', text: 'Music', icon: 'folder',
child: [
{ id: '01-01', text: 'Gouttes.mp3', icon: 'file' }
]
},
{
id: '02', text: 'Videos', icon: 'folder',
child: [
{ id: '02-01', text: 'Naturals.mp4', icon: 'file' },
{ id: '02-02', text: 'Wild.mpeg', icon: 'file' },
]
},
{
id: '03', text: 'Documents', icon: 'folder',
child: [
{ id: '03-01', text: 'Environment Pollution.docx', icon: 'file' },
{ id: '03-02', text: 'Global Water, Sanitation, & Hygiene.docx', icon: 'file' },
{ id: '03-03', text: 'Global Warming.ppt', icon: 'file' },
{ id: '03-04', text: 'Social Network.pdf', icon: 'file' },
{ id: '03-05', text: 'Youth Empowerment.pdf', icon: 'file' },
]
},
{
id: '04', text: 'Pictures', icon: 'folder',
child: [
{
id: '04-01', text: 'Camera Roll', icon: 'folder',
child: [
{ id: '04-01-01', text: 'WIN_20160726_094117.JPG', icon: 'file' },
{ id: '04-01-02', text: 'WIN_20160726_094118.JPG', icon: 'file' },
{ id: '04-01-03', text: 'WIN_20160726_094119.JPG', icon: 'file' }
]
},
{
id: '04-02', text: 'Wind.jpg', icon: 'file'
},
{
id: '04-02', text: 'Stone.jpg', icon: 'file'
},
{
id: '04-02', text: 'Home.jpg', icon: 'file'
},
{
id: '04-02', text: 'Bridge.png', icon: 'file'
}
]
},
{
id: '05', text: 'Downloads', icon: 'folder',
child: [
{ id: '05-01', text: 'UI-Guide.pdf', icon: 'file' },
{ id: '05-02', text: 'Tutorials.zip', icon: 'file' },
{ id: '05-03', text: 'Game.exe', icon: 'file' },
{ id: '05-04', text: 'TypeScript.7z', icon: 'file' },
]
},
];
public fields: Object = { iconCss: 'icon', tooltip: 'text' };
wintemplate: any;
//Select event to add new list item in child page
onSelect(args: SelectEventArgs) {
//Add new file to the child page of selected list item
(this.dataSource[args.index]['child'] as any).push({ id: '01-02', text: 'Newly Added File', icon: 'file', htmlAttributes: { role: 'li', class: 'list' } });
}
}
@import 'node_modules/@syncfusion/ej2-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-lists/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-buttons/styles/material.css';
#sample-list {
display: block;
max-width: 400px;
margin: auto;
border: 1px solid #dddddd;
border-radius: 3px;
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));