Module: JqgridsHelper

Defined in:
lib/app/helpers/jqgrids_helper.rb

Instance Method Summary collapse

Instance Method Details

#col_model_for_jqgrid(columns, options = {}) ⇒ Object

Returns an array to be used as col_model for the grid where each item is a hash for each column. Each hash will have at least two keys by default, the name and the index, whose values will be the name of the column. Other keys will be included if present in the options hash.

  • columns - Array with the name of the columns to include in the col_model.

Options

Every item in the options hash will be a property in the col_model for every column, unless the item is also a hash, where only will be included in the specified columns. An item can be also a Proc, that will be called passing columns as parameter.

Examples

col_model_for_jqgrid(['inv_date', 'total' ])

#=> [{:name=>"inv_date", :index=>"inv_date"}, {:name=>"total", :index=>"total"}]

col_model_for_jqgrid(['inv_date', 'total' ], {:width => 100})

#=> [{:name=>"inv_date", :index=>"inv_date", :width=>100}, {:name=>"total", :index=>"total", :width=>100}]

col_model_for_jqgrid(['inv_date', 'total'], {:width => {'inv_date' => 100}})

#=> [{:name => 'inv_date', :index => 'inv_date', :width => 100}, {:name => 'total', :index => 'total'}]

col_model_for_jqgrid(['inv_date'], {:prop => Proc.new {|c| c.camelize}})

#=> [{:name => 'inv_date', :index => 'inv_date', :prop => 'InvDate'}]


144
145
146
147
148
149
# File 'lib/app/helpers/jqgrids_helper.rb', line 144

def col_model_for_jqgrid columns, options = {}
  columns.map do |c|
    h = {:name => c, :index => c}
    h.merge property_from_options(c, options)
  end
end

#jqgrid_api(div_id, *args) ⇒ Object

Generates the jqGrid javascript code. This method returns a jqgrid api function after parsing the received parameters. If more than one function is specified in the parameters, all the resulting functions will be chained.

Parameters

  • grid_id - This is the id of the html table tag that will contain the grid.
  • *args - Each item in the args array will be translated to a jqgrid api function. Each item should be an array whose elements will be encoded to json to create the jqgrid api function. All subsequent functions after the first one will be chained. See the examples for details.

Options

This method accepts an options hash as their last parameter. The accepted options are:

  • :script_tags - If false,

    Extracts the pager id from the options hash.

    jQgrid accepts three different formats to set the pager id option.

    1. jQuery('#my_pager_div')
    2. #my_pager_div
    3. my_pager_div

    Example

    pager_id_from_options({:pager => "jQuery('#my_pager_id')"})
    #=> 'my_pager_id'
    
    
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    # File 'lib/app/helpers/jqgrids_helper.rb', line 95
    
    def pager_id_from_options options
      pager_option = options[:pager]
    
      # jQuery('#my_pager_div')
      if pager_option =~ /^jQuery\(('|")#\w+('|")\)$/
        pager_option.match(/#\w+/).to_s[1..-1]
      # #my_pager_div
      elsif pager_option =~ /^#\w+$/
        pager_option.match(/#\w+/).to_s[1..-1]
      # my_pager_div
      else
        pager_option
      end
    end