`
wiwika
  • 浏览: 1547 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

修改will_paginate支持ajax方式

阅读更多
网上关于will_paginate支持ajax的文章不少,可是看了一下,都比较旧了,在最新更新的will_paginate插件下用不了,所以就自己改了一下
在ViewHelper.rb中
 @@pagination_options = {
      :class        => 'pagination',
      :prev_label   => '« Previous',
      :next_label   => 'Next »',
      :inner_window => 4, # links around the current page
      :outer_window => 1, # links around beginning and end
      :separator    => ' ', # single space is friendly to spiders and non-graphic browsers
      :param_name   => :page,
      :params       => nil,
      :renderer     => 'WillPaginate::LinkRenderer',
      :page_links   => true,
      :container    => true
    }

增加update 修改为:
 @@pagination_options = {
      :class        => 'pagination',
      :prev_label   => '« Previous',
      :next_label   => 'Next »',
      :inner_window => 4, # links around the current page
      :outer_window => 1, # links around beginning and end
      :separator    => ' ', # single space is friendly to spiders and non-graphic browsers
      :param_name   => :page,
      :params       => nil,
      :renderer     => 'WillPaginate::LinkRenderer',
      :page_links   => true,
      :container    => true,
      :update       => nil
    }

page_link_or_span方法修改为:
def page_link_or_span(page, span_class = 'current', text = nil)
      text ||= page.to_s
      if page and page != current_page
        if(@options[:update] != nil)
          @template.link_to_remote text, :url=>url_options(page),:update=>@options[:update]
        else
          @template.link_to text, url_options(page)
        end
      else
        @template.content_tag :span, text, :class => span_class
      end
    end

在页面上:普通链接方式:
<%=will_paginate @users, {:class=>'pagination', :prev_label   => '上一页',:next_label   => '下一页'}%>  


ajax方式:
<%=will_paginate @users, {:class=>'pagination', :prev_label   => '上一页',:next_label   => '下一页',:update => 'content'}%>  


分享到:
评论
3 楼 wiwika 2008-05-01  
AjaxLinkRenderer类:
class AjaxLinkRenderer < WillPaginate::LinkRenderer
  def page_link_or_span(page, span_class = 'current', text = nil)
    text ||= page.to_s
    if page and page != current_page
        @template.link_to_remote text, :url=>url_options(page),:update=>@options[:update]
    else
      @template.content_tag :span, text, :class => span_class
    end
  end
end

页面:
<%=will_paginate @users, {:class=>'pagination', 
  :prev_label   => '上一页',
  :next_label   => '下一页',
  :renderer => 'AjaxLinkRenderer',
  :update => 'content'}%>  
2 楼 wiwika 2008-05-01  
Quake Wang 写道
will_paginate接受一个renderer参数,默认是WillPaginate::LinkRenderer,更好的做法是subclass一个AjaxLinkRenderer,这样不需要修改它的源代码,在做plugin升级的时候会很方便。

是这样的,之前没怎么仔细读源码,惭愧
1 楼 QuakeWang 2008-05-01  
will_paginate接受一个renderer参数,默认是WillPaginate::LinkRenderer,更好的做法是subclass一个AjaxLinkRenderer,这样不需要修改它的源代码,在做plugin升级的时候会很方便。

相关推荐

    will_paginate 2.2.2

    `will_paginate`支持国际化,可以方便地调整分页文字。 五、兼容性与更新 尽管2.2.2是较老的版本,但`will_paginate`一直保持更新,以适应Rails的新版本。如果你的项目还在使用这个版本,可能需要考虑升级到更现代...

    will_paginate分页插件

    `will_paginate`不仅支持ActiveRecord,还支持Mongoid和其他ORM系统。 2. **国际化** 支持多语言,可以方便地为不同的语言环境定制分页提示文本。 3. **自定义渲染** 提供了自定义分页链接渲染器的能力,方便与...

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

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

    will_paginate插件下载

    在传统的安装方式中,开发者通常会通过RubyGems和Bundler来安装will_paginate。但由于某些特定情况,如在文中所述,直接下载插件文件可能是更方便的选择。首先,你需要访问will_paginate的GitHub仓库或官方网站,...

    scaffolding+will_paginate+提供will_paginate下载

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

    will_paginate 2.1.0

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

    will_paginate v2.3.11

    2. **兼容性**:除了支持Rails,will_paginate还兼容其他ORM,如DataMapper和Sequel,使得在不同框架间的迁移更为便捷。 3. **模板辅助**:在视图层,will_paginate提供了方便的辅助方法,如`will_paginate`和`page...

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

    will_paginate 带无限滚动示例应用程序 使用gem 'will_paginate', '~&gt; 3.0.7'实现无限/无限滚动的工作 Rails CRUD 应用程序示例。 gem 'faker'被用来为项目gem 'faker'虚假数据。 演示 现场演示: : 在本地开发中...

    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 ...

    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' 有关...

    WillPaginateExample:Ruby on Rails的Will_Paginate示例

    `will_paginate`支持自定义链接样式。例如,如果你正在使用Bootstrap,可以这样设置: ```ruby &lt;%= will_paginate @posts, renderer: WillPaginate::ActionView::BootstrapPagination::LinkRenderer %&gt; ``` #### ...

    ruby will_paginate的用法

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

    ruby on rails 常用插件下载

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

    rails 2.0.2 分页 需另外下载插件

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

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

    Ruby-on-Rails、Sinatra、Merb、DataMapper 和 Sequel 这些都是 Ruby 开发领域中的关键组件,而 `will_paginate` 是一个在这些框架中广泛使用的分页库。它提供了简单、灵活的接口,帮助开发者在处理大量数据时进行...

    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

    resume 用Ruby技术实现的search 分页的例子

    Rails提供了`will_paginate`或`kaminari`这样的插件来简化分页实现。以`will_paginate`为例,首先添加到Gemfile并安装: ```ruby gem 'will_paginate' gem 'will_paginate-bootstrap' # 如果你想使用Bootstrap样式 ...

Global site tag (gtag.js) - Google Analytics