Git Product home page Git Product logo

pager's Introduction

Pager

Easy to use paginator for modern crystal web framework.

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  pager:
    github: imdrasil/pager
  1. Run shards install

Also if you use default bootstrap presenter ensure that you've added appropriate css to your project from the CDN or directly in the code.

Usage

Pager provides two basic container: for Array and Jennifer::QueryBuilder::ModelQuery. Also it includes basic bootstrap presenter, which is the default presenter.

require "pager"
require "pager/collections/array" # for arrays
require "pager/collections/jennifer" # for Jennifer collections

Collection objects responds to #each method which yield objects in the collection so you can easily iterate over them.

Array

Pager::ArrayCollection extends Array with new #paginate method, which accepts current page and count of elements on each one.

(1..21).to_a.paginate(1, 4) # [5, 6, 7, 8]

Jennifer

Pager::JenniferCollection extends Jennifer::QueryBuilder::ModelQuery with #paginate methods which accepts current page number and count of records in a page. This method is executable one so should be the last one in a chain.

User.all.where { _active }.paginate(1, 4)

Configuration

Pager.configure do |config|
  config.default_presenter = Pager::Bootstrap
  config.visible_pages = 7
  config.per_page = 20
end

Available configurations:

  • default_presenter - presenter class to be used by ViewHelper#paginate
  • visible_pages - number of page links generated by ViewHelper#paginate
  • per_page - default number of pages for collection

View

To render navigation section include Pager::ViewHelper to appropriate context and call #paginate method passing collection, path to be used to generate links and number of visible pages.

<%= paginate((1..100).collection(1, 5), "/", 3) %>

Will generate next html:

<nav>
  <ul class='pagination'>
    <li class="page-item ">
      <a class="page-link" href="/?page=0" aria-label="Previous">
        <span class="sr-only">Previous</span>
        <span aria-hidden="true">&lsaquo; Previous</span>
      </a>
    </li>
    <li class='page-item '>
      <a class='page-link' href='/?page=0'>1</a>
    </li>
    <li class='page-item disabled'>
      <a class='page-link' href='#'>2</a>
    </li>
    <li class='page-item '>
      <a class='page-link' href='/?page=2'>3</a>
    </li>
    <li class="page-item ">
      <a class="page-link" href="/?page=2" aria-label="Next">
        <span class="sr-only">Next</span>
        <span aria-hidden="true">Next &rsaquo;</span>
      </a>
    </li>
  </ul>
</nav>

As you can see page is added as a GET parameter page. If query already has some GET parameter this will be recognized and page will be added to the end.

Translation

The default labels for the "next" and "previous" texts are stored in the i18n yaml. It can be easily override. Here is existing translation file:

pager:
  previous_label: "&lsaquo; Previous"
  next_label: "Next &rsaquo;"

For the translation purpose crimson-knight/i18n.cr library is used.

Development

Presenter

TBA. Take a look at existing bootstrap presenter implementation.

Collection

TBA. Take a look at existing array presenter implementation.

Contributing

  1. Fork it (https://github.com/imdrasil/pager/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

pager's People

Contributors

imdrasil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cimtico

pager's Issues

Old I18n reference

Should the I18n shard reference be updated to use crimson-knight/i18n.cr to match the https://github.com/imdrasil/jennifer.cr project?

Or maybe remove the I18n reference, and leave it up to the project how to implement the internationalization/text somehow as parameters?

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.