https://github.com/imdrasil/pager
Easy to use paginator for modern crystal web framework
https://github.com/imdrasil/pager
crystal pagination paginator
Last synced: 5 months ago
JSON representation
Easy to use paginator for modern crystal web framework
- Host: GitHub
- URL: https://github.com/imdrasil/pager
- Owner: imdrasil
- License: mit
- Created: 2018-12-17T22:21:55.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-09-25T20:56:00.000Z (about 3 years ago)
- Last Synced: 2025-04-21T08:14:05.313Z (6 months ago)
- Topics: crystal, pagination, paginator
- Language: Crystal
- Size: 19.5 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pager
Easy to use paginator for modern crystal web framework.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
pager:
github: imdrasil/pager
```2. 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.
```crystal
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.
```crystal
(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.
```crystal
User.all.where { _active }.paginate(1, 4)
```### Configuration
```crystal
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.
```erb
<%= paginate((1..100).collection(1, 5), "/", 3) %>
```Will generate next html:
```html
```
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:
```yaml
pager:
previous_label: "‹ Previous"
next_label: "Next ›"
```
For the translation purpose `crimson-knight/i18n.cr` library is used.
## Development
### Presenter
TBA. Take a look at existing [bootstrap](./src/pager/presenters/bootstrap.cr) presenter implementation.
### Collection
TBA. Take a look at existing [array](./src/pager/collections/array.cr) presenter implementation.
## Contributing
1. Fork it ()
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
- [Roman Kalnytskyi](https://github.com/imdrasil) - creator and maintainer