Virtualization in ComboBox Component

8 Jul 202411 minutes to read

ComboBox virtualization is a technique used to efficiently render extensive lists of items while minimizing the impact on performance. This method is particularly advantageous when dealing with large datasets because it ensures that only a fixed number of DOM (Document Object Model) elements are created. When scrolling through the list, existing DOM elements are reused to display relevant data instead of generating new elements for each item. This recycling process is managed internally.

During virtual scrolling, the data retrieved from the data source depends on the popup height and the calculation of the list item height. Enabling the enableVirtualization option in a ComboBox activates this virtualization technique.

When fetching data from the data source, the actionBegin event is triggered before data retrieval begins. Then, the actionComplete event is triggered once the data is successfully fetched.

Furthermore, Incremental Search is supported with virtualization in the Combobox component. When a key is typed, the focus is moved to the respective element in the open popup state. In the closed popup state, the popup opens, and focus is moved to the respective element in the popup list based on the typed key. The Incremental Search functionality is well-suited for scenarios involving remote data binding.

When the enableVirtualization property is enabled, the skip and take properties provided by the user within the Query class at the initial state or during the actionBegin or actionComplete events will not be considered, since it is internally managed and calculated based on certain dimensions with respect to the popup height.

Binding local data

The Combobox can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the fields property. When using virtual scrolling, the list updates based on the scroll offset value, triggering a request to fetch more data from the server. As the data is being fetched, the actionBegin event occurs before the data retrieval starts. Once the data retrieval is successful, the actionComplete event is triggered, indicating that the data fetch process is complete.

In the following example, id column and text column from complex data have been mapped to the value field and text field, respectively.

@{
    var data = new Record().RecordModelList();
}

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;'>
        <ejs-combobox id="records" dataSource="@data" placeholder="Select a item" allowFiltering="false" enableVirtualization="true" popupHeight="200px">
            <e-combobox-fields value="ID" text="Text"></e-combobox-fields>
        </ejs-combobox>
    </div>
</div>
...
public class Record
{
    public string ID { get; set; }
    public string Text { get; set; }
    public List<Record> RecordList { set; get; }
    public List<Record> RecordModelList()
    {
        return Enumerable.Range(1, 150).Select(i => new Record()
        {
            ID = i.ToString(),
            Text = "Item " + i,
        }).ToList();
    }
}

Binding remote data

The Combobox supports the retrieval of data from remote data services with the help of the DataManager component, triggering the actionBegin and actionComplete events, and then updating the list data. During virtual scrolling, additional data is retrieved from the server, triggering the actionBegin and actionComplete events at that time as well.

The following sample displays the OrderId from the Orders Data Service.

@{
    var data = new Record().RecordModelList();
}

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;'>
        <ejs-combobox id="records" dataSource="@data" placeholder="Select a item" allowFiltering="true" enableVirtualization="true" popupHeight="200px">
            <e-data-manager adaptor="WebApiAdaptor" url="https://services.syncfusion.com/aspnet/production/api/Orders" crossDomain="true"></e-data-manager>
            <e-combobox-fields text="OrderID" value="OrderID"></e-combobox-fields>
        </ejs-combobox>
    </div>
</div>
...
public class Record
{
    public string ID { get; set; }
    public string Text { get; set; }
    public List<Record> RecordList { set; get; }
    public List<Record> RecordModelList()
    {
        return Enumerable.Range(1, 150).Select(i => new Record()
        {
            ID = i.ToString(),
            Text = "Item " + i,
        }).ToList();
    }
}

Customizing items count in virtualization

When the enableVirtualization property is enabled, the take property provided by the user within the Query parameter at the initial state or during the actionBegin event will be considered. Internally, it calculates the items that fit onto the current page (i.e., probably twice the amount of the popup’s height). If the user-provided take value is less than the minimum number of items that fit into the popup, the user-provided take value will not be considered.

The following sample shows the example for Customizing items count in virtualization.

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;'>
        <ejs-combobox id="records" dataSource="@ViewBag.data" query="new ej.data.Query().take(40)" actionBegin="onbegin" placeholder="Select a item" allowFiltering="false" enableVirtualization="true" popupHeight="200px">
            <e-combobox-fields value="ID" text="Text"></e-combobox-fields>
        </ejs-combobox>
    </div>
</div>

<script>
    function onbegin(e) {
        e.query = new ej.data.Query().take(45);
    }
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class MultiSelectController : Controller
    {
        public ActionResult virtualscroll()
        {
            ViewBag.data = new Record().RecordModelList();
            return View();
        }
    }
}

Grouping

The Combobox component supports grouping with Virtualization. It allows you to organize elements into groups based on different categories. Each item in the list can be classified using the groupBy field in the data table. After grouping, virtualization works similarly to local data binding, providing a seamless user experience. When the data source is bound to remote data, an initial request is made to retrieve all data for the purpose of grouping. Subsequently, the grouped data works in the same way as local data binding virtualization, enhancing performance and responsiveness.

The following sample shows the example for Grouping with Virtualization.

@{
    var data = new Record().RecordModelList();
}

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;'>
        <ejs-combobox id="records" dataSource="@data" placeholder="Select a item" allowFiltering="true" enableVirtualization="true" popupHeight="200px">
            <e-combobox-fields groupBy="Group" value="ID" text="Text"></e-combobox-fields>
        </ejs-combobox>
    </div>
</div>
...
public class Record
{
    public string ID { get; set; }
    public string Text { get; set; }
        public string Group { get; set; }
    public List<Record> RecordList { set; get; }
    public List<Record> RecordModelList()
    {
        Random random = new Random();
        return Enumerable.Range(1, 150).Select(i => new Record()
        {
            ID = i.ToString(),
            Text = "Item " + i,
            Group = GetRandomGroup(random),

        }).ToList();
    }
    public string GetRandomGroup(Random random)
    {
        // Generate a random number between 1 and 4 to determine the group
        int randomGroup = random.Next(1, 5);
        switch (randomGroup)
        {
            case 1:
                return "Group A";
            case 2:
                return "Group B";
            case 3:
                return "Group C";
            case 4:
                return "Group D";
            default:
                return string.Empty;
        }
    }
}

Filtering with Virtualization

The ComboBox component supports Filtering with Virtualization. The ComboBox includes a built-in feature that enables data filtering when the allowFiltering option is enabled. In the context of Virtual Scrolling, the filtering process operates in response to the typed characters. Specifically, the DropDownList sends a request to the server, utilizing the full data source, to achieve filtering. Before initiating the request, an action event is triggered. Upon successful retrieval of data from the server, an action complete event is triggered. The initial data is loaded when the popup is opened. Whether the filter list has a selection or not, the popup closes.

The following sample shows the example for Filtering with Virtualization.

@{
    var data = new Record().RecordModelList();
}

<div class="control-wrapper">
    <div id="default" style='padding-top:75px;'>
        <ejs-combobox id="records" dataSource="@data" placeholder="Select a item" allowFiltering="true" enableVirtualization="true" popupHeight="200px">
            <e-combobox-fields value="ID" text="Text"></e-combobox-fields>
        </ejs-combobox>
    </div>
</div>
...
public class Record
{
    public string ID { get; set; }
    public string Text { get; set; }
    public List<Record> RecordList { set; get; }
    public List<Record> RecordModelList()
    {
        return Enumerable.Range(1, 150).Select(i => new Record()
        {
            ID = i.ToString(),
            Text = "Item " + i,
        }).ToList();
    }
}