Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sortable_table

> 编程语言
Open source

Sort HTML tables in your Rails app.

157 stars0 likes2 views
WebsiteGitHub

About

Sort HTML tables in your Rails app.

DEPRECATED

Thoughtbot is no longer supporting this project. We have not used it a long time. If you would like to maintain it, please email [email protected].

Sortable Table

Sort HTML tables in your Rails app.

Install

script/plugin install git://github.com/thoughtbot/sortable_table.git

In app/controllers/application_controller.rb:

class ApplicationController  "groups.name"

  context "GET to #index" do
    setup { get :index }

    should_display_sortable_table_header_for :name, :email, :age, :group
  end
end

This is the common case for a RESTful UsersController.

  • should_sort_by_attributes tests that the controller's index action can sort by the attributes.
  • should_display_sortable_header_for tests that a sortable header displays for the attributes.

Controller

class UsersController  "groups.name"

  def index
    @users = User.paginate :page => params[:page], :order => sort_order
  end
end

sortable_attributes defines a sort_order method that gets called in your action.

If the index action is rendered without a params[:sort] option, @users will be sorted by :name, the first option in the list of sortable_attributes.

View

Users

     "Name",  :sort => "name" %>
     "Email", :sort => "email" %>
     "Age",   :sort => "age" %>
     "Group", :sort => "group" %>

sortable_table_header creates a table header containing a link with the correct :sort and :order params. It also has a class of "ascending" or "descending" so you can add styles with arrows. You can add your own styles as well.

Example styles

th.ascending a {
  background: url(/images/sort-ascending-arrow.gif) 0% 50% no-repeat;
  padding-left: 15px;
}

th.descending a {
  background: url(/images/sort-descending-arrow.gif) 0% 50% no-repeat;
  padding-left: 15px;
}

Overriding should_sort_by_attributes

Opinionated defaults:

  • GET to :index
  • collection same name as controller (@users for UsersController)
  • model name same name as controller (User for UsersController

If you need to test another action (or a nested controller), pass a block:

should_sort_by_attributes :age do |sort, order|
  get :show, :sort => sort, :order => order, :group_id => @group.id
end

If you need to test another collection or model name, use should_sort_by.

Overriding should_sort_by

The :collection, :model_name, and :action options of should_sort_by.

context "with a non-standard collection name" do
  action = lambda { |sort, order| get :members, :sort => sort, :order => order }
  should_sort_by :name, { :collection => "members",
                          :model_name => "user",
                          :action     => action } do |user|
    user.name
  end
end

sort_order

The default sort order is descending. This applies to the first time you click on a table header. You can override this to be ascending:

def index
  @users = User.find :all, :order => sort_order(:default => "ascending")
end

Credits

Thank you to all the contributors.

License

Sortable Table is Copyright © 2008-2011 thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言