Class: JqGridRails::JqGrid
- Inherits:
-
Object
- Object
- JqGridRails::JqGrid
- Includes:
- ActionView::Helpers::JavaScriptHelper, ActionView::Helpers::UrlHelper, Helpers
- Defined in:
- lib/jqgrid_rails/jqgrid.rb
Instance Attribute Summary collapse
-
#detached_javascript ⇒ Object
readonly
Detached javascript.
-
#link_toolbar_options ⇒ Object
readonly
Options used for links toolbar.
-
#local ⇒ Object
Array of local data rows.
-
#options ⇒ Object
Options used to build tables.
-
#table_id ⇒ Object
table DOM ID.
Instance Method Summary collapse
-
#add_column(name, attr, args = {}) ⇒ Object
- name
- Name of column (Invoice) attr
- Attribute of model (invoice_num) args
-
Hash of colModel options.
-
#add_excel_export_button(name = 'Export to XLS') ⇒ Object
- name
-
text for button Exports current grid into excel document TODO: Add options to turn off paging and the like.
-
#add_javascript(js) ⇒ Object
- js
-
Javascript string Adds javascript to run on document ready.
-
#add_local_data(hsh) ⇒ Object
- hsh
-
Hash of row data for loading into table Adds new row of data to be loaded into table.
-
#add_value_mapper(map) ⇒ Object
- map
-
Hash of key value mapping Creates a client side value mapper using a randomized function name.
-
#build ⇒ Object
(also: #to_s)
Builds out the jqGrid javascript and returns the string.
-
#create_toolbar_button(url_hash) ⇒ Object
- url_hash
-
Hash of url options.
- #disable_filter_toolbar ⇒ Object
- #disable_link_toolbar ⇒ Object
-
#enable_filter_toolbar(options = {}) ⇒ Object
- options
-
Options hash for the filter toolbar Enables the filter toolbar for the grid.
-
#enable_link_toolbar(options = {}) ⇒ Object
- options
-
Options for toolbar Enables the link toolbar for the grid.
-
#enable_sortable_rows(sortable_rows) ⇒ Object
- sortable_rows
-
options hash Enables row sorting on grid TODO: Add helpers to build remote callbacks in the same format as the click events and toolbar links.
-
#filter_toolbar_autoselect_blanks ⇒ Object
Auto selects blank drop down option to combat Chrome/webkit’s unordered hashing.
-
#fix_grid_width ⇒ Object
Resizes grid after loading has completed to prevent blowing out container.
-
#has_filter_toolbar? ⇒ Boolean
Returns if the grid has a filter toolbar enabled.
-
#has_link_toolbar? ⇒ Boolean
Returns if the grid has a link toolbar enabled.
-
#has_pager? ⇒ Boolean
Returns if the grid has a pager enabled.
-
#initialize(table_id, args = {}) ⇒ JqGrid
constructor
- table_id
- DOM ID of table for grid to use args
-
Hash of jqGrid options.
-
#insert_into_callback(key, js) ⇒ Object
- key
- callback name js
-
JS string Insert JS into callback leaving existing callback code intact.
-
#link_toolbar_add(link) ⇒ Object
(also: #add_toolbar_link)
- link
-
url hash: :name, :url, :class Enables link on link toolbar.
-
#load_multi_select_fix ⇒ Object
This is a fix for the multi select within jqGrid.
-
#map_double_click ⇒ Object
Creates function callback for row double clicks.
-
#map_single_click ⇒ Object
Creates function callback from row single clicks.
-
#resizable_grid(opts = {}) ⇒ Object
- pad
-
Padding after resize Binds to resizestop event on available parent that has been marked resizable via jqquery-ui.
-
#set_local_data(ary) ⇒ Object
- ary
-
Array of Hashes (rows in table) Sets data to be loaded into table.
-
#set_search_options ⇒ Object
Syncs up filter toolbar values with advanced search values if the advanced search values have not already been provided.
Methods included from Helpers
#build_default_callback, #build_selection_callback, #build_single_callback, #build_toolbar_button, #confirm_if_required, #convert_dom_id, #csrf_token_discovery, #extract_callback_variables, #hash_to_callback, #map_click, #scrub_options_hash, #selection_array
Constructor Details
#initialize(table_id, args = {}) ⇒ JqGrid
- table_id
-
DOM ID of table for grid to use
- args
-
Hash of jqGrid options
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 25 def initialize(table_id, args={}) defaults = { :datatype => :json, :col_names => [], :col_model => [], :viewrecords => true, :json_reader => { :root => 'rows', :page => 'page', :total => 'total', :records => 'records', :id => args.delete(:row_id) || 0, :repeatitems => false, } } if(args[:url].blank? && args[:datatype].to_s != 'local') raise ArgumentError.new 'URL is required unless :datatype is set to local' end @table_id = table_id.is_a?(String) ? table_id.gsub('#', '') : table_id @options = defaults.merge(args) @pager_options = {:edit => false, :add => false, :del => false} if(t_args = @options.delete(:filter_toolbar)) (t_args.is_a?(Hash) ? t_args : nil) end if(t_args = .delete(:link_toolbar)) (t_args.is_a?(Hash) ? t_args : nil) end @local = [] @output = '' @detached_javascript = [] end |
Instance Attribute Details
#detached_javascript ⇒ Object (readonly)
Detached javascript
20 21 22 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 20 def detached_javascript @detached_javascript end |
#link_toolbar_options ⇒ Object (readonly)
Options used for links toolbar
18 19 20 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 18 def @link_toolbar_options end |
#local ⇒ Object
Array of local data rows
14 15 16 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 14 def local @local end |
#options ⇒ Object
Options used to build tables
12 13 14 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 12 def @options end |
#table_id ⇒ Object
table DOM ID
16 17 18 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 16 def table_id @table_id end |
Instance Method Details
#add_column(name, attr, args = {}) ⇒ Object
- name
-
Name of column (Invoice)
- attr
-
Attribute of model (invoice_num)
- args
-
Hash of colModel options
66 67 68 69 70 71 72 73 74 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 66 def add_column(name, attr, args={}) col = {:name => attr, :index => attr}.merge(args) map = col.delete(:map_values) col[:index] = JqGridRails.escape(col[:index]) unless @options[:no_index_escaping] col[:formatter] = add_value_mapper(map) if map @options[:col_names].push name @options[:col_model].push col self end |
#add_excel_export_button(name = 'Export to XLS') ⇒ Object
- name
-
text for button
Exports current grid into excel document TODO: Add options to turn off paging and the like
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 144 def (name='Export to XLS') name = 'Export to XLS' unless name.is_a?(String) ( :name => name, :empty_selection => true, :method => @options[:excel_request_method] || :post, :url => RawJS.new("' + jQuery(#{convert_dom_id(@table_id)}).jqGrid('getGridParam', 'url') + '"), :ajax_args => { :data => RawJS.new("(function(){ var res = {}; var grid_url = jQuery(#{convert_dom_id(@table_id)}).jqGrid('getGridParam', 'url'); var vals = jQuery(#{convert_dom_id(@table_id)}).jqGrid('getGridParam', 'postData'); jQuery( grid_url.substr(grid_url.indexOf('?'), grid_url.length) .split('&') ).each(function(idx,val){ var x = val.split('='); var key = decodeURIComponent(x[0]); var value = decodeURIComponent(x[1]); if(key.substr(key.length - 2, key.length) == '[]'){ res[key.substr(0, key.length - 2)] = []; res[key.substr(0, key.length - 2)].push(value); } else { res[key] = value; } }); jQuery(Object.keys(vals)).each(function(idx,key){ res[key] = vals[key]; }); res['rows'] = 10000000; res['page'] = 1; res['format'] = 'xls'; return res; })()") } ) self end |
#add_javascript(js) ⇒ Object
- js
-
Javascript string
Adds javascript to run on document ready
59 60 61 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 59 def add_javascript(js) @detached_javascript << js end |
#add_local_data(hsh) ⇒ Object
- hsh
-
Hash of row data for loading into table
Adds new row of data to be loaded into table
89 90 91 92 93 94 95 96 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 89 def add_local_data(hsh) if(hsh.is_a?(Hash)) @local.push hsh else raise TypeError.new "Expecting Hash value. Received: #{ary.class}" end self end |
#add_value_mapper(map) ⇒ Object
- map
-
Hash of key value mapping
Creates a client side value mapper using a randomized function name
273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 273 def add_value_mapper(map) function_name = "map_#{Digest::SHA1.hexdigest(Time.now.to_f.to_s)}" @output << "jQuery.extend(jQuery.fn.fmatter, { #{function_name} : function(cellvalue, options, rowdata){ keys = #{format_type_to_js(map.keys)} values = #{format_type_to_js(map.values)} return values[jQuery.inArray(cellvalue, keys)]; } });" function_name end |
#build ⇒ Object Also known as: to_s
Builds out the jqGrid javascript and returns the string
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 204 def build output = '' fix_grid_width unless @options[:no_filter_toolbar_autoselect_blanks] @options[:datatype] = 'local' unless @local.blank? resizable = @options.delete(:resizable_grid) ############################################################# load_multi_select_fix unless @options[:no_multi_select_fix] # TODO: Remove this when fixed in jqGrid ############################################################# map_double_click map_single_click @options = (@options) sortable_rows = @options.delete(:sortable_rows) has_pager? # convert if required if(@options[:excel_exportable]) (@options.delete(:excel_exportable)) end output << "jQuery(#{convert_dom_id(@table_id)}).jqGrid(#{format_type_to_js(@options)});\n" unless(@local.blank?) output << "if(typeof(jqgrid_local_data) == 'undefined'){ var jqgrid_local_data = new Hash(); }\n" output << "jqgrid_local_data.set(#{convert_dom_id(@table_id)}, #{format_type_to_js(@local)});\n" output << "for(var i = 0; i < jqgrid_local_data.get(#{convert_dom_id(@table_id)}).length; i++){ jQuery(#{convert_dom_id(@table_id)}).jqGrid('addRowData', i+1, jqgrid_local_data.get(#{convert_dom_id(@table_id)})[i]); }\n" end if(has_pager?) output << "jQuery(#{convert_dom_id(@table_id)}).jqGrid('navGrid', #{format_type_to_js(@options[:pager])}, #{format_type_to_js(@pager_options)});" end if() output << "jQuery(#{convert_dom_id(@table_id)}).jqGrid('filterToolbar', #{format_type_to_js(@filter_toolbar_options)});\n" end if() @link_toolbar_options[:links].each do |url_hash| output << (url_hash) end output << "jQuery(#{convert_dom_id(@table_id)}).jqGrid('navGrid', #{convert_dom_id(@table_id)} + '_linkbar', {edit:false,add:false,del:false});\n" end if(sortable_rows) output << enable_sortable_rows((sortable_rows)) end unless(resizable == false) if(resizable.respond_to?(:[])) output << resizable_grid(resizable) else output << resizable_grid end end "#{@output}\n#{output}" end |
#create_toolbar_button(url_hash) ⇒ Object
- url_hash
-
Hash of url options. Use :method to specify request method other than ‘get’
Creates a toolbar button on the grid
313 314 315 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 313 def (url_hash) (url_hash) end |
#disable_filter_toolbar ⇒ Object
122 123 124 125 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 122 def @filter_toolbar_options = nil self end |
#disable_link_toolbar ⇒ Object
127 128 129 130 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 127 def @link_toolbar_options = nil self end |
#enable_filter_toolbar(options = {}) ⇒ Object
- options
-
Options hash for the filter toolbar
Enables the filter toolbar for the grid
100 101 102 103 104 105 106 107 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 100 def (={}) = {} unless .is_a?(Hash) @filter_toolbar_options = { :string_result => true, :search_on_enter => true }.merge() self end |
#enable_link_toolbar(options = {}) ⇒ Object
- options
-
Options for toolbar
Enables the link toolbar for the grid
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 111 def (={}) = {} unless .is_a?(Hash) @link_toolbar_options = { :top => true, :bottom => false, :links => [] }.merge() @options[:toolbar] = [true, 'top'] self end |
#enable_sortable_rows(sortable_rows) ⇒ Object
- sortable_rows
-
options hash
Enables row sorting on grid TODO: Add helpers to build remote callbacks in the same format as the click events and toolbar links
321 322 323 324 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 321 def enable_sortable_rows(sortable_rows) sortable_rows = {} unless sortable_rows.is_a?(Hash) "jQuery(#{convert_dom_id(@table_id)}).sortableRows(#{format_type_to_js(sortable_rows)});\n" end |
#filter_toolbar_autoselect_blanks ⇒ Object
Auto selects blank drop down option to combat Chrome/webkit’s unordered hashing
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 192 def add_javascript( "jQuery(#{convert_dom_id(@table_id)} + '_holder')" << ".find('th[role=\"columnheader\"]')" << ".find('option[value=\"\"]').each(" << 'function(){' << "jQuery(this).attr('selected', true);" << '});' ) end |
#fix_grid_width ⇒ Object
Resizes grid after loading has completed to prevent blowing out container
184 185 186 187 188 189 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 184 def fix_grid_width insert_into_callback( :load_complete, "jQuery(#{convert_dom_id(@table_id)}).jqGrid('setGridWidth', jQuery(#{convert_dom_id(@table_id)} + '_holder').innerWidth(), true);" ) end |
#has_filter_toolbar? ⇒ Boolean
Returns if the grid has a filter toolbar enabled
256 257 258 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 256 def !@filter_toolbar_options.blank? end |
#has_link_toolbar? ⇒ Boolean
Returns if the grid has a link toolbar enabled
267 268 269 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 267 def !@link_toolbar_options.blank? && !@link_toolbar_options[:links].empty? end |
#has_pager? ⇒ Boolean
Returns if the grid has a pager enabled
261 262 263 264 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 261 def has_pager? @options[:pager] = RawJS.new("#{convert_dom_id(@table_id)} + '_pager'") if @options[:pager] == true @options.has_key?(:pager) end |
#insert_into_callback(key, js) ⇒ Object
- key
-
callback name
- js
-
JS string
Insert JS into callback leaving existing callback code intact
370 371 372 373 374 375 376 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 370 def insert_into_callback(key, js) if(@options[key]) @options[key] = RawJS.new(@options[key].to_s.sub(/^(\s*function.*?\{)/, "\\1#{js}")) else @options[key] = RawJS.new("function(){ #{js} return true; }") end end |
#link_toolbar_add(link) ⇒ Object Also known as: add_toolbar_link
- link
-
url hash: :name, :url, :class
Enables link on link toolbar
134 135 136 137 138 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 134 def (link) unless @link_toolbar_options[:links].push(link) self end |
#load_multi_select_fix ⇒ Object
This is a fix for the multi select within jqGrid. Rouge values will appear in the selection listing so this cleans things up properly
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 328 def load_multi_select_fix @options[:on_select_all] = "function(row_ids, status){ var grid = jQuery(this); grid.jqGrid('resetSelection'); if(status){ jQuery.each(grid.jqGrid('getRowData'), function(){ grid.jqGrid( 'setSelection', this['id'] ); }); } jQuery('#cb_' + #{convert_dom_id(@table_id)}.replace(/^#/, '')).attr('checked', status); }" end |
#map_double_click ⇒ Object
Creates function callback for row double clicks
286 287 288 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 286 def map_double_click map_click(:ondbl_click_row, ) if [:ondbl_click_row] end |
#map_single_click ⇒ Object
Creates function callback from row single clicks
291 292 293 294 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 291 def map_single_click map_click(:on_cell_select, ) if [:on_cell_select] map_click(:on_select_row, ) if [:on_select_row] end |
#resizable_grid(opts = {}) ⇒ Object
- pad
-
Padding after resize
Binds to resizestop event on available parent that has been marked resizable via jqquery-ui. Resizes grid after container is resized
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 347 def resizable_grid(opts = {}) "var _resizable_parent = jQuery(#{convert_dom_id(@table_id)}).parents('.ui-resizable'); _resizable_parent.bind('resizestop', function(){ var width = _resizable_parent.attr('clientWidth'); if(width == null || width < 1){ width = _resizable_parent.attr('offsetWidth'); } if(width > 0 && ((Math.abs(width) - jQuery(#{convert_dom_id(@table_id)}).width() > 5) || (Math.abs(width) - jQuery(#{convert_dom_id(@table_id)}).width() < -5))){ jQuery(#{convert_dom_id(@table_id)}).setGridWidth(width - #{(opts[:width_pad] || 40).to_i}); } var height = _resizable_parent.attr('clientHeight'); if(height == null || height < 1){ height = _resizable_parent.attr('offsetHeight'); } if(height > 0 && ((Math.abs(height) - jQuery(#{convert_dom_id(@table_id)}).height() > 5) || (Math.abs(height) - jQuery(#{convert_dom_id(@table_id)}).height() < -5))){ jQuery(#{convert_dom_id(@table_id)}).setGridHeight(height - #{(opts[:height_pad] || 40).to_i}); } }).trigger('resize');" end |
#set_local_data(ary) ⇒ Object
- ary
-
Array of Hashes (rows in table)
Sets data to be loaded into table
78 79 80 81 82 83 84 85 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 78 def set_local_data(ary) if(ary.is_a?(Array)) @local = ary else raise TypeError.new "Expecting Array value. Received: #{ary.class}" end self end |
#set_search_options ⇒ Object
Syncs up filter toolbar values with advanced search values if the advanced search values have not already been provided
298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/jqgrid_rails/jqgrid.rb', line 298 def if(@options[:col_model]) @options[:col_model].each do |column| if(column[:editoptions] && column[:editoptions][:value]) column[:searchoptions] ||= {} unless(column[:searchoptions].has_key?(:value)) column[:searchoptions][:value] = column[:editoptions][:value] end end end end end |