Virtual scrolling in ASP.NET Core Schedule control

15 Sep 202318 minutes to read

To achieve better performance in the Scheduler when loading a large number of resources and events, we have added virtual scrolling support to load a large set of resources and events instantly as you scroll. You can dynamically load large number of resources and events in the Scheduler by setting true to the allowVirtualScrolling property within the view specific settings. The virtual loading of events is possible in Agenda view, by setting allowVirtualScrolling property to true within the agenda view specific settings.

@using Syncfusion.EJ2.Schedule

<ejs-schedule id="schedule" width="100%" height="650" selectedDate="new DateTime(DateTime.Today.Year, 4, 1)" currentView="TimelineMonth">
    <e-schedule-views>
        <e-schedule-view option="TimelineMonth" allowVirtualScrolling="true"></e-schedule-view>
        <e-schedule-view option="TimelineYear" orientation="Vertical" allowVirtualScrolling="true"></e-schedule-view>
    </e-schedule-views>
    <e-schedule-group enableCompactView="false" resources="@ViewBag.Resource"></e-schedule-group>
    <e-schedule-resources>
        <e-schedule-resource field="ResourceId" dataSource="@ViewBag.resourcesData" title="Resource" name="Resources" textField="Text" idField="Id" colorField="Color" allowMultiple="true"></e-schedule-resource>
    </e-schedule-resources>
    <e-schedule-eventsettings dataSource="@ViewBag.datasource"></e-schedule-eventsettings>
</ejs-schedule>
public ActionResult Index()
{
    ViewBag.datasource = this.generateStaticEvents(new DateTime(2018, 4, 1), 300, 12);
    ViewBag.resourcesData = this.GenerateResourceData(1, 300);
    string[] resources = new string[] { "Resources" };
    ViewBag.Resource = resources;
    return View();
}
private List<ResourceData> GenerateResourceData(int start, int end)
{
    List<ResourceData> resources = new List<ResourceData>(300);
    var colors = new string[] { "#ff8787", "#9775fa", "#748ffc", "#3bc9db", "#69db7c",
    "#fdd835", "#748ffc", "#9775fa", "#df5286", "#7fa900",
    "#fec200", "#5978ee", "#00bdae", "#ea80fc"};
    for (int a = start; a <= end; a++)
    {
        int index = a % colors.Length;
        index = index == 0 ? (colors.Length / a):index;  
        resources.Add(new ResourceData
        {
            Id = a,
            Text = "Resource " + a,
            Color = colors[index]
        });
    }
    return resources;
}
private List<EventData> generateStaticEvents(DateTime date, int v1, int v2)
{
    List<EventData> data = new List<EventData>(3600);
    var id = 1;
    for (var i = 0; i < 300; i++)
    {
        Random random= new Random();
        List<int> listNumbers = new List<int>();
        int[] randomCollection = new int[24];
        int number;
        int max = 30;
        for (int a = 0; a < 12; a++)
        {
            do
            {
                number = random.Next(max);
            } while (listNumbers.Contains(number));
            listNumbers.Add(number);                    
            var startDate = date.AddDays(number);
            startDate = startDate.AddMilliseconds((((number % 10) * 10) * (1000 * 60)));
            var endDate = startDate.AddMilliseconds(((1440 + 30) * (1000 * 60)));
            data.Add(new EventData
            {
                Id = id,
                Subject = "Event #" + id,
                StartTime = startDate,
                EndTime = endDate,
                IsAllDay = (id % 10 == 0) ? false : true,
                ResourceId = i + 1
            });
            id++;
        }              
    }
    return data;
}
class EventData
{
    public int Id { get; set; }
    public string Subject { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public bool IsAllDay { get; set; }
    public int ResourceId { get; set; }
}

class ResourceData
{
    public int Id { get; set; }
    public string Text { get; set; }
    public string Color { get; set; }
}

NOTE

For now, the virtual loading of resources and events is not supported in MonthAgenda, Year and TimelineYear (Horizontal Orientation) views.

Enabling lazy loading for appointments

The lazy loading feature provides a convenient way to efficiently load resource appointments into the Scheduler using an on-demand approach. With this feature, you can seamlessly load a large volume of appointment data into the Scheduler without experiencing any performance degradation.

By default, the Scheduler fetches all the relevant appointments from the server with in the current date range. However, enabling this feature will trigger query requests to the server for appointment retrieval whenever new resources are rendered due to scroll actions. These queries contain the resource IDs of currently displayed resources along with current date range, which can be passed as a comma-separated string. In the server controller, these resource IDs are parsed to filter the necessary appointments to render in the scheduler.

When you enable this feature, the Scheduler becomes capable of fetching events from remote services only for the current view port alone to optimize the data retrieval. The remaining appointment data is fetched form the server on-demand based on currently rendered view port resources as you scroll’s through the scheduler content.

To enable this feature, you have to set the enableLazyLoading property to true within the view specific settings.

@using Syncfusion.EJ2.Schedule

<ejs-schedule id="schedule" width="100%" height="650" selectedDate="new DateTime(DateTime.Today.Year, 4, 1)" currentView="TimelineMonth">
    <e-schedule-views>
        <e-schedule-view option="TimelineMonth" allowVirtualScrolling="true"></e-schedule-view>
        <e-schedule-view option="TimelineYear" orientation="Vertical" allowVirtualScrolling="true"></e-schedule-view>
    </e-schedule-views>
    <e-schedule-group enableCompactView="false" resources="@ViewBag.Resource"></e-schedule-group>
    <e-schedule-resources>
        <e-schedule-resource field="ResourceId" dataSource="@ViewBag.resourcesData" title="Resource" name="Resources" textField="Text" idField="Id" colorField="Color" allowMultiple="true"></e-schedule-resource>
    </e-schedule-resources>
    <e-schedule-eventsettings dataSource="@ViewBag.datasource"></e-schedule-eventsettings>
</ejs-schedule>
public ActionResult Index()
{
    ViewBag.datasource = this.generateStaticEvents(new DateTime(2018, 4, 1), 300, 12);
    ViewBag.resourcesData = this.GenerateResourceData(1, 300);
    string[] resources = new string[] { "Resources" };
    ViewBag.Resource = resources;
    return View();
}
private List<ResourceData> GenerateResourceData(int start, int end)
{
    List<ResourceData> resources = new List<ResourceData>(300);
    var colors = new string[] { "#ff8787", "#9775fa", "#748ffc", "#3bc9db", "#69db7c",
    "#fdd835", "#748ffc", "#9775fa", "#df5286", "#7fa900",
    "#fec200", "#5978ee", "#00bdae", "#ea80fc"};
    for (int a = start; a <= end; a++)
    {
        int index = a % colors.Length;
        index = index == 0 ? (colors.Length / a):index;  
        resources.Add(new ResourceData
        {
            Id = a,
            Text = "Resource " + a,
            Color = colors[index]
        });
    }
    return resources;
}
private List<EventData> generateStaticEvents(DateTime date, int v1, int v2)
{
    List<EventData> data = new List<EventData>(3600);
    var id = 1;
    for (var i = 0; i < 300; i++)
    {
        Random random= new Random();
        List<int> listNumbers = new List<int>();
        int[] randomCollection = new int[24];
        int number;
        int max = 30;
        for (int a = 0; a < 12; a++)
        {
            do
            {
                number = random.Next(max);
            } while (listNumbers.Contains(number));
            listNumbers.Add(number);                    
            var startDate = date.AddDays(number);
            startDate = startDate.AddMilliseconds((((number % 10) * 10) * (1000 * 60)));
            var endDate = startDate.AddMilliseconds(((1440 + 30) * (1000 * 60)));
            data.Add(new EventData
            {
                Id = id,
                Subject = "Event #" + id,
                StartTime = startDate,
                EndTime = endDate,
                IsAllDay = (id % 10 == 0) ? false : true,
                ResourceId = i + 1
            });
            id++;
        }              
    }
    return data;
}
class EventData
{
    public int Id { get; set; }
    public string Subject { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public bool IsAllDay { get; set; }
    public int ResourceId { get; set; }
}

class ResourceData
{
    public int Id { get; set; }
    public string Text { get; set; }
    public string Color { get; set; }
}

Here’s the server-side controller code that retrieves appointment data based on the resource IDs provided as query parameters:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.OData.Query;

namespace LazyLoadingServices.Controllers
{
    public class VirtualEventDataController : Controller
    {
        private readonly EventsContext dbContext;

        [HttpGet]
        [EnableQuery]
        [Route("api/VirtualEventData")]
        public IActionResult GetData([FromQuery] Params param)
        {
            IQueryable<EventData> query = dbContext.Events;
            // Filter the appointment data based on the ResourceId query params.
            if (!string.IsNullOrEmpty(param.ResourceId))
            {
                string[] resourceId = param.ResourceId.Split(',');
                query = query.Where(data => resourceId.Contains(data.ResourceId.ToString()));
            }
            return Ok(query.ToList());
        }
    }
    public class Params
    {
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public string ResourceId { get; set; }
    }
}

Note:

  • The property will be effective, when large number of resources and appointments bound to the Scheduler.
  • This property is applicable only when resource grouping is enabled in Scheduler.

NOTE

You can refer to our ASP.NET Core Scheduler feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET Core Scheduler example to knows how to present and manipulate data.