Skip to content

knowyourcompany/infinite-page

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Infinite scrolling and pagination for Rails 3.1+

Requirements:

  • jQuery
  • Underscore.js
  • CoffeeScript
  • Rails 3.1+

Installing

Gemfile

gem 'infinite_page', '~> 0.4.0'

app/assets/javascripts/application.js.coffee

#= require infinite_page

$(document).ready ->
  $('[data-behavior~=infinite_page]').infinitePage()

Using

Add the infinite_page behavior to an html element that contains the records you want to paginate. As you scroll and approach the bottom of the page, infinite_page will make an ajax request for the next page and the results will be appended to the container. This will continue until an empty response is returned.

<!-- app/views/posts/index.html.erb -->
<section data-behavior="infinite_page">
  <%= render @posts %>
</section>
class PostsController < ApplicationController
  def index
    @posts = @project.posts.page(current_page)

    respond_to do |format|
      format.js { render partial: @posts }
      format.html
    end
  end
end

infinite_page adds a page scope to ActiveRecord::Base that takes two arguments. The first is the page number and the second optional argument is the number of records per page (defaults to 50).

Also provided is a current_page helper method that returns the current page number from params[:page] as an integer (defaults to 1).

About

Basecamp's legacy pagination - DO NOT USE THIS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published