Virtualization in EJ2 JavaScript Listview control
8 Aug 202313 minutes to read
UI virtualization loads only viewable list items in a view port, which will improve the ListView performance while loading a large number of data.
Module injection
To use UI virtualization, you should import virtualization
module from the ej2-lists
package and inject it using ListView.Inject()
method as shown in the following code snippet.
import { ListView, Virtualization } from '@syncfusion/ej2-lists';
ListView.Inject(Virtualization);
Getting started
UI virtualization can be enabled in the ListView by setting the enableVirtualization
property to true.
It has two types of scrollers as follows:
Window scroll: This scroller is used in the ListView by default.
Container scroll: This scroller is used, when the height property of the ListView is set.
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,
});
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/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.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>
</body></html>
You can use the template
property to customize list items in UI virtualization.
var listData = [];
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/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.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>
</body></html>
Conditional rendering
The following conditional rendering support is provided for the template and groupTemplate.
Name | Syntax |
---|---|
conditional class | <div class="${ $id % 2 === 0 ? 'even-list' : 'odd-list'}"></div> |
conditional attribute | <div id="${ $id % 2 === 0 ? 'even-list' : 'odd-list'}"></div> |
conditional text content | <div>${ $id % 2 === 0 ? 'even-list' : 'odd-list'}</div> |
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: 500,
//Set defined customized template
template: '<div id="list-container" class="${ $id % 2 === 0 ? \'even-list\' : \'odd-list\' }" > ${text} </div>'
});
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/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.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>
</body></html>