Achieve virtual scrolling in EJ2 JavaScript Multi select control
2 May 20235 minutes to read
The Virtual Scrolling is used to display a large amount of data without buffering the entire load of a huge database record in the MultiSelect, that is, when scrolling, the request is sent and fetch some amount of data from the server dynamically. Using the scroll
event, get the data and generate the list add to popup using the addItem
method.
Refer to the following code sample for virtual scrolling.
var data = new ej.data.DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers',
adaptor: new ej.data.ODataV4Adaptor(),
crossDomain: true
});
//initiates the component
let mulObject = new ej.dropdowns.MultiSelect({
// bind the DataManager instance to dataSource property
dataSource: data,
// bind the Query instance to query property
query: new ej.data.Query().select(['ContactName']),
// map the appropriate columns to fields property
fields: { text: 'ContactName', value: 'ContactName' },
// set the placeholder to MultiSelect input element
placeholder: 'Select a customer',
// sort the resulted items
sortOrder: 'Ascending',
// set the height of the popup element
popupHeight: '200px',
actionComplete: function (e) {
let operator = new ej.data.Query().from('Customers').select('ContactName');
let start = 7;
let end = 12;
let listElement = this.list;
listElement.addEventListener('scroll', () => {
if ((listElement.scrollTop + listElement.offsetHeight >= listElement.scrollHeight)) {
let filterQuery = operator.clone();
data.executeQuery(filterQuery.range(start, end)).then((event) => {
start = end;
end += 5;
mulObject.addItem(event.result);
}).catch((e) => {
});
}
})
}
});
mulObject.appendTo('#atcelement');
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2 AutoComplete</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="styles.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/25.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/25.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/25.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/25.2.3/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" style="margin:0 auto; width:250px;">
<input type="text" tabindex="1" id="atcelement">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>