Pagination dans Rails avec la gemme will_paginate

The world is spinning faster and faster and this acceleration is evident in all facets of our lives. Especially when it comes to business, the premium is on the speed. In this frantic accelerated, volatility is the only constant and people have great affinity towards things that can save time. The popularity of Ruby on Rails is owing to the fact that what could take months and years for other programming languages to complete can be built in weeks and days with Ruby on Rails. So how does Ruby on Rails manage to build applications in such short time? Among other things, Ruby gems are one of most important contributing factors in making Ruby on Rails reduce development overheads. These gems are a list of categorized reusable codes helping the developers build application at a faster rate. One of the most important gem among a horde of gems is pagination gem. Pagination which is a really important aspect of any web application help in dividing documents into discrete pages. In Ruby on Rails we can easily paginate data using a gem called ‘will_paginate’. The will_paginate library makes adding pagination functionality to Rails apps (and other Ruby frameworks) effortless. will_paginate is very well designed plugin. Besides ActiveRecord object integration, it can integrate with array and any collection.

Étapes d'intégration de will_paginate avec Rails :

Étape 1 : Ajoutez « gem will_paginate » dans votre Gemfile

Fichier gemme :

gem ‘will_paginate’, ‘~> 3.0’

Étape 2 :  Bundle Install

Effectuez une « installation groupée » de votre projet Rails

Installation groupée :

This command has to be executed in Rails Application directory which will install gem ‘will_paginate’ in Ruby environment if it is not installed already. If this gem is already available in your environment then it will just use the gem will Rails Application.

Étape 3 : Intégration de base

Ajoutez un paramètre de pagination au modèle interrogé pour le jeu de résultats paginé. Maintenant, ajoutons la pagination à notre application. Ouvrez à nouveau votre contrôleur et modifiez-le pour qu'il ressemble au code ci-dessous.

Code du contrôleur :

app/controllers/articles_controller.rb :

classe ArticlesController <ApplicationController

indice déf

@articles = Blog.paginate(:page => params[:page], :per_page => 10)

fin

fin

Cela ajouterait les paramètres requis dans la collection d'enregistrements suivante pour afficher les liens de pagination dans le front-end (.erb). Paramètres ajoutés :

  • current_page – il s'agit du numéro de page actuel pour l'ensemble de données de résultats paginés
  • total_entries – numbers of records in database which satisfies the given criteria
  • limit – per page limit for the paginated result data
  • offset – current paginated data set -> to show current page

Paramètres :

  • :page – This is parameter sent in Query String. Based on this, which records are to be fetched is decided.
  • :per_page – This is Number of results that you want to fetch per page

Voir le code :

app/views/articles/index.html.erb

<div class="”row" margin-twenty5-zero margin-b-zero pagination-row”>

<div class="”col-lg-8" col-md-8 col-sm-7 col-xs-6 padding-zero”>

<ul class=”pagination”>

<li class=”disabled”> <%= will_paginate(@articles,:previous_label => “&laquo;”, :next_label => “&raquo;”,:class=>”small-font”,:outer_window => 1,:inner_window => 0) %></li>

</div>

<div class="”col-lg-4" col-md-4 col-sm-5 col-xs-6 pagination-info”>

<span class="”pull-right”"><%= page_entries_info(@articles) %></span>

</div>

</div>

Assistants de vue standard :

Renvoie le HTML représentant les liens de page pour un objet de type WillPaginate::Collection. S’il n’y a pas plus d’une page au total, zéro est renvoyé.

  • :class – CSS class name for the generated DIV (default: “pagination”)
  • :previous_label – default: “« Previous”
  • :next_label – default: “Next »”
  • :page_links – when false, only previous/next links are rendered (default: true)
  • :inner_window – how many links are shown around the current page (default: 4)
  • :outer_window – how many links are around the first and the last page (default: 1)
  • :link_separator – string separator for page HTML elements (default: single space)
  • :param_name – parameter name for page number in URLs (default: :page)
  • :params – additional parameters when generating pagination links (eg. :controller => “foo”, :action => nil)
  • :renderer – class name, class or instance of a link renderer (default in Rails: WillPaginate::ActionView::LinkRenderer)
  • :container – toggles rendering of the DIV container for pagination links, set to false only when you are rendering your own pagination markup (default: true)

Toutes les options non reconnues par will_paginate deviendront des attributs HTML sur l'élément conteneur pour les liens de pagination

<%= will_paginate @posts, :style => ‘color:blue’ %>
page_entries_info(collection, options)
Affiche un message contenant le nombre d'entrées affichées par rapport au nombre total.

<%= page_entries_info @posts %>
#=> Displaying posts 6 – 12 of 26 in total

  • :model – a model class or string name (default: collection.first.class)
  • :html – set to false to generate plain text (default: true)

GitHub : https://github.com/mislav/will_paginate

Lire des articles similaires :

Abonnez-vous pour les dernières mises à jour

Articles Similaires

Laissez un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

French
English
English
Japanese
German
French
Spanish

WhatsApp nous

Quitter la version mobile