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.
Schritte zur Integration von will_paginate mit Rails :
Schritt 1 : Fügen Sie „gem will_paginate“ zu Ihrer Gemfile hinzu
Gemfile:
gem ‘will_paginate’, ‘~> 3.0’
Schritt 2 : Bundle Install
Führen Sie die „Bundle-Installation“ Ihres Rails-Projekts durch
Bundle-Installation:
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.
Schritt 3 : Grundlegende Integration
Fügen Sie dem Modell, das nach dem paginierten Ergebnissatz abgefragt wird, einen Paginierungsparameter hinzu. Fügen wir nun unserer Anwendung eine Paginierung hinzu. Öffnen Sie Ihren Controller erneut und ändern Sie ihn so, dass er wie der folgende Code aussieht.
Controller-Code :
app/controllers/articles_controller.rb:
Klasse ArticlesController < ApplicationController
Def-Index
@articles = Blog.paginate(:page => params[:page], :per_page => 10)
Ende
Ende
Es würde der folgenden Sammlung von Datensätzen erforderliche Parameter hinzufügen, um Paginierungslinks im Front-End (.erb) anzuzeigen. Hinzugefügte Parameter:
- current_page – Dies ist die aktuelle Seitenzahl für den paginierten Ergebnisdatensatz
- 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
Parameter :
- :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
Code anzeigen :
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 => “«”, :next_label => “»”,: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>
Standardansicht-Helfer :
Gibt HTML zurück, das Seitenlinks für ein WillPaginate::Collection-ähnliches Objekt darstellt. Falls insgesamt nicht mehr als eine Seite vorhanden ist, wird Null zurückgegeben.
- :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)
Alle von will_paginate nicht erkannten Optionen werden zu HTML-Attributen im Containerelement für Paginierungslinks
<%= will_paginate @posts, :style => ‘color:blue’ %>
page_entries_info(Sammlung, Optionen)
Rendert eine Nachricht, die die Anzahl der angezeigten Einträge im Vergleich zur Gesamtzahl der Einträge enthält.
<%= 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
Lesen Sie ähnliche Artikel: