简体   繁体   English

使用will_paginate进行分页排序的结果集

[英]Ordering a results set with pagination using will_paginate

I'm using will_paginate for pagination in a Rails 3 site. 我在Rails 3站点中使用will_paginate进行分页。 I'd like to be able to click on the table headers (eg "name", "created at") for each of the output fields and have the results ordered on that field. 我希望能够单击每个输出字段的表标题(例如“名称”,“创建于”),并在该字段上排序结果。 It would also be nice to be able to click again for reverse ordering. 能够再次单击以进行反向排序也很好。 Is there something that works well with will_paginate for ordering? 是否有一些可以与will_paginate配合使用的东西? Can I do this just using will_paginate? 我可以只使用will_paginate来做到这一点吗?

The table format I'm using is pretty generic: 我使用的表格格式非常通用:

<%= will_paginate %>
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Updated</th>
    </tr>   
    <% @shops.each do |shop| %>
    <tr>
        <td><%= shop.id %></td>
        <td><%= shop.name %></td>
        <td><%= shop.updated_at.strftime("%a %b %d %H:%M ") %></td>
    </tr>
    <% end %>
</table>
<%= will_paginate %>

您将在此处找到所有期望的插图: http : //railscasts.com/episodes/240-search-sort-paginate-with-ajax

I am using 我在用

gem "meta_search"

with will_paginate 与will_paginate

Github project site Github项目现场

Railscasct #251 MetaWhere & MetaSearch Railscasct#251 MetaWhere和MetaSearch

Index controller example 索引控制器示例

@search =  Customer.search(params[:search])
@customer = @search.paginate(:page => params[:page], :per_page => 50)

and view example 并查看示例

<th><%= sort_link @search, :name%></th>
<th><%= sort_link @search, :surname %></th>
<th><%= sort_link @search, :address %></th>

Alternatives: 备择方案:

1) Using powerful jquery plugin: 1)使用功能强大的jquery插件:

Sorting and pagination became simple with jquery plugin. 使用jQuery插件,排序和分页变得简单。

There is a powerful jquery plugin here . 有一个强大的jQuery插件这里

More for jQuery Tablesorter: Pagination and Sorting made simple . jQuery Tablesorter的更多内容:分页和排序变得简单

2) Sortable Table Columns: 2)可排序的表格栏:

If you want sorting without jquery plugin then you will get everything you want here . 如果您想在没有jquery插件的情况下进行排序,那么您将在这里得到想要的一切。 The above method adds the ability to sort table columns in ascending or descending order by clicking the header. 通过单击标题,上述方法增加了按升序或降序对表列进行排序的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM