Custom paging

Hello Syncfusion,

I want to have custom paging.

To cut down the data from the database. I know how many pages are available.

So let's say I have 10000 records in total. That means 500 pages with heavy data.

Now I want to use the paging method within the grid with a custom function. That when the user presses 10 the function is kicked and get the records that is part of page 10.

How is this possible within the Syncfusion Grid?



5 Replies

RN Rahul Narayanasamy Syncfusion Team January 18, 2022 02:18 PM UTC

Hi Bossink, 

Greetings from Syncfusion. 

Query: I want to use the paging method within the grid with a custom function. That when the user presses 10 the function is kicked and get the records that is part of page 10. How is this possible within the Syncfusion Grid? 

We suspect that you want to get the particular page records using some custom button/function. You can achieve your requirement by using GoToPageAsync method of the Grid. Find the below code snippets and sample for your reference. 

 
<button @onclick="Get">Get 10th page records</button> 
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true"> 
    <GridPageSettings PageSize="20"></GridPageSettings> 
    <GridColumns> 
        . . . 
   </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<Order> Grid; 
    public List<Order> Orders { get; set; } 
 
    . . . 
   public async Task Get() 
    { 
        await Grid.GoToPageAsync(10); 
    } 
} 


If it does not meet your requirement, then could you please shar more details about your requirement.  

Please let us know if you have any concerns. 

Regards, 
Rahul 



BO bossink January 19, 2022 09:17 AM UTC

Dear Syncfusion,


Thanks for the reply it is as a thought. There is not a way to create the paging within the grid itself only outside with custom buttons?


<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true"> 
    <GridPageSettings AmountOfPages ="500" PageSize="20"></GridPageSettings>
<GridEvents OnPageClick="@OnPageClicked"> </GridEvents>
    <GridColumns> 
        . . . 
   </GridColumns> 
</SfGrid>

@code{ 
    SfGrid<Order> Grid; 
    public List<Order> Orders { get; set; } 
    . . . 
   public async Task OnPageClicked(Int PageNumber) 
    { 
        await Grid.GoToPageAsync(PageNumber ); 
    } 
}

Something along the line as above.


RN Rahul Narayanasamy Syncfusion Team January 20, 2022 02:40 PM UTC

Hi Bossink, 

Thanks for the update, 

We are quite unclear about your requirement case. We would like to inform you that while enabling paging(AllowPaging=true), the pager will be rendered in the bottom of the Grid. You can click the particular page to get the particular set of records in the Grid. 

Reference

 

(Or) 

If you want to get the clicked page number(in above pager) to perform your paging operation, then you can achieve your requirement by using OnActionBegin event of the Grid. You can get the clicked page number in action being event arguments. Find the below code snippets and documentation for your reference. 

<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true"> 
    <GridPageSettings PageCount="12" PageSize="20"></GridPageSettings> 
    <GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents> 
    <GridColumns> 
        . . . 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<Order> Grid; 
    . . . 
    public void ActionBeginHandler(ActionEventArgs<Order> args) 
    { 
        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Paging)) 
        { 
            //you can get the clicked page number in args.CurrentPage. use this to peform your operation for paging. 
            // Here you can customize your code 
        } 
    } 
} 

Reference: 

If you are still facing the problem, then could you please share more details about your requirement. It will be helpful to validate and provide a better solution. 

  • Full code snippets.
  • More details about your requirement.

Regards, 
Rahul 



BO bossink January 21, 2022 09:26 AM UTC

Hello Syncfusion,


With a little of a puzzle with the awnsers linked and trying, I came with a solution that works well enough for me.

I Wanted to share this.

Within the template I show (in case possible) when on page 11 on the left side 1-10 and on the right side 12-21

In the middle is a box that is able to fill that cannot exceed the max amount of pages or negative. All letters are ignored by the catch. The numbers are clickable itself to go to the desired page.

This example has no records. The records are to be called when going to the page and than shown in the grid depending on the side (Get 20 records on page 10) When going to page 11 get the next 20 records from the database etc. This way I don't have to get thousends or records and get only what is needed at that moment.


I Hope I can help people with this.

Schermafbeelding 2022-01-21 102449.png

@page "/TestPage"

@using Syncfusion.Blazor.Grids


<SfGrid @ref="defaultGrid" DataSource="students" AllowPaging="true">

    <GridPageSettings PageSize="10">

        <Template>

            <div class="PagerTemplate">


                <div class="@($"e-first e-icons e-icon-first {ValidateFirst()} align-icons e-firstpage")" @onclick="ShowFirstPage" title="Go to first page">

                </div>

                <div class="@($"e-prev e-icons e-icon-prev {ValidateBack()} align-icons")" @onclick="ShowPreviousPage" title="Go to previous page"></div>

                @{

                    int maxPagesShow = pageNo + 10;

                    int minPagesShow = pageNo - 10;

                    for (int i = minPagesShow; i < pageNo; i++)

                    {

                        int local_i = i;

                        if (i !> 0)

                        {

                            <div class="e-next" @onclick="((args)=> ShowCurrentPage(local_i))">@i</div>

                        }

                    }

                }

                <div>

                    <input class="e-next textbox add-border" type="text" @bind="pageNo" size="2" @oninput="LaunchEnteredPage" />

                    <div id="totalpages" class="textbox"> of @totalPages pages </div>

                </div>

                @{

                    for (int i = (pageNo+1); i < maxPagesShow+1; i++)

                    {

                        if (i !< totalPages)

                        {

                            int local_i = i;

                            <div class="e-next" @onclick="((args)=> ShowCurrentPage(local_i))">@i</div>

                        }

                    }

                }

                <div class="@($"e-next e-icons e-icon-next {ValidateForward()} align-icons e-next")" @onclick="ShowNextPage" title="Go to next page"></div>

                <div class="@($"e-last e-icons e-icon-last {ValidateLast()} align-icons")" @onclick="ShowLastPage" title="Go to last page"></div>

            </div>

            <style>

                .PagerTemplate {

                    width: 1000px;

                    height: 64px;

                    left: 183px;

                    top: 615px;

                    border-radius: 0px;

                }


                .textbox {

                    margin-top: 9px;

                    margin-bottom: 9px;

                    margin-right: 2px;

                    text-align: center;

                }


                .add-border {

                    border: #ddd 1px solid;

                }


                .align-icons {

                    margin-top: 9px;

                    margin-bottom: 9px;

                    margin-right: 16px;

                    cursor: pointer;

                }


                .e-firstpage {

                    margin-left: 6px;

                }


                .e-next {

                    margin-left: 16px;

                }


                .disableFirst {

                    pointer-events: none;

                    opacity: 0.3;

                }


                .disableLast {

                    pointer-events: none;

                    opacity: 0.3;

                }


                .disableFront {

                    pointer-events: none;

                    opacity: 0.3;

                }


                .disableBack {

                    pointer-events: none;

                    opacity: 0.3;

                }

            </style>

        </Template>

    </GridPageSettings>

    <GridColumns>

        <GridColumn Field="@nameof(Student.OrderNr)" HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>

    </GridColumns>

</SfGrid>


@code{

    public SfGrid< Students > defaultGrid;

    public List<Students> students { get; set; }

    public int pageNo { get; set; } = 1;

    public int totalPages { get; set; } = 500;

    public bool DisableBackIcon = false;

    public bool DisableForwardIcon = false;

    public bool DisableFirstIcon = false;

    public bool DisableLastIcon = false;

    protected override void OnInitialized()

    {

        base.OnInitialized();

    }

    protected override void OnAfterRender(bool firstRender)

    {

        base.OnAfterRender(firstRender);

        if (firstRender)

        {

            //totalPages = defaultGrid.TotalItemCount / defaultGrid.PageSettings.PageSize;

        }

        pageNo = defaultGrid.PageSettings.CurrentPage;

        if (defaultGrid.PageSettings.CurrentPage == totalPages)

        {

            DisableForwardIcon = true;

            DisableLastIcon = true;

        }

        else

        {

            DisableForwardIcon = false;

            DisableLastIcon = false;

        }

        if (defaultGrid.PageSettings.CurrentPage == 1)

        {

            DisableBackIcon = true;

            DisableFirstIcon = true;

        }

        else

        {

            DisableBackIcon = false;

            DisableFirstIcon = false;

        }

        StateHasChanged();

    }

    public string ValidateFirst()

    {

        if (DisableFirstIcon)

        {

            return "disableFirst";

        }

        return "";

    }

    public string ValidateLast()

    {

        if (DisableLastIcon)

        {

            return "disableLast";

        }

        return "";

    }

    public string ValidateForward()

    {

        if (DisableForwardIcon)

        {

            return "disableFront";

        }

        return "";

    }

    public string ValidateBack()

    {

        if (DisableBackIcon)

        {

            return "disableBack";

        }

        return "";

    }

    public void ShowNextPage()

    {

        defaultGrid.GoToPage((defaultGrid.PageSettings.CurrentPage) + 1);

    }

    public void ShowPreviousPage()

    {

        defaultGrid.GoToPage((defaultGrid.PageSettings.CurrentPage) - 1);

    }

    public void ShowFirstPage()

    {

        defaultGrid.GoToPage(1);

    }

    public void ShowLastPage()

    {

        defaultGrid.GoToPage(totalPages);

    }

    public void ShowCurrentPage(int pClickedPage)

    {

        defaultGrid.GoToPage(pClickedPage);

    }

    public void LaunchEnteredPage(Microsoft.AspNetCore.Components.ChangeEventArgs page)

    {

        if (page.Value == null || page.Value.ToString() == "")

        {

            return;

        }

        try

        {

            if (Convert.ToDouble(page.Value) < 0)

                defaultGrid.GoToPage(1);

            else if (Convert.ToDouble(page.Value) > totalPages)

                defaultGrid.GoToPage(totalPages);

            else

            {

                double enteredPage = Convert.ToDouble(page.Value);

                if (enteredPage <= totalPages)

                    defaultGrid.GoToPage(enteredPage);

            }

        }

        catch { return; }

    }

}



RN Rahul Narayanasamy Syncfusion Team January 24, 2022 01:13 PM UTC

Hi Bossink, 

Thanks for the update. 

We are happy to hear that you have achieved your customized requirement by using Pager Template with the previously provided solution. Please get back to us if you need further assistance. 

Regards, 
Rahul 


Loader.
Up arrow icon