Rails3 - 使用 Kaminari + 进行排序和分页AJAX +不显眼的 JavaScript

发布于 2024-11-03 11:36:17 字数 1599 浏览 8 评论 0原文

我已经成功地用 Kaminari 设置了 ajax 分页。

为了添加排序功能,我遵循了这个伟大的 Railscast并将 will_paginate 替换为 Kaminari。

分页工作得很好,但排序只是第一次工作,因为 sort_column 和 sort_direction 没有更新。

我不明白为什么。

这是我的代码:

控制器:

def index
 @questions = Question.order(sort_column + " " +  sort_direction).page(params[:page])
end

....

private

def sort_column
 Question.column_names.include?(params[:sort]) ? params[:sort] :  "created_at"
end

def sort_direction
 %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end

我添加了 :remote =>正确,:方法=> :get

ApplicationHelper :

def sortable(title = nil, column)
 title ||= column.titleize
 direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
 link_to title, params.merge(:sort => column, :direction => direction), :remote => true, :method => :get
end

index.html.erb :

<%= sortable "created_at" %><br/>
<div id="questions"><%= render @questions %></div>
<div id="paginator"><%= paginate @questions, :remote => true %>

index.js.erb :

$('#questions').html('<%= escape_javascript(render @questions) %>');
$('#paginator').html('<%= escape_javascript(paginate(@questions, :remote => true).to_s) %>');

这是 :remote => 的问题吗?正确,:方法=> :get 部分?如果我删除它,排序可以工作,但没有ajax。

I have successfully set up an ajax pagination with Kaminari.

In order to add a sort feature I followed this great Railscast and replaced will_paginate by Kaminari.

The pagination works great but the sort works just the first time because the sort_column and the sort _direction are not updated.

I can't figure out why.

Here is my code :

Controller :

def index
 @questions = Question.order(sort_column + " " +  sort_direction).page(params[:page])
end

....

private

def sort_column
 Question.column_names.include?(params[:sort]) ? params[:sort] :  "created_at"
end

def sort_direction
 %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end

I have added :remote => true, :method => :get to

ApplicationHelper :

def sortable(title = nil, column)
 title ||= column.titleize
 direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
 link_to title, params.merge(:sort => column, :direction => direction), :remote => true, :method => :get
end

index.html.erb :

<%= sortable "created_at" %><br/>
<div id="questions"><%= render @questions %></div>
<div id="paginator"><%= paginate @questions, :remote => true %>

index.js.erb :

$('#questions').html('<%= escape_javascript(render @questions) %>');
$('#paginator').html('<%= escape_javascript(paginate(@questions, :remote => true).to_s) %>');

Is this a problem with the :remote => true, :method => :get part ? If I remove it the sort works but without ajax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-11-10 11:36:17
@products = Product.order("name").page(params[:page])  

http://asciicasts.com/episodes/254-pagination-with-kaminari

@products = Product.order("name").page(params[:page])  

http://asciicasts.com/episodes/254-pagination-with-kaminari

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文