Coder Social home page Coder Social logo

pagedresults.cs's Introduction

PagedResults

A C# Generic Class to paginate an IQueryable.

How To Use

Get a PagedResult Object

return new PagedResults<ModelName>
            (_Context.DbSetName,
                pageNumber,
                resultsPerPage);

Razorpages Partial

<nav aria-label="Page navigation example">
    <ul class="pagination">
        @if (@Model.PagedResults.CurrentPage != 1) // Show a link to the first page as well as previous page as long as we are not on the first page.
        {
            <li class="page-item"><a href="./@Model.PagedResults.FirstPage" class="page-link">First</a></li>
            <li class="page-item">
                <a href="./@Model.PagedResults.PreviousPage" class="page-link">
                    <span aria-hidden="true">&laquo;</span>
                    <span class="sr-only">Previous</span>
                </a></li>
        }
        
        @{ var pageCount = @Model.PagedResults.PageCount; }
        
        @for (int i = 1; i <= pageCount && i < 10; i++)
        {
            var currentPage = @Model.PagedResults.CurrentPage;
            if (pageCount > 10)
            {
                var activePage = ((currentPage - 5) + i);
                var active = activePage == currentPage ? "active" : string.Empty;
                if (activePage <= (pageCount - 1) && (activePage > 0))
                {
                    <li class="page-item @active"><a href="./@activePage" class="page-link">@activePage</a></li>
                }
            }
            else
            {
                var active = i == currentPage ? "active" : string.Empty;
                <li class="page-item @active"><a href="./@i" class="page-link">@i</a></li>
            }
        }
        
        
        
        @if (@Model.PagedResults.CurrentPage != @Model.PagedResults.LastPage)
        {
            <li class="page-item"><a href="./@Model.PagedResults.NextPage" class="page-link">
                <span aria-hidden="true">&raquo;</span>
                <span class="sr-only">Next</span>
            </a></li>
            <li class="page-item"><a href="./@Model.PagedResults.LastPage" class="page-link">Last</a></li>
        }
    </ul>
</nav>

Further Reading

For further reading and to understand how this works in detail read our developer's writeup on this class https://nsuchy.me/2020/04/08/easily-paginating-your-entityframework-core-queries-with-c-generics/

pagedresults.cs's People

Contributors

elaine-jackson avatar

Stargazers

Ludvig Falck avatar Calin avatar  avatar

Watchers

James Cloos avatar  avatar Lunar avatar

Forkers

elaine-jackson

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.