Search results

Infinite Scrolling in ASP.NET MVC Grid control

Infinite scrolling is used to load a huge amount of data without degrading the Grid performance. This feature works like the lazy loading concept, which means the buffer data is loaded only when the scrollbar reaches the end of the scroller.

To enable Infinite scrolling, set enableInfiniteScrolling property as true.

  • In this feature, Grid will not make a new data request when you visit the same page again.
razor
infinite-scroll-normal.cs
@Html.EJS().Grid("InfiniteGrid").DataBound("hide").EnableInfiniteScrolling().Height("500").Columns(col =>
   {

       col.Field("Field1").HeaderText("PlayerName").Width("140").Add();
       col.Field("Field2").HeaderText("Year").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field3").HeaderText("Stint").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field4").HeaderText("TMID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field5").HeaderText("LGID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   }).Render()

    <script>
        var infiniteData = [], date1=null, date2=null, flag= true;
        var names = ['hardire', 'abramjo01', 'aubucch01', 'Hook', 'Rumpelstiltskin', 'Belle', 'Emma', 'Regina', 'Aurora', 'Elsa',
            'Anna', 'Snow White', 'Prince Charming', 'Cora', 'Zelena', 'August', 'Mulan', 'Graham', 'Discord', 'Will', 'Robin Hood',
            'Jiminy Cricket', 'Henry', 'Neal', 'Red', 'Aaran', 'Aaren', 'Aarez', 'Aarman', 'Aaron', 'Aaron-James', 'Aarron', 'Aaryan', 'Aaryn',
            'Aayan', 'Aazaan', 'Abaan', 'Abbas', 'Abdallah', 'Abdalroof', 'Abdihakim', 'Abdirahman', 'Abdisalam', 'Abdul', 'Abdul-Aziz',
            'Abdulbasir', 'Abdulkadir', 'Abdulkarem', 'Abdulkhader', 'Abdullah', 'Abdul-Majeed', 'Abdulmalik', 'Abdul-Rehman', 'Abdur',
            'Abdurraheem', 'Abdur-Rahman', 'Abdur-Rehmaan', 'Abel', 'Abhinav', 'Abhisumant', 'Abid', 'Abir', 'Abraham', 'Abu', 'Abubakar',
            'Ace', 'Adain', 'Adam', 'Adam-James', 'Addison', 'Addisson', 'Adegbola', 'Adegbolahan', 'Aden', 'Adenn', 'Adie', 'Adil', 'Aditya',
            'Adnan', 'Adrian', 'Adrien', 'Aedan', 'Aedin', 'Aedyn', 'Aeron', 'Afonso', 'Ahmad', 'Ahmed', 'Ahmed-Aziz', 'Ahoua', 'Ahtasham',
            'Aiadan', 'Aidan', 'Aiden', 'Aiden-Jack', 'Aiden-Vee'];


        document.getElementById("generate").addEventListener('click', () => {

            var grid = document.getElementById("InfiniteGrid").ej2_instances[0]
            if (!infiniteData.length) {
                show();
                dataSource();
                date1 = new Date().getTime();
                grid.dataSource = infiniteData;
            } else {
                flag = true;
                show();
                date1 = new Date().getTime();
                grid.refresh();
            }

        })
        function show() {
            document.getElementById('popup').style.display = 'inline-block';
        }
        function hide(args) {


            if (flag && date1) {
                var date2 = new Date().getTime();
                document.getElementById('performanceTime').innerHTML = 'Time Taken: ' + (date2 - date1) + 'ms';
                flag = false;
            }
            document.getElementById('popup').style.display = 'none';
        }
        function dataSource() {
            for (var i= 0; i < 100000; i++) {
                infiniteData.push({
                    'Field1': names[Math.floor(Math.random() * names.length)],
                    'Field2': 1967 + (i % 10),
                    'Field3': Math.floor(Math.random() * 200),
                    'Field4': Math.floor(Math.random() * 100),
                    'Field5': Math.floor(Math.random() * 2000),
                });
            }
        }

    </script>
public IActionResult Index()
 {
    return View();
 }

InitialBlocks

You can define the initial loading pages count by using infiniteScrollSettings.initialBlocks property. By default, this feature loads three pages in initial rendering.

In the below demo, we have changed this property value to load five page records instead of three.

razor
infinite-scroll-initialblocks.cs
@Html.EJS().Grid("InfiniteGrid").DataBound("hide").EnableInfiniteScrolling().InfiniteScrollSettings(settings => { settings.InitialBlocks(5);  }).Height("500").Columns(col =>
   {

       col.Field("Field1").HeaderText("PlayerName").Width("140").Add();
       col.Field("Field2").HeaderText("Year").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field3").HeaderText("Stint").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field4").HeaderText("TMID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field5").HeaderText("LGID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   }).Render()

    <script>
        var infiniteData = [], date1=null, date2=null, flag= true;
        var names = ['hardire', 'abramjo01', 'aubucch01', 'Hook', 'Rumpelstiltskin', 'Belle', 'Emma', 'Regina', 'Aurora', 'Elsa',
            'Anna', 'Snow White', 'Prince Charming', 'Cora', 'Zelena', 'August', 'Mulan', 'Graham', 'Discord', 'Will', 'Robin Hood',
            'Jiminy Cricket', 'Henry', 'Neal', 'Red', 'Aaran', 'Aaren', 'Aarez', 'Aarman', 'Aaron', 'Aaron-James', 'Aarron', 'Aaryan', 'Aaryn',
            'Aayan', 'Aazaan', 'Abaan', 'Abbas', 'Abdallah', 'Abdalroof', 'Abdihakim', 'Abdirahman', 'Abdisalam', 'Abdul', 'Abdul-Aziz',
            'Abdulbasir', 'Abdulkadir', 'Abdulkarem', 'Abdulkhader', 'Abdullah', 'Abdul-Majeed', 'Abdulmalik', 'Abdul-Rehman', 'Abdur',
            'Abdurraheem', 'Abdur-Rahman', 'Abdur-Rehmaan', 'Abel', 'Abhinav', 'Abhisumant', 'Abid', 'Abir', 'Abraham', 'Abu', 'Abubakar',
            'Ace', 'Adain', 'Adam', 'Adam-James', 'Addison', 'Addisson', 'Adegbola', 'Adegbolahan', 'Aden', 'Adenn', 'Adie', 'Adil', 'Aditya',
            'Adnan', 'Adrian', 'Adrien', 'Aedan', 'Aedin', 'Aedyn', 'Aeron', 'Afonso', 'Ahmad', 'Ahmed', 'Ahmed-Aziz', 'Ahoua', 'Ahtasham',
            'Aiadan', 'Aidan', 'Aiden', 'Aiden-Jack', 'Aiden-Vee'];


        document.getElementById("generate").addEventListener('click', () => {

            var grid = document.getElementById("InfiniteGrid").ej2_instances[0]
            if (!infiniteData.length) {
                show();
                dataSource();
                date1 = new Date().getTime();
                grid.dataSource = infiniteData;
            } else {
                flag = true;
                show();
                date1 = new Date().getTime();
                grid.refresh();
            }

        })
        function show() {
            document.getElementById('popup').style.display = 'inline-block';
        }
        function hide(args) {


            if (flag && date1) {
                var date2 = new Date().getTime();
                document.getElementById('performanceTime').innerHTML = 'Time Taken: ' + (date2 - date1) + 'ms';
                flag = false;
            }
            document.getElementById('popup').style.display = 'none';
        }
        function dataSource() {
            for (var i= 0; i < 100000; i++) {
                infiniteData.push({
                    'Field1': names[Math.floor(Math.random() * names.length)],
                    'Field2': 1967 + (i % 10),
                    'Field3': Math.floor(Math.random() * 200),
                    'Field4': Math.floor(Math.random() * 100),
                    'Field5': Math.floor(Math.random() * 2000),
                });
            }
        }

    </script>
public IActionResult Index()
 {
    return View();
 }

Cache Mode

Cache is used to store the loaded rows object in the Grid instance which can be reused for creating the row elements whenever you scroll to already visited page. Also, this mode maintains row elements based on the infiniteScrollSettings.maxBlocks count value, once this limit exceeds then it will remove row elements from DOM for new rows.

To enable the cache mode in Infinite scrolling, set infiniteScrollSettings.enableCache property as true.

razor
infinite-scroll-cache.cs
@Html.EJS().Grid("InfiniteGrid").DataBound("hide").EnableInfiniteScrolling().InfiniteScrollSettings(settings => { settings.EnableCache(true);  }).Height("500").Columns(col =>
   {

       col.Field("Field1").HeaderText("PlayerName").Width("140").Add();
       col.Field("Field2").HeaderText("Year").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field3").HeaderText("Stint").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field4").HeaderText("TMID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("Field5").HeaderText("LGID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   }).Render()

    <script>
        var infiniteData = [], date1=null, date2=null, flag= true;
        var names = ['hardire', 'abramjo01', 'aubucch01', 'Hook', 'Rumpelstiltskin', 'Belle', 'Emma', 'Regina', 'Aurora', 'Elsa',
            'Anna', 'Snow White', 'Prince Charming', 'Cora', 'Zelena', 'August', 'Mulan', 'Graham', 'Discord', 'Will', 'Robin Hood',
            'Jiminy Cricket', 'Henry', 'Neal', 'Red', 'Aaran', 'Aaren', 'Aarez', 'Aarman', 'Aaron', 'Aaron-James', 'Aarron', 'Aaryan', 'Aaryn',
            'Aayan', 'Aazaan', 'Abaan', 'Abbas', 'Abdallah', 'Abdalroof', 'Abdihakim', 'Abdirahman', 'Abdisalam', 'Abdul', 'Abdul-Aziz',
            'Abdulbasir', 'Abdulkadir', 'Abdulkarem', 'Abdulkhader', 'Abdullah', 'Abdul-Majeed', 'Abdulmalik', 'Abdul-Rehman', 'Abdur',
            'Abdurraheem', 'Abdur-Rahman', 'Abdur-Rehmaan', 'Abel', 'Abhinav', 'Abhisumant', 'Abid', 'Abir', 'Abraham', 'Abu', 'Abubakar',
            'Ace', 'Adain', 'Adam', 'Adam-James', 'Addison', 'Addisson', 'Adegbola', 'Adegbolahan', 'Aden', 'Adenn', 'Adie', 'Adil', 'Aditya',
            'Adnan', 'Adrian', 'Adrien', 'Aedan', 'Aedin', 'Aedyn', 'Aeron', 'Afonso', 'Ahmad', 'Ahmed', 'Ahmed-Aziz', 'Ahoua', 'Ahtasham',
            'Aiadan', 'Aidan', 'Aiden', 'Aiden-Jack', 'Aiden-Vee'];


        document.getElementById("generate").addEventListener('click', () => {

            var grid = document.getElementById("InfiniteGrid").ej2_instances[0]
            if (!infiniteData.length) {
                show();
                dataSource();
                date1 = new Date().getTime();
                grid.dataSource = infiniteData;
            } else {
                flag = true;
                show();
                date1 = new Date().getTime();
                grid.refresh();
            }

        })
        function show() {
            document.getElementById('popup').style.display = 'inline-block';
        }
        function hide(args) {


            if (flag && date1) {
                var date2 = new Date().getTime();
                document.getElementById('performanceTime').innerHTML = 'Time Taken: ' + (date2 - date1) + 'ms';
                flag = false;
            }
            document.getElementById('popup').style.display = 'none';
        }
        function dataSource() {
            for (var i= 0; i < 100000; i++) {
                infiniteData.push({
                    'Field1': names[Math.floor(Math.random() * names.length)],
                    'Field2': 1967 + (i % 10),
                    'Field3': Math.floor(Math.random() * 200),
                    'Field4': Math.floor(Math.random() * 100),
                    'Field5': Math.floor(Math.random() * 2000),
                });
            }
        }

    </script>
public IActionResult Index()
 {
    return View();
 }

Limitations for Infinite Scrolling

  • Due to the element height limitation in browsers, the maximum number of records loaded by the grid is limited due to the browser capability.
  • Initial loading rows total height must be greater than the viewport height.
  • Cell selection will not be persisted in cache mode.
  • Infinite scrolling is not compatible with batch editing, detail template and hierarchy features.
  • Group expand and collapse state will not be persisted in cache mode.
  • The aggregated information and total group items are displayed based on the current view items. To get these information regardless of the view items, refer to the Group with Page topic.
  • Programmatic selection using the selectRows and selectRow method is not supported in infinite scrolling.