Luilliarcec.Pagination 1.1.0

dotnet add package Luilliarcec.Pagination --version 1.1.0                
NuGet\Install-Package Luilliarcec.Pagination -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Luilliarcec.Pagination" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Luilliarcec.Pagination --version 1.1.0                
#r "nuget: Luilliarcec.Pagination, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Luilliarcec.Pagination as a Cake Addin
#addin nuget:?package=Luilliarcec.Pagination&version=1.1.0

// Install Luilliarcec.Pagination as a Cake Tool
#tool nuget:?package=Luilliarcec.Pagination&version=1.1.0                

NET Framework Pagination

latest version Azure DevOps tests downloads GitHub license

Allows paging of data from an IOrderedQueryable object.

Installation

You can install the package via nuget:

dotnet add package Luilliarcec.Pagination

Usage

Whether you use Code First or if you use Database First, implement the Interface exposed in the Package as IPaginable in your Models or Entities.

using Luilliarcec.Pagination.Contracts;
// ...

namespace Entities.Models
{
    public class User : IPaginable
    {
        // ...
    }
}

Once this is done, in your data layer, where you access the database to obtain the records, you can use the following example to make your queries.

using Luilliarcec.Pagination;
// ...

namespace Data.DataAccessObject
{
    public class UsersDAO
    {
        // ...

        public IDictionary<string, object> GetUsers(string option, int current_page, string searched_phrase)
        {
            var query = _context.Providers.Where(
                model =>
                    model.Name.Contains(searched_phrase) ||
                    model.Email.Contains(searched_phrase)
                ).OrderBy(mod => mod.Name);

            return Pagination.Paginate(query, option, current_page, 10);
        }
    }
}

In this way you can make queries, order your records by the field you want, etc. However, remember that the Paging function receives an object of type IOrderedQueryable, so it is vital that your query is ordered before paging it.

The parameters that the paging function receives are:

Parameters Type Description
query IOrderedQueryable Query on which the pagination will be applied
option string Option or type of pagination that can only be, first, next, previous, last, current
current_page int Receive the current page you are working on
limit int Limits of records per page

This function will return a dictionary of <string, object>, in which we will find the following values.

Keys Values Description
total int Returns total records
per_page int Returns records per page
current_page int Returns current page you are working on
last_page int Returns the last page or total pages
data IReadOnlyList<IPaginable> Returns paged records in a read-only list

In your view you can manage yourself in the following way.:

using Luilliarcec.Pagination.Contracts;
// ...

namespace Views.Users
{
    public partial class FormManageUsers : Form
    {
        private int _current_page;

        // ...

        private void ListData(string option = "first", string searched_phrase = "", int current_page = 1)
        {
            var data = _dao.GetUsers(option, current_page, searched_phrase);

            var items = (IReadOnlyCollection<IPaginable>)data["data"];
            // OR // var items = (IReadOnlyCollection<object>)data["data"];

            DgvProviders.Rows.Clear();

            foreach (User item in items)
            {
                DgvProviders.Rows.Add(
                    item.Id,
                    item.Name,
                    item.Email,
                    item.Address
                );
            }

            _current_page = (int)data["current_page"];

            LbCurrentPage.Text = _current_page.ToString();
            LbTotalPages.Text = ((int)data["last_page"]).ToString();
        }

        private void BtnFirstPage_Click(object sender, EventArgs e)
        {
            ListData("first", TxtSearch.Text);
        }

        private void BtnNextPage_Click(object sender, EventArgs e)
        {
            ListData("next", TxtSearch.Text, _current_page);
        }

        private void BtnPreviousPage_Click(object sender, EventArgs e)
        {
            ListData("previous", TxtSearch.Text, _current_page);
        }

        private void BtnLastPage_Click(object sender, EventArgs e)
        {
            ListData("last", TxtSearch.Text);
        }
    }
}

Remember not to manipulate the value of current_page because the package works for you to increase and decrease its value, in addition to validating that it is not out of range.

Follow these tips and have a happy code.

Security

If you discover any security related issues, please email luilliarcec@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 535 4/13/2020
1.0.0 451 4/7/2020

Pagination data.