- Module injection
- Conditional rendering
Contact Support
Virtualization in EJ2 JavaScript ListView control
22 Feb 202520 minutes to read
UI virtualization loads only the viewable list items within a viewport, significantly improving ListView performance when dealing with a large amount of data.
Module injection
To use UI virtualization, you should import the Virtualization
module from the ej2-lists
package and inject it using the ListView.Inject()
method, as shown in the following code snippet.
import { ListView, Virtualization } from '@syncfusion/ej2-lists';
ListView.Inject(Virtualization);
UI virtualization can be enabled in the ListView by setting the enableVirtualization
property to true.
There are two types of scroll behaviors:
Window Scroll: This is used by default in the ListView.
Container Scroll: This is used when the height
property of the ListView is set.
var listData = [
{ text: 'Nancy', id: '0', },
{ text: 'Andrew', id: '1' },
{ text: 'Janet', id: '2' },
{ text: 'Margaret', id: '3' },
{ text: 'Steven', id: '4' },
{ text: 'Laura', id: '5' },
{ text: 'Robert', id: '6' },
{ text: 'Michael', id: '7' },
{ text: 'Albert', id: '8' },
{ text: 'Nolan', id: '9' }
];
for (var i = 10; i <= 1010; i++) {
var index = parseInt((Math.random() * 10).toString());
listData.push({ text: listData[index].text, id: i.toString() });
}
var listObj = new ej.lists.ListView({
//Set defined data to dataSource property.
dataSource: listData,
//Set height
height: 500,
//enable UI virtualization
enableVirtualization: true,
});
listObj.appendTo('#ui-list');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for ListView </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 for ListView UI Control">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="ui-list"></div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
#ui-list {
display: block;
max-width: 350px;
margin: auto;
border-radius: 3px;
cursor: pointer;
}
</style>
</body>
</html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
top: 45%;
left: 45%;
}
You can use the template
property to customize list items in UI virtualization.
var listData = [
{ name: 'Nancy', icon: 'N', id: '0', },
{ name: 'Andrew', icon: 'A', id: '1' },
{ name: 'Janet', icon: 'J', id: '2' },
{ name: 'Margaret', imgUrl: '//ej2.syncfusion.com/demos/src/listview/images/margaret.png', id: '3' },
{ name: 'Steven', icon: 'S', id: '4' },
{ name: 'Laura', imgUrl: '//ej2.syncfusion.com/demos/src/listview/images/laura.png', id: '5' },
{ name: 'Robert', icon: 'R', id: '6' },
{ name: 'Michael', icon: 'M', id: '7' },
{ name: 'Albert', imgUrl: '//ej2.syncfusion.com/demos/src/listview/images/albert.png', id: '8' },
{ name: 'Nolan', icon: 'N', id: '9' }
]
for (var i = 10; i <= 1010; i++) {
var index = parseInt((Math.random() * 10).toString());
listData.push({ name: listData[index].name, icon: listData[index].icon, imgUrl: listData[index].imgUrl, id: i.toString() });
}
var template = function (data) {
var showIcon = data.imgUrl ? 'hideUI' : 'showUI';
var showImg = data.imgUrl ? 'showUI' : 'hideUI';
var imgUrl = data.imgUrl || '';
var result = '<div class="e-list-wrapper e-list-avatar">' +
'<span class="e-avatar e-avatar-circle ' + data.icon + ' ' + showIcon + '">' + data.icon + '</span>' +
'<img class="e-avatar e-avatar-circle ' + showImg + '" src="' + imgUrl + '" />' +
'<span class="e-list-content">' + data.name + '</span>' +
'</div>';
return result;
};
var listObj = new ej.lists.ListView({
//Set defined data to dataSource property.
dataSource: listData,
//enable UI virtualization
enableVirtualization: true,
//Set height
height: 500,
//Set header title
headerTitle: 'Contacts',
//Set true to show header title
showHeader: true,
//Set defined customized css class
cssClass: 'e-list-template',
//Set defined customized template
template: template
});
listObj.appendTo('#ui-list');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for ListView </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 for ListView UI Control">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-layouts/styles/material.css" rel="stylesheet" />
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="ui-list"></div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
#ui-list.e-listview {
margin: auto;
max-width: 325px;
line-height: initial;
border: 1px solid lightgray;
}
#ui-list.e-listview .e-list-header {
height: 50px
}
#ui-list.e-listview .e-list-header .e-text {
line-height: 18px
}
#ui-list.e-listview .R {
background: lightgrey;
}
#ui-list.e-listview .M {
background: pink;
}
#ui-list.e-listview .A {
background: lightgreen;
}
#ui-list.e-listview .S {
background: lightskyblue;
}
#ui-list.e-listview .J {
background: orange;
}
#ui-list.e-listview .N {
background: lightblue;
}
#ui-list.e-listview .e-list-item {
padding: 3px 0;
}
#ui-list .list-container {
width: inherit;
height: 100%;
}
#ui-list.e-listview .showUI {
display: flex;
}
#ui-list.e-listview .hideUI {
display: none;
}
</style>
</body>
</html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
top: 45%;
left: 45%;
}
Conditional rendering
The following conditional rendering support is provided for the template and groupTemplate.
Name | Syntax |
---|---|
conditional rendering | ${if(id % 2=="0")}<div id="list-container" class="even-list"> ${text} </div>${else}<div id="list-container" class="odd-list"> ${text} </div>${/if} |
In the following sample, the light blue is applied for the even list and light coral is applied for the odd list based on the conditional class.
var listData = [];
listData = [
{ text: 'Nancy', id: '0', },
{ text: 'Andrew', id: '1' },
{ text: 'Janet', id: '2' },
{ text: 'Margaret', id: '3' },
{ text: 'Steven', id: '4' },
{ text: 'Laura', id: '5' },
{ text: 'Robert', id: '6' },
{ text: 'Michael', id: '7' },
{ text: 'Albert', id: '8' },
{ text: 'Nolan', id: '9' }
];
for (var i = 10; i <= 1010; i++) {
var index = parseInt((Math.random() * 10).toString());
listData.push({ text: listData[index].text, id: i.toString() });
}
var listObj = new ej.lists.ListView({
//Set defined data to dataSource property.
dataSource: listData,
//enable UI virtualization
enableVirtualization: true,
//Set height
height: 380,
//Set defined customized template
template: '${if(id % 2=="0")}<div id="list-container" class="even-list"> ${text} </div>${else}<div id="list-container" class="odd-list"> ${text} </div>${/if}'
});
listObj.appendTo('#ui-list');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for ListView </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Essential JS 2 for ListView UI Control">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="ui-list"></div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
#ui-list {
display: block;
max-width: 350px;
margin: auto;
border: 1px solid #dddddd;
border-radius: 3px;
cursor: pointer;
}
#list-container {
height: inherit;
width: inherit;
padding-left: 30px;
}
#ui-list .e-list-item {
padding: 0;
}
#ui-list .even-list {
background-color: #cfd8dc;
}
#ui-list .odd-list {
background-color: #eceff1;
}
</style>
</body>
</html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
font-family: 'Helvetica Neue','calibiri';
font-size: 14px;
top: 45%;
left: 45%;
}