Skip to content

Instantly share code, notes, and snippets.

@julian7
Created January 25, 2011 09:17
Show Gist options
  • Save julian7/794700 to your computer and use it in GitHub Desktop.
Save julian7/794700 to your computer and use it in GitHub Desktop.
2dc_jqgrid + rails3
class UsersController < ApplicationController
extend ActiveSupport::Memoizable
...
def index
@@index_columns ||= ['username', 'email', 'comments']
@users = User.scoped
if params[:_search] == "true"
@@index_columns.each do |param|
if params[param].present?
@users = @users.where(param.matches("%#{params[param]}%"))
end
end
end
if order_by
@users = @users.order(order_by)
end
@users = @users.paginate(:page => params[:page], :per_page => params[:rows] || 10)
respond_to do |format|
format.html
format.json do
render :json => @users.to_jqgrid_json(
@@index_columns,
params[:page], params[:rows], @users.total_entries
)
end
end
end
private
def order_by
if sort_index and sort_order
sort_index + " " + sort_order
else
nil
end
end
def sort_index
if @@index_columns.include?(params[:sidx])
params[:sidx]
else
nil
end
end
def sort_order
if %w[asc desc].include?(params[:sord])
params[:sord]
else
nil
end
end
memoize :order_by, :sort_index, :sort_order
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment