`
tim.teng
  • 浏览: 9956 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

will_paginate ajax pagination

阅读更多

Ajax pagination

This is the most popular feature request for will_paginate library. Reasons why the core library doesn’t support this are:

  1. pagination is a concept decoupled from HTTP requests and rendering;
  2. Ajax pagination is done differently across web frameworks;
  3. Ajax is done differently across JavaScript libraries (not everyone uses Prototype);
  4. The JavaScript side of the Ajax pagination concept is not trivial at all.

So the short reason why you can’t get Ajax pagination out-of-the-box with this library is that this kind of functionality belongs to user code.

Here are some examples to get you on the right track.

Warning:
don’t use the “RemoteLinkRenderer” or similar solutions you might find on blogs using Rails’ link_to_remote helper. Such solutions are obtrusive and will usually make your app broken for web spiders and difficult to debug.

Basic unobtrusive Ajax pagination for Rails

Let’s suppose you have a search box and you want to paginate search results. The following code assumes that:

  1. the partial that renders those results is named “search_results”;
  2. the same partial renders all the results wrapped in a DIV with an ID “results”;
  3. the “results” DIV also contains the pagination links;
  4. you have a graphic progress indicator (a “spinner”) saved in “images/spinner.gif”.
# app/views/posts/_search_results.html.erb

<div id="results">
  <% for post in @posts %>
    ... render each post ...
  <% end %>
  <%= will_paginate @posts %>
</div>

First of all, make sure your controller responds to Ajax requests by updating the “results” DIV :

# app/controllers/posts_controller.rb

def index
  @posts = Post.paginate :page => params[:page]

  respond_to do |format|
    format.html
    format.js {
      render :update do |page|
        # 'page.replace' will replace full "results" block...works for this example
        # 'page.replace_html' will replace "results" inner html...useful elsewhere
        page.replace 'results', :partial => 'search_results'
      end
    }
  end
end

Next, the unobtrusive JavaScript code:

# public/javascripts/application.js

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
    var img = new Image
    img.src = '/images/spinner.gif'

    function createSpinner() {
      return new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }
})

分享到:
评论

相关推荐

    will_paginate分页插件

    **will_paginate分页插件详解** 在Web开发中,数据分页是一项常见的需求,它可以帮助用户更有效地浏览大量信息,提高用户体验。Rails框架中,`will_paginate`是一个非常流行且强大的分页插件,它简洁易用,功能强大...

    will_paginate 2.2.2

    `will_paginate`是Rails框架中非常流行的一个分页插件,版本2.2.2是它的一个历史版本。在本文中,我们将深入探讨`will_paginate`的使用方法、功能特性以及它如何与Rails集成,帮助提升Web应用的性能和用户体验。 一...

    will_paginate-bootstrap, 将 Twitter Bootstrap 分页组件与will_paginate集成.zip

    will_paginate-bootstrap, 将 Twitter Bootstrap 分页组件与will_paginate集成 will_paginate-bootstrap不再维护我不再使用 Bootstrap 来使用 Rails,所以不幸的是,我不再接受请求请求或者维护这个库。 为了发布你...

    scaffolding+will_paginate+提供will_paginate下载

    在Rails项目中,安装`will_paginate`非常简单,只需要在Gemfile中添加gem `'will_paginate'`,然后执行`bundle install`即可。 `will_paginate`的使用通常包括以下几个步骤: 1. **引入Gem**:首先,在Gemfile中...

    will_paginate插件下载

    《will_paginate插件详解与应用》 在Web开发中,数据分页是常见的功能,尤其是在处理大量数据时,为了提高用户体验和加载速度,我们通常会将数据分页展示。will_paginate是一款广受欢迎的Ruby on Rails插件,用于...

    will_paginate v2.3.11

    《will_paginate v2.3.11:Rails分页插件详解》 在Web开发中,处理大量的数据是常见的需求,特别是在使用Ruby on Rails框架时。为了优化用户体验,提高页面加载速度,分页功能显得尤为重要。这时,will_paginate...

    will_paginate 2.1.0

    `will_paginate` 是一个非常流行的 Ruby on Rails(Rails)框架中的分页库,它为开发者提供了方便的方式来处理大量数据的分页展示。版本 `2.1.0` 是这个库的一个特定版本,包含了该库在当时的一些特性、优化和可能的...

    will_paginate_endless_scroll_example:如何使用 will_paginate gem 创建无限滚动的演示

    cd will_paginate_endless_scroll_example 安装依赖: bundle install 迁移和种子数据库: bundle exec rake db:migrate db:seed 就是这样,它准备好了。 跑步: 像普通的 rails 应用程序一样运行: rails s ...

    will_paginate-bootstrap4:钩入will_paginate以格式化html以匹配Twitter Bootstrap 4样式

    gem 'will_paginate-bootstrap4' 用法 &lt;&#37;= will_paginate @clients, renderer: WillPaginate::ActionView::BootstrapLinkRenderer %&gt; 以下选项可用(除了will_paginate中可用的选项之外): :list_classes ...

    WillPaginateExample:Ruby on Rails的Will_Paginate示例

    **Will_Paginate在Ruby on Rails中的应用** `Will_Paginate` 是一个非常流行的Ruby on Rails插件,用于处理大型数据集的分页。它提供了简洁、灵活的API,使得在Rails应用中实现数据库查询结果的分页变得简单易行。...

    ruby will_paginate的用法

    新版本的will_paginate已经抛弃了这样的做法,转而使用gem的方式,本文通过建立一个名为foobar的应用来了解一下will_paginate的用法。 ============================== C:\&gt;rails foobar -d mysql C:\&gt;cd foobar C:\...

    rails 2.0.2 分页 需另外下载插件

    为了实现分页,开发者通常需要安装并使用第三方插件,比如"will_paginate"。这个插件允许你在Rails应用中轻松地对数据进行分页显示,提高用户体验并减轻服务器压力。 **1. will_paginate插件介绍** `will_paginate`...

    ruby on rails 常用插件下载

    此外,`will_paginate` 还支持 AJAX 效果,以便无刷新地切换页面。通过使用 `remote: true` 参数,可以轻松实现这一功能: ```erb &lt;%= will_paginate @posts, remote: true %&gt; ``` 在对应的 JavaScript 文件中,你...

    will_paginate:用于Rails,Sinatra,Merb,DataMapper等的分页库

    will_paginate will_paginate是一个分页库,与Ruby on Rails,Sinatra,Hanami :: View,Merb,DataMapper和Sequel集成。 安装: ## Gemfile for Rails 3+, Sinatra, and Merbgem 'will_paginate' , '~&gt; 3.1.0' 有关...

    Ruby-willpaginate一个分页库可与RubyonRailsSinatraMerbDataMapper和Sequel集成

    在 Sinatra 中,`will_paginate` 需要通过中间件 `sinatra-pagination` 来集成。配置好中间件后,同样可以使用类似 ActiveRecord 的方法来实现分页: ```ruby get '/posts' do @posts = Post.paginate(params[:...

    Python库 | flask-paginate-0.4.4.zip

    from flask_paginate import Pagination, get_page_args app = Flask(__name__) app.config['PAGINATION_PAGE_RANGE'] = 5 # 显示5个页码 app.config['PAGINATION_PER_PAGE'] = 20 # 每页显示20条数据 ``` 三、...

    Python库 | mkdocs_paginate_plugin-0.0.3-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:mkdocs_paginate_plugin-0.0.3-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

Global site tag (gtag.js) - Google Analytics