Module: JqgridForRails::Controllers::Helpers
- Defined in:
- lib/jqgrid_for_rails/controllers/helpers.rb
Overview
Those helpers are convenience methods added to ApplicationController.
Instance Method Summary collapse
-
#col_model_for_jqgrid(columns, options = {}) ⇒ Object
DEPRECATED! Moved to jqgrids_helper.rb.
-
#json_for_jqgrid(records, columns = nil, options = {}) ⇒ Object
Returns a json string ready to be sent to a jqgrid component.
-
#order_by_from_params(params, quote_char = nil) ⇒ Object
Returns the ‘order by’ string created using the params received from the jqgrid.
Instance Method Details
#col_model_for_jqgrid(columns, options = {}) ⇒ Object
DEPRECATED! Moved to jqgrids_helper.rb
7 8 9 10 11 12 |
# File 'lib/jqgrid_for_rails/controllers/helpers.rb', line 7 def col_model_for_jqgrid columns, = {} columns.map do |c| h = {:name => c, :index => c} h.merge (c, ) end end |
#json_for_jqgrid(records, columns = nil, options = {}) ⇒ Object
Returns a json string ready to be sent to a jqgrid component.
-
records
- Should be the result of an active record query through thepaginate
method offered by will_paginate. -
columns
- Array with the name of the fields in the order they should be returned.
Options
-
:id_column
- Says which is the column that should be used as the row id. This should a be the column name, but it can also be a Proc, in which case the record will be passed to the proc. See examples for more info. -
:id_prefix
- If specified, thecolumn_id
option will be concatenated to this prefix. This helps to keep the html id unique in the document. -
:translate
- Array with the name of the fields that should be localized with theI18n.l
method. The field content must be of any class accepted by the method. I.e. Time, DateTime, Date. -
:format
- Says the proc and the columns where to apply it. It can be used to change the format of the field as well as to run any other code on it. If the key of the hash is not a column name but an array of column names, the proc is applied to all of them. -
:page
- Says the page number (Deprecated. The page number is now inferred fromrecords
.
Examples
records = Invoice.paginate(:page => 1)
my_proc = Proc.new {|r| "invid_#{r.invid}" }
json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:id_column => my_proc })
# => {"rows":[{"cell":["2011-01-01T00:00:00Z",10.0,11.0],"id":"invid_1"}],"total":1,"page":1,"records":1}
records = Invoice.paginate(:page => 1)
my_format = Proc.new {|val| val + 100 }
json_for_jqgrid(records, ['invdate', 'amount', 'total' ], {:format => { 'total' => my_format })
# Because of the :format option, 100 is added to 'total' field.
# => {"rows":[{"cell":["2011-01-01T00:00:00Z",10.0,111.0],"id":"invid_1"}],"total":2,"page":1,"records":1}
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jqgrid_for_rails/controllers/helpers.rb', line 59 def json_for_jqgrid records, columns = nil, = {} columns ||= (records.first.attributes.keys rescue []) [:id_column] ||= columns.first [:page] ||= records.current_page { :page => [:page], :total => records.total_pages, :records => records.total_entries, :rows => records.map {|r| row_from_record(r, columns, )} }.to_json end |
#order_by_from_params(params, quote_char = nil) ⇒ Object
Returns the ‘order by’ string created using the params received from the jqgrid. If quote_char
is specified, the column name used in the ‘order by’ clause will be quoted.
Example
order_by_from_params({'sidx' => 'updated_at', 'sord' => 'asc'})
#=> 'updated_at asc'
order_by_from_params({'sidx' => 'updated_at', 'sord' => 'desc'}, '`')
#=> '`updated_at` desc'
85 86 87 88 89 |
# File 'lib/jqgrid_for_rails/controllers/helpers.rb', line 85 def order_by_from_params params, quote_char = nil order_by = quote(params['sidx'], quote_char) unless params['sidx'].blank? order_by << " #{params['sord']}" if params['sord'] && order_by order_by end |