Giter Club home page Giter Club logo

handles_sortable_columns's Introduction

Sortable Table Columns

Introduction

A simple yet flexible Rails gem/plugin to quickly add sortable table columns to your controller and views.

Setup (Rails 3)

In your app's Gemfile, add:

gem "handles_sortable_columns"

To install the gem with RDoc/ri documentation, do a:

$ gem install handles_sortable_columns

Otherwise, do a bundle install.

Setup (Rails 2)

In your app's config/environment.rb add:

config.gem "handles_sortable_columns"

To install the gem, do a:

$ gem sources --add http://rubygems.org
$ gem install handles_sortable_columns

, or use rake gems:install.

Basic Usage

Activate the feature in your controller class:

class MyController < ApplicationController
  handles_sortable_columns
  ...

In a view, mark up sortable columns by using the sortable_column helper:

<%= sortable_column "Product" %>
<%= sortable_column "Price" %>

In controller action, fetch and use the order clause according to current state of sortable columns:

def index
  order = sortable_column_order
  @records = Article.order(order)           # Rails 3.
  @records = Article.all(:order => order)   # Rails 2.
end

That's it for basic usage. Production usage may require passing additional parameters to listed methods.

Production Usage

Please take time to read the gem's full RDoc documentation. This README has a limited coverage.

Configuration

Change names of GET parameters used for sorting and pagination:

class MyController < ApplicationController
  handles_sortable_columns do |conf|
    conf.sort_param = "s"
    conf.page_param = "p"
  end
  ...

Change CSS class of all sortable column <a> tags:

handles_sortable_columns do |conf|
  conf.class = "SortableLink"
  conf.indicator_class = {:asc => "AscSortableLink", :desc => "DescSortableLink"}
end

Change how text-based sort indicator looks like:

handles_sortable_columns do |conf|
  conf.indicator_text = {:asc => "[asc]", :desc => "[desc]"}
end

Disable text-based sort indicator completely:

handles_sortable_columns do |conf|
  conf.indicator_text = {}
end

Helper Options

Explicitly specify column name:

<%= sortable_column "Highest Price", :column => "max_price" %>

Specify CSS class for this particular link:

<%= sortable_column "Name", :class => "SortableLink" %>

Specify sort direction on first click:

<%= sortable_column "Created At", :direction => :asc %>

Fetching Sort Order

To fetch sort order securely, with column name validation, default values and multiple sort criteria, use the block form of sortable_column_order:

order = sortable_column_order do |column, direction|
  case column
  when "name"
    "#{column} #{direction}"
  when "created_at", "updated_at"
    "#{column} #{direction}, name ASC"
  else
    "name ASC"
  end
end

Feedback

Send bug reports, suggestions and criticisms through project's page on GitHub.

handles_sortable_columns's People

Contributors

dimfeld avatar tvdeyen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

handles_sortable_columns's Issues

Sorting in the context of other params

From examining the code, it looks like the sort params should be merged with any other params that should be preserved (such as a search param) here:

sortable_columns.rb line 257: url = url_for(params.merge( . . . .

but my sort links are always generated with just direction= and sort= params - it seems so simple, I can't imagine what could go wrong. Any thoughts?

Rails 3.2.13 and handles_sortable_columns (0.1.4)

uninitialized constant ActionController::Base::Handles (NameError)

Rails 3.0.0

gem install handles_sortable_columns
Gemfile:
gem "handles_sortable_columns"

And restart the server (rails s):

/usr/lib/ruby/gems/1.8/gems/handles_sortable_columns-0.1.1/lib/action_controller/base/handles_sortable_columns.rb:2: uninitialized constant ActionController::Base::Handles (NameError)
from /usr/lib/ruby/gems/1.8/gems/handles_sortable_columns-0.1.1/lib/handles_sortable_columns.rb:3
from /usr/lib/ruby/gems/1.8/gems/handles_sortable_columns-0.1.1/lib/handles_sortable_columns.rb:2:in each' from /usr/lib/ruby/gems/1.8/gems/handles_sortable_columns-0.1.1/lib/handles_sortable_columns.rb:2 from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:64:inrequire'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:64:in require' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:62:ineach'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:62:in require' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:51:ineach'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler/runtime.rb:51:in require' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.6/lib/bundler.rb:112:inrequire'
from /home/gerard/project/visualpis/config/application.rb:7
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/commands.rb:28:in require' from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/commands.rb:28 from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/commands.rb:27:intap'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0/lib/rails/commands.rb:27
from script/rails:6:in `require'
from script/rails:6

how to change desc and asc query

hi
this gem dont use DESC and ASC methode of mysql. i see that it use just ' - ' befor the name of the column used to be sorted.
its there a way to fix it please?

Ajax sortable

Hi can you please let me know how to do ajax for this gem.

Present we have
<%= sortable_column "column_name" %> if I click it loading the page, but i want instead of loading total page i want to add ajax.

I haven't seen ajax functionality to this gem. Can you please anyone explain me how to add ajax for this sortable column.

how to change desc and asc indicator

i cant find the way to change the display of indicator โ†“ and โ†‘ .
in css i do:
//
/
sotable table /
/
/

.asc{

background: url(/images/up_arrow.gif) no-repeat;
}

.desc{
margin-left: 10px;
background-image: url(/images/down_arrow.gif);
}

and in the controller:
handles_sortable_columns do |conf|
conf.class = "SortableLink"
conf.indicator_class = {:asc => "asc", :desc => "desc"}
end

what i did miss !!?
thanks.

Sortable column for referenced name in other table

Thanks for a simple and functional plugin. But I have a question:

I have a column that is a lookup of the name of a column in another table.

That is, I have cities.country_id as a column and in the View template I do:

city.country.name to display the name.

I would like to be able to sort by this column, but the countries.name, not the cities.country_id.

Is this possible with the plugin? If so how?

Thanks.

Sorting on association doesn't work for me

I saw the closed issue that another user submitted, and I tried doing something similar, but it doesn't work for me.

Course belongs_to category
Category has_many courses

  #View
 <%= sortable_column "Category", column: "category_title" %>


##Courses Controller
 def index
     order = sortable_column_order do |column, direction|
       case column
       when "category_title"
         "category.title #{direction}" 
       end
     end
     if params[:query].blank? 
       @courses = current_account.courses.order(order).paginate(:page => params[:page], :per_page => 15)
     else 
       @courses = current_account.courses.search_by_content(params[:query]).order(order).paginate(:page => params[:page], :per_page => 15)
     end
   end

Error message:

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "categories"
LINE 1: ...urses" WHERE "courses"."account_id" = 2 ORDER BY categories...
^
: SELECT "courses".* FROM "courses" WHERE "courses"."account_id" = 2 ORDER BY categories.title asc LIMIT 15 OFFSET 0

Also, how would I be able to use the methods I defined in my models?
i.e. test.average_score
=> average_score is not saved in a table but is calculuted in the test.rb model

undefined local variable or method `has_sortable_columns' for module

Pardon my ignorance, but I am getting this error:

undefined local variable or method `has_sortable_columns' for ModelFunctions:Module

When trying to use the plugin with some methods that I extend via a Module.

I extend the search method inside my controller by redeclaring it again in the module since I used it with multiple controllers. Is there a way to get the search method in the Module to understand about the sortable columns?

Thanks.

And thanks for the plugin, it's very nice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.