Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yannvery/7463886 to your computer and use it in GitHub Desktop.
Save yannvery/7463886 to your computer and use it in GitHub Desktop.
How to paginate on an ajax way ( with jquery and kaminari )

Paginate with Kaminari without reload all data

Use :remote => true option on link_to and paginate Region index page

gem 'kaminari'
class RegionsController < ApplicationController
def index
@current_page = 1
@regions = Region.page(@current_page).per(10)
end
end
<div id="content-header">
<h1>Regions</h1>
</div>
<div id="breadcrumb">
<%= link_to raw('<i class="icon-home"></i> Home'), root_path, :class => "tip-bottom", "data-original-title" => "Go to Home" %>
<a href="#" class="current">Regions</a>
</div>
<div id="regions">
<%= render "paginate_regions/index" %>
</div>
class PaginateRegionsController < ApplicationController
def index
@current_page = params[:page]
@regions = Region.page(@current_page).per(10)
end
end
<div class="row">
<div class="col-xs-12">
<div class="widget-box">
<div class="widget-title">
<span class="icon">
<i class="icon-screenshot"></i>
</span>
<h5>Regions</h5>
</div>
<div class="widget-content">
<div class="span6"><%= link_to 'New Region', new_region_path, :class => "btn btn-primary" %></div>
</div>
<div class="widget-content nopadding">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<% @regions.each do |region| %>
<tr>
<td><%= region.id %></td>
<td><%= link_to region.name, region %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'Previous', paginate_regions_path(:page => @regions.current_page - 1), :remote => true unless @regions.first_page? %> |
<%= link_to 'Next', paginate_regions_path(:page => @regions.current_page + 1), :remote => true unless @regions.last_page? %>
</div>
</div>
</div>
</div>
$('#regions').empty();
$('#regions').append('<%= escape_javascript(render partial: 'index') %>');
resources :paginate_regions, :only => [:index]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment