compass-jquery-plugin 0.3.1.beta.2 → 0.3.1.beta.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,438 +1,438 @@
1
- module ActionView
2
- module Helpers
3
-
4
- def jqgrid_javascripts(locale)
5
- js = capture { javascript_include_tag "i18n/jqgrid/locale-#{locale}.min" }
6
- js << capture {javascript_tag "jQuery.jgrid.no_legacy_api = true;" }
7
- js << capture { javascript_include_tag 'jquery.jqGrid.min' }
8
- end
9
-
10
- def jqgrid(title, id, action, columns = {},
11
- options = {}, edit_options = "", add_options = "", del_options = "", search_options = "",
12
- custom1_button = nil, custom2_button = nil, custom3_button = nil, custom4_button = nil, custom5_button = nil )
13
-
14
- # Default options
15
- options =
16
- {
17
- :rows_per_page => '10',
18
- :sort_column => '',
19
- :sort_order => '',
20
- :height => '150',
21
- :gridview => 'false',
22
- :error_handler => 'null',
23
- :inline_edit_handler => 'null',
24
- :add => 'false',
25
- :delete => 'false',
26
- :search => 'true',
27
- :edit => 'false',
28
- :inline_edit => 'false',
29
- :autowidth => 'false',
30
- :hidegrid => 'false',
31
- :rownumbers => 'false'
32
- }.merge(options)
33
-
34
- # Stringify options values
35
- options.inject({}) do |options, (key, value)|
36
- options[key] = (key != :subgrid) ? value.to_s : value
37
- options
38
- end
39
-
40
- options[:error_handler_return_value] = (options[:error_handler] == 'null') ? 'true;' : options[:error_handler]
41
- edit_button = (options[:edit] == 'true' && options[:inline_edit] == 'false').to_s
42
-
43
- # Generate columns data
44
- col_names, col_model = gen_columns(columns)
45
-
46
- # Enable multi-selection (checkboxes)
47
- multiselect = "multiselect: false,"
48
- if options[:multi_selection]
49
- multiselect = "multiselect: true,"
50
- multihandler = %Q/
51
- jQuery("##{id}_select_button").click( function() {
52
- var s; s = jQuery("##{id}").jqGrid('getGridParam', 'selarrrow');
53
- #{options[:selection_handler]}(s);
54
- return false;
55
- });/
56
- end
57
-
58
- # Enable master-details
59
- masterdetails = ""
60
- if options[:master_details]
61
- masterdetails = %Q/
62
- onSelectRow: function(ids) {
63
- if(ids == null) {
64
- ids=0;
65
- if(jQuery("##{id}_details").jqGrid('getGridParam', 'records') >0 )
66
- {
67
- jQuery("##{id}_details").jqGrid('setGridParam', {url:"#{options[:details_url]}?q=1&id="+ids,page:1})
68
- jQuery("##{id}_details").jqGrid('setCaption', "#{options[:details_caption]}: "+ids)
69
- jQuery("##{id}_details").trigger('reloadGrid');
70
- }
71
- }
72
- else
73
- {
74
- jQuery("##{id}_details").jqGrid('setGridParam', {url:"#{options[:details_url]}?q=1&id="+ids,page:1})
75
- jQuery("##{id}_details").jqGrid('setCaption', "#{options[:details_caption]} : "+ids)
76
- jQuery("##{id}_details").trigger('reloadGrid');
77
- }
78
- },/
79
- end
80
-
81
- # Enable selection link, button
82
- # The javascript function created by the user (options[:selection_handler]) will be called with the selected row id as a parameter
83
- selection_link = ""
84
- if options[:direct_selection].blank? && options[:selection_handler].present? && options[:multi_selection].blank?
85
- selection_link = %Q/
86
- jQuery("##{id}_select_button").click( function(){
87
- var id = jQuery("##{id}").jqGrid('getGridParam', 'selrow');
88
- if (id) {
89
- #{options[:selection_handler]}(id);
90
- } else {
91
- alert("Please select a row");
92
- }
93
- return false;
94
- });/
95
- end
96
-
97
- # Enable direct selection (when a row in the table is clicked)
98
- # The javascript function created by the user (options[:selection_handler]) will be called with the selected row id as a parameter
99
- direct_link = ""
100
- if options[:direct_selection] && options[:selection_handler].present? && options[:multi_selection].blank?
101
- direct_link = %Q/
102
- onSelectRow: function(id){
103
- if(id){
104
- #{options[:selection_handler]}(id);
105
- }
106
- },/
107
- end
108
-
109
- # Enable grid_loaded callback
110
- # When data are loaded into the grid, call the Javascript function options[:grid_loaded] (defined by the user)
111
- grid_loaded = ""
112
- if options[:grid_loaded].present?
113
- grid_loaded = %Q/
114
- loadComplete: function(){
115
- #{options[:grid_loaded]}();
116
- },
117
- /
118
- end
119
-
120
- # Enable inline editing
121
- # When a row is selected, all fields are transformed to input types
122
- editable = ""
123
- if options[:edit] && options[:inline_edit] == 'true'
124
- editable = %Q/
125
- onSelectRow: function(id){
126
- if(id && id!==lastsel){
127
- jQuery('##{id}').jqGrid('restoreRow', lastsel);
128
- jQuery('##{id}').jqGrid('editRow', id, true, #{options[:inline_edit_handler]}, #{options[:error_handler]});
129
- lastsel=id;
130
- }
131
- },/
132
- end
133
-
134
- # Enable subgrids
135
- subgrid = ""
136
- subgrid_enabled = "subGrid:false,"
137
-
138
- if options[:subgrid].present?
139
-
140
- subgrid_enabled = "subGrid:true,"
141
-
142
- options[:subgrid] =
143
- {
144
- :rows_per_page => '10',
145
- :sort_column => 'id',
146
- :sort_order => 'asc',
147
- :add => 'false',
148
- :edit => 'false',
149
- :delete => 'false',
150
- :search => 'false'
151
- }.merge(options[:subgrid])
152
-
153
- # Stringify options values
154
- options[:subgrid].inject({}) do |suboptions, (key, value)|
155
- suboptions[key] = value.to_s
156
- suboptions
157
- end
158
-
159
- subgrid_inline_edit = ""
160
- if options[:subgrid][:inline_edit] == true
161
- options[:subgrid][:edit] = 'false'
162
- subgrid_inline_edit = %Q/
163
- onSelectRow: function(id){
164
- if(id && id!==lastsel){
165
- jQuery('#'+subgrid_table_id).jqGrid('restoreRow', lastsel);
166
- jQuery('#'+subgrid_table_id).jqGrid('editRow', id,true);
167
- lastsel=id;
168
- }
169
- },
170
- /
171
- end
172
-
173
- if options[:subgrid][:direct_selection] && options[:subgrid][:selection_handler].present?
174
- subgrid_direct_link = %Q/
175
- onSelectRow: function(id){
176
- if(id){
177
- #{options[:subgrid][:selection_handler]}(id);
178
- }
179
- },
180
- /
181
- end
182
-
183
- sub_col_names, sub_col_model = gen_columns(options[:subgrid][:columns])
184
-
185
- subgrid = %Q(
186
- subGridRowExpanded: function(subgrid_id, row_id) {
187
- var subgrid_table_id, pager_id;
188
- subgrid_table_id = subgrid_id+"_t";
189
- pager_id = "p_"+subgrid_table_id;
190
- $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
191
- jQuery("#"+subgrid_table_id).jqGrid({
192
- url:"#{options[:subgrid][:url]}?q=2&id="+row_id,
193
- editurl:'#{options[:subgrid][:edit_url]}?parent_id='+row_id,
194
- datatype: "json",
195
- colNames: #{sub_col_names},
196
- colModel: #{sub_col_model},
197
- rowNum:#{options[:subgrid][:rows_per_page]},
198
- pager: pager_id,
199
- sortname: '#{options[:subgrid][:sort_column]}',
200
- sortorder: '#{options[:subgrid][:sort_order]}',
201
- viewrecords: true,
202
- //toolbar : [true,"top"],
203
- #{subgrid_inline_edit}
204
- #{subgrid_direct_link}
205
- height: '100%'
206
- });
207
- jQuery("#"+subgrid_table_id).jqGrid('navGrid', "#"+pager_id,{edit:#{options[:subgrid][:edit]},add:#{options[:subgrid][:add]},del:#{options[:subgrid][:delete]},search:false})
208
- jQuery("#"+subgrid_table_id).jqGrid('navButtonAdd', "#"+pager_id,{caption:"Search",title:"Toggle Search",buttonimg:'/images/jqgrid/search.png',
209
- onClickButton:function(){
210
- if(jQuery("#t_"+subgrid_table_id).css("display")=="none") {
211
- jQuery("#t_"+subgrid_table_id).css("display","");
212
- } else {
213
- jQuery("#t_"+subgrid_table_id).css("display","none");
214
- }
215
- }
216
- });
217
- jQuery("#t_"+subgrid_table_id).height(25).hide().jqGrid('filterGrid', ""+subgrid_table_id,{gridModel:true,gridToolbar:true});
218
- },
219
- subGridRowColapsed: function(subgrid_id, row_id) {
220
- },
221
- )
222
- end
223
-
224
- # Generate required Javascript & html to create the jqgrid
225
- %Q(
226
- <script type="text/javascript">
227
- var lastsel;
228
- jQuery(document).ready(function(){
229
- jQuery("##{id}").jqGrid({
230
- url:'#{action}?q=1',
231
- height: "#{options[:height]}",
232
- // page: 1,
233
- rowNum:#{options[:rows_per_page]},
234
- // records: 0,
235
- pager: '##{id}_pager',
236
- // pgbuttons: true,
237
- // pginput: true,
238
- colModel:#{col_model},
239
- rowList:[10,25,50,100],
240
- colNames:#{col_names},
241
- sortorder: '#{options[:sort_order]}',
242
- sortname: '#{options[:sort_column]}',
243
- datatype: "json",
244
- // mtype: "GET",
245
- // altRows: false,
246
- // selarrrow: [],
247
- // savedRow: [],
248
- // shrinkToFit: true,
249
- // xmlReader: {},
250
- // jsonReader: {},
251
- // subGrid: false,
252
- // subGridModel :[],
253
- // reccount: 0,
254
- // lastpage: 0,
255
- // lastsort: 0,
256
- // selrow: null,
257
- // beforeSelectRow: null,
258
- // onSelectRow: null,
259
- // onSortCol: null,
260
- // ondblClickRow: null,
261
- // onRightClickRow: null,
262
- // onPaging: null,
263
- // onSelectAll: null,
264
- // loadComplete: null,
265
- // gridComplete: null,
266
- // loadError: null,
267
- // loadBeforeSend: null,
268
- // afterInsertRow: null,
269
- // beforeRequest: null,
270
- // onHeaderClick: null,
271
- viewrecords: true,
272
- // loadonce: false,
273
- // multikey: false,
274
- editurl:'#{options[:edit_url]}',
275
- // search: false,
276
- caption: "#{title}",
277
- hidegrid: #{options[:hidegrid]},
278
- // hiddengrid: false,
279
- // postData: {},
280
- // userData: {},
281
- // treeGrid : false,
282
- // treeGridModel : 'nested',
283
- // treeReader : {},
284
- // treeANode : -1,
285
- // ExpandColumn: null,
286
- // tree_root_level : 0,
287
- // prmNames: {page:"page",rows:"rows", sort: "sidx",order: "sord", search:"_search", nd:"nd", id:"id",oper:"oper",editoper:"edit",addoper:"add",deloper:"del"},
288
- // forceFit : false,
289
- // gridstate : "visible",
290
- // cellEdit: false,
291
- // cellsubmit: "remote",
292
- // nv:0,
293
- // loadui: "enable",
294
- // toolbar: [false,""],
295
- // scroll: false,
296
- // multiboxonly : false,
297
- // deselectAfterSort : true,
298
- scrollrows: true,
299
- autowidth: #{options[:autowidth]},
300
- // scrollOffset :18,
301
- // cellLayout: 5,
302
- // subGridWidth: 20,
303
- // multiselectWidth: 20,
304
- gridview: #{options[:gridview]},
305
- // rownumWidth: 25,
306
- rownumbers: #{options[:rownumbers]},
307
- // pagerpos: 'center',
308
- // recordpos: 'right',
309
- // footerrow : false,
310
- // userDataOnFooter : false,
311
- // hoverrows : true,
312
- // altclass : 'ui-priority-secondary',
313
- // viewsortcols : [false,'vertical',true],
314
- // resizeclass : '',
315
- // autoencode : false,
316
- // remapColumns : [],
317
- // ajaxGridOptions :{},
318
- // direction : "ltr",
319
- #{multiselect}
320
- #{masterdetails}
321
- #{grid_loaded}
322
- #{direct_link}
323
- #{editable}
324
- #{subgrid_enabled}
325
- #{subgrid}
326
- // toppager: false,
327
- // headertitles: false
328
- });
329
- jQuery("##{id}").jqGrid('navGrid', '##{id}_pager',
330
- {edit:#{edit_button},add:#{options[:add]},del:#{options[:delete]},search:#{options[:search]},refresh:true},
331
- {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'edit');}},
332
- {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'add');}},
333
- {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'delete');}},
334
- {#{search_options}}
335
- );
336
- #{multihandler}
337
- #{selection_link}
338
- });
339
- </script>
340
- )
341
- end
342
-
343
- private
344
-
345
- def gen_columns(columns)
346
- # Generate columns data
347
- col_names = "[" # Labels
348
- col_model = "[" # Options
349
- columns.each do |c|
350
- col_names << "'#{c[:label]}',"
351
- col_model << "{name:'#{c[:field]}', index:'#{c[:field]}'#{get_attributes(c)}},"
352
- end
353
- col_names.chop! << "]"
354
- col_model.chop! << "]"
355
- [col_names, col_model]
356
- end
357
-
358
- # Generate a list of attributes for related column (align:'right', sortable:true, resizable:false, ...)
359
- def get_attributes(column)
360
- options = ","
361
- column.except(:field, :label).each do |couple|
362
- if couple[0] == :editoptions
363
- options << "editoptions:#{get_sub_options(couple[1])},"
364
- elsif couple[0] == :formoptions
365
- options << "formoptions:#{get_sub_options(couple[1])},"
366
- elsif couple[0] == :searchoptions
367
- options << "searchoptions:#{get_sub_options(couple[1])},"
368
- elsif couple[0] == :editrules
369
- options << "editrules:#{get_sub_options(couple[1])},"
370
- else
371
- if couple[1].class == String
372
- options << "#{couple[0]}:'#{couple[1]}',"
373
- else
374
- options << "#{couple[0]}:#{couple[1]},"
375
- end
376
- end
377
- end
378
- options.chop!
379
- end
380
-
381
- # Generate options for editable fields (value, data, width, maxvalue, cols, rows, ...)
382
- def get_sub_options(editoptions)
383
- options = "{"
384
- editoptions.each do |couple|
385
- if couple[0] == :value # :value => [[1, "Rails"], [2, "Ruby"], [3, "jQuery"]]
386
- options << %Q/value:"/
387
- couple[1].each do |v|
388
- options << "#{v[0]}:#{v[1]};"
389
- end
390
- options.chop! << %Q/",/
391
- elsif couple[0] == :data # :data => [Category.all, :id, :title])
392
- options << %Q/value:"/
393
- couple[1].first.each do |obj|
394
- options << "%s:%s;" % [obj.send(couple[1].second), obj.send(couple[1].third)]
395
- end
396
- options.chop! << %Q/",/
397
- else # :size => 30, :rows => 5, :maxlength => 20, ...
398
- if couple[1].instance_of?(Fixnum) || couple[1] == 'true' || couple[1] == 'false' || couple[1] == true || couple[1] == false
399
- options << %Q/#{couple[0]}:#{couple[1]},/
400
- else
401
- options << %Q/#{couple[0]}:"#{couple[1]}",/
402
- end
403
- end
404
- end
405
- options.chop! << "}"
406
- end
407
- end
408
- end
409
-
410
- module JqgridJson
411
- include ActionView::Helpers::JavaScriptHelper
412
- include HandleAttributes
413
-
414
- def to_jqgrid_json(attributes, current_page, per_page, total)
415
- json = %Q({"page":"#{current_page}","total":#{total/per_page.to_i+1},"records":"#{total}")
416
- if total > 0
417
- json << %Q(,"rows":[)
418
- each do |elem|
419
- elem.id ||= index(elem)
420
- json << %Q({"id":"#{elem.id}","cell":[)
421
- couples = elem.attributes.symbolize_keys
422
- attributes.each do |atr|
423
- value = get_atr_value(elem, atr, couples)
424
- value = escape_javascript(value) if value and value.is_a? String
425
- json << %Q("#{value}",)
426
- end
427
- json.chop! << "]},"
428
- end
429
- json.chop! << "]}"
430
- else
431
- json << "}"
432
- end
433
- end
434
- end
435
-
436
- class Array
437
- include JqgridJson
1
+ module ActionView
2
+ module Helpers
3
+
4
+ def jqgrid_javascripts(locale)
5
+ js = capture { javascript_include_tag "i18n/jqgrid/locale-#{locale}.min" }
6
+ js << capture {javascript_tag "jQuery.jgrid.no_legacy_api = true;" }
7
+ js << capture { javascript_include_tag 'jquery.jqGrid.min' }
8
+ end
9
+
10
+ def jqgrid(title, id, action, columns = {},
11
+ options = {}, edit_options = "", add_options = "", del_options = "", search_options = "",
12
+ custom1_button = nil, custom2_button = nil, custom3_button = nil, custom4_button = nil, custom5_button = nil )
13
+
14
+ # Default options
15
+ options =
16
+ {
17
+ :rows_per_page => '10',
18
+ :sort_column => '',
19
+ :sort_order => '',
20
+ :height => '150',
21
+ :gridview => 'false',
22
+ :error_handler => 'null',
23
+ :inline_edit_handler => 'null',
24
+ :add => 'false',
25
+ :delete => 'false',
26
+ :search => 'true',
27
+ :edit => 'false',
28
+ :inline_edit => 'false',
29
+ :autowidth => 'false',
30
+ :hidegrid => 'false',
31
+ :rownumbers => 'false'
32
+ }.merge(options)
33
+
34
+ # Stringify options values
35
+ options.inject({}) do |options, (key, value)|
36
+ options[key] = (key != :subgrid) ? value.to_s : value
37
+ options
38
+ end
39
+
40
+ options[:error_handler_return_value] = (options[:error_handler] == 'null') ? 'true;' : options[:error_handler]
41
+ edit_button = (options[:edit] == 'true' && options[:inline_edit] == 'false').to_s
42
+
43
+ # Generate columns data
44
+ col_names, col_model = gen_columns(columns)
45
+
46
+ # Enable multi-selection (checkboxes)
47
+ multiselect = "multiselect: false,"
48
+ if options[:multi_selection]
49
+ multiselect = "multiselect: true,"
50
+ multihandler = %Q/
51
+ jQuery("##{id}_select_button").click( function() {
52
+ var s; s = jQuery("##{id}").jqGrid('getGridParam', 'selarrrow');
53
+ #{options[:selection_handler]}(s);
54
+ return false;
55
+ });/
56
+ end
57
+
58
+ # Enable master-details
59
+ masterdetails = ""
60
+ if options[:master_details]
61
+ masterdetails = %Q/
62
+ onSelectRow: function(ids) {
63
+ if(ids == null) {
64
+ ids=0;
65
+ if(jQuery("##{id}_details").jqGrid('getGridParam', 'records') >0 )
66
+ {
67
+ jQuery("##{id}_details").jqGrid('setGridParam', {url:"#{options[:details_url]}?q=1&id="+ids,page:1})
68
+ jQuery("##{id}_details").jqGrid('setCaption', "#{options[:details_caption]}: "+ids)
69
+ jQuery("##{id}_details").trigger('reloadGrid');
70
+ }
71
+ }
72
+ else
73
+ {
74
+ jQuery("##{id}_details").jqGrid('setGridParam', {url:"#{options[:details_url]}?q=1&id="+ids,page:1})
75
+ jQuery("##{id}_details").jqGrid('setCaption', "#{options[:details_caption]} : "+ids)
76
+ jQuery("##{id}_details").trigger('reloadGrid');
77
+ }
78
+ },/
79
+ end
80
+
81
+ # Enable selection link, button
82
+ # The javascript function created by the user (options[:selection_handler]) will be called with the selected row id as a parameter
83
+ selection_link = ""
84
+ if options[:direct_selection].blank? && options[:selection_handler].present? && options[:multi_selection].blank?
85
+ selection_link = %Q/
86
+ jQuery("##{id}_select_button").click( function(){
87
+ var id = jQuery("##{id}").jqGrid('getGridParam', 'selrow');
88
+ if (id) {
89
+ #{options[:selection_handler]}(id);
90
+ } else {
91
+ alert("Please select a row");
92
+ }
93
+ return false;
94
+ });/
95
+ end
96
+
97
+ # Enable direct selection (when a row in the table is clicked)
98
+ # The javascript function created by the user (options[:selection_handler]) will be called with the selected row id as a parameter
99
+ direct_link = ""
100
+ if options[:direct_selection] && options[:selection_handler].present? && options[:multi_selection].blank?
101
+ direct_link = %Q/
102
+ onSelectRow: function(id){
103
+ if(id){
104
+ #{options[:selection_handler]}(id);
105
+ }
106
+ },/
107
+ end
108
+
109
+ # Enable grid_loaded callback
110
+ # When data are loaded into the grid, call the Javascript function options[:grid_loaded] (defined by the user)
111
+ grid_loaded = ""
112
+ if options[:grid_loaded].present?
113
+ grid_loaded = %Q/
114
+ loadComplete: function(){
115
+ #{options[:grid_loaded]}();
116
+ },
117
+ /
118
+ end
119
+
120
+ # Enable inline editing
121
+ # When a row is selected, all fields are transformed to input types
122
+ editable = ""
123
+ if options[:edit] && options[:inline_edit] == 'true'
124
+ editable = %Q/
125
+ onSelectRow: function(id){
126
+ if(id && id!==lastsel){
127
+ jQuery('##{id}').jqGrid('restoreRow', lastsel);
128
+ jQuery('##{id}').jqGrid('editRow', id, true, #{options[:inline_edit_handler]}, #{options[:error_handler]});
129
+ lastsel=id;
130
+ }
131
+ },/
132
+ end
133
+
134
+ # Enable subgrids
135
+ subgrid = ""
136
+ subgrid_enabled = "subGrid:false,"
137
+
138
+ if options[:subgrid].present?
139
+
140
+ subgrid_enabled = "subGrid:true,"
141
+
142
+ options[:subgrid] =
143
+ {
144
+ :rows_per_page => '10',
145
+ :sort_column => 'id',
146
+ :sort_order => 'asc',
147
+ :add => 'false',
148
+ :edit => 'false',
149
+ :delete => 'false',
150
+ :search => 'false'
151
+ }.merge(options[:subgrid])
152
+
153
+ # Stringify options values
154
+ options[:subgrid].inject({}) do |suboptions, (key, value)|
155
+ suboptions[key] = value.to_s
156
+ suboptions
157
+ end
158
+
159
+ subgrid_inline_edit = ""
160
+ if options[:subgrid][:inline_edit] == true
161
+ options[:subgrid][:edit] = 'false'
162
+ subgrid_inline_edit = %Q/
163
+ onSelectRow: function(id){
164
+ if(id && id!==lastsel){
165
+ jQuery('#'+subgrid_table_id).jqGrid('restoreRow', lastsel);
166
+ jQuery('#'+subgrid_table_id).jqGrid('editRow', id,true);
167
+ lastsel=id;
168
+ }
169
+ },
170
+ /
171
+ end
172
+
173
+ if options[:subgrid][:direct_selection] && options[:subgrid][:selection_handler].present?
174
+ subgrid_direct_link = %Q/
175
+ onSelectRow: function(id){
176
+ if(id){
177
+ #{options[:subgrid][:selection_handler]}(id);
178
+ }
179
+ },
180
+ /
181
+ end
182
+
183
+ sub_col_names, sub_col_model = gen_columns(options[:subgrid][:columns])
184
+
185
+ subgrid = %Q(
186
+ subGridRowExpanded: function(subgrid_id, row_id) {
187
+ var subgrid_table_id, pager_id;
188
+ subgrid_table_id = subgrid_id+"_t";
189
+ pager_id = "p_"+subgrid_table_id;
190
+ $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
191
+ jQuery("#"+subgrid_table_id).jqGrid({
192
+ url:"#{options[:subgrid][:url]}?q=2&id="+row_id,
193
+ editurl:'#{options[:subgrid][:edit_url]}?parent_id='+row_id,
194
+ datatype: "json",
195
+ colNames: #{sub_col_names},
196
+ colModel: #{sub_col_model},
197
+ rowNum:#{options[:subgrid][:rows_per_page]},
198
+ pager: pager_id,
199
+ sortname: '#{options[:subgrid][:sort_column]}',
200
+ sortorder: '#{options[:subgrid][:sort_order]}',
201
+ viewrecords: true,
202
+ //toolbar : [true,"top"],
203
+ #{subgrid_inline_edit}
204
+ #{subgrid_direct_link}
205
+ height: '100%'
206
+ });
207
+ jQuery("#"+subgrid_table_id).jqGrid('navGrid', "#"+pager_id,{edit:#{options[:subgrid][:edit]},add:#{options[:subgrid][:add]},del:#{options[:subgrid][:delete]},search:false})
208
+ jQuery("#"+subgrid_table_id).jqGrid('navButtonAdd', "#"+pager_id,{caption:"Search",title:"Toggle Search",buttonimg:'/images/jqgrid/search.png',
209
+ onClickButton:function(){
210
+ if(jQuery("#t_"+subgrid_table_id).css("display")=="none") {
211
+ jQuery("#t_"+subgrid_table_id).css("display","");
212
+ } else {
213
+ jQuery("#t_"+subgrid_table_id).css("display","none");
214
+ }
215
+ }
216
+ });
217
+ jQuery("#t_"+subgrid_table_id).height(25).hide().jqGrid('filterGrid', ""+subgrid_table_id,{gridModel:true,gridToolbar:true});
218
+ },
219
+ subGridRowColapsed: function(subgrid_id, row_id) {
220
+ },
221
+ )
222
+ end
223
+
224
+ # Generate required Javascript & html to create the jqgrid
225
+ %Q(
226
+ <script type="text/javascript">
227
+ var lastsel;
228
+ jQuery(document).ready(function(){
229
+ jQuery("##{id}").jqGrid({
230
+ url:'#{action}?q=1',
231
+ height: "#{options[:height]}",
232
+ // page: 1,
233
+ rowNum:#{options[:rows_per_page]},
234
+ // records: 0,
235
+ pager: '##{id}_pager',
236
+ // pgbuttons: true,
237
+ // pginput: true,
238
+ colModel:#{col_model},
239
+ rowList:[10,25,50,100],
240
+ colNames:#{col_names},
241
+ sortorder: '#{options[:sort_order]}',
242
+ sortname: '#{options[:sort_column]}',
243
+ datatype: "json",
244
+ // mtype: "GET",
245
+ // altRows: false,
246
+ // selarrrow: [],
247
+ // savedRow: [],
248
+ // shrinkToFit: true,
249
+ // xmlReader: {},
250
+ // jsonReader: {},
251
+ // subGrid: false,
252
+ // subGridModel :[],
253
+ // reccount: 0,
254
+ // lastpage: 0,
255
+ // lastsort: 0,
256
+ // selrow: null,
257
+ // beforeSelectRow: null,
258
+ // onSelectRow: null,
259
+ // onSortCol: null,
260
+ // ondblClickRow: null,
261
+ // onRightClickRow: null,
262
+ // onPaging: null,
263
+ // onSelectAll: null,
264
+ // loadComplete: null,
265
+ // gridComplete: null,
266
+ // loadError: null,
267
+ // loadBeforeSend: null,
268
+ // afterInsertRow: null,
269
+ // beforeRequest: null,
270
+ // onHeaderClick: null,
271
+ viewrecords: true,
272
+ // loadonce: false,
273
+ // multikey: false,
274
+ editurl:'#{options[:edit_url]}',
275
+ // search: false,
276
+ caption: "#{title}",
277
+ hidegrid: #{options[:hidegrid]},
278
+ // hiddengrid: false,
279
+ // postData: {},
280
+ // userData: {},
281
+ // treeGrid : false,
282
+ // treeGridModel : 'nested',
283
+ // treeReader : {},
284
+ // treeANode : -1,
285
+ // ExpandColumn: null,
286
+ // tree_root_level : 0,
287
+ // prmNames: {page:"page",rows:"rows", sort: "sidx",order: "sord", search:"_search", nd:"nd", id:"id",oper:"oper",editoper:"edit",addoper:"add",deloper:"del"},
288
+ // forceFit : false,
289
+ // gridstate : "visible",
290
+ // cellEdit: false,
291
+ // cellsubmit: "remote",
292
+ // nv:0,
293
+ // loadui: "enable",
294
+ // toolbar: [false,""],
295
+ // scroll: false,
296
+ // multiboxonly : false,
297
+ // deselectAfterSort : true,
298
+ scrollrows: true,
299
+ autowidth: #{options[:autowidth]},
300
+ // scrollOffset :18,
301
+ // cellLayout: 5,
302
+ // subGridWidth: 20,
303
+ // multiselectWidth: 20,
304
+ gridview: #{options[:gridview]},
305
+ // rownumWidth: 25,
306
+ rownumbers: #{options[:rownumbers]},
307
+ // pagerpos: 'center',
308
+ // recordpos: 'right',
309
+ // footerrow : false,
310
+ // userDataOnFooter : false,
311
+ // hoverrows : true,
312
+ // altclass : 'ui-priority-secondary',
313
+ // viewsortcols : [false,'vertical',true],
314
+ // resizeclass : '',
315
+ // autoencode : false,
316
+ // remapColumns : [],
317
+ // ajaxGridOptions :{},
318
+ // direction : "ltr",
319
+ #{multiselect}
320
+ #{masterdetails}
321
+ #{grid_loaded}
322
+ #{direct_link}
323
+ #{editable}
324
+ #{subgrid_enabled}
325
+ #{subgrid}
326
+ // toppager: false,
327
+ // headertitles: false
328
+ });
329
+ jQuery("##{id}").jqGrid('navGrid', '##{id}_pager',
330
+ {edit:#{edit_button},add:#{options[:add]},del:#{options[:delete]},search:#{options[:search]},refresh:true},
331
+ {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'edit');}},
332
+ {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'add');}},
333
+ {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'delete');}},
334
+ {#{search_options}}
335
+ );
336
+ #{multihandler}
337
+ #{selection_link}
338
+ });
339
+ </script>
340
+ )
341
+ end
342
+
343
+ private
344
+
345
+ def gen_columns(columns)
346
+ # Generate columns data
347
+ col_names = "[" # Labels
348
+ col_model = "[" # Options
349
+ columns.each do |c|
350
+ col_names << "'#{c[:label]}',"
351
+ col_model << "{name:'#{c[:field]}', index:'#{c[:field]}'#{get_attributes(c)}},"
352
+ end
353
+ col_names.chop! << "]"
354
+ col_model.chop! << "]"
355
+ [col_names, col_model]
356
+ end
357
+
358
+ # Generate a list of attributes for related column (align:'right', sortable:true, resizable:false, ...)
359
+ def get_attributes(column)
360
+ options = ","
361
+ column.except(:field, :label).each do |couple|
362
+ if couple[0] == :editoptions
363
+ options << "editoptions:#{get_sub_options(couple[1])},"
364
+ elsif couple[0] == :formoptions
365
+ options << "formoptions:#{get_sub_options(couple[1])},"
366
+ elsif couple[0] == :searchoptions
367
+ options << "searchoptions:#{get_sub_options(couple[1])},"
368
+ elsif couple[0] == :editrules
369
+ options << "editrules:#{get_sub_options(couple[1])},"
370
+ else
371
+ if couple[1].class == String
372
+ options << "#{couple[0]}:'#{couple[1]}',"
373
+ else
374
+ options << "#{couple[0]}:#{couple[1]},"
375
+ end
376
+ end
377
+ end
378
+ options.chop!
379
+ end
380
+
381
+ # Generate options for editable fields (value, data, width, maxvalue, cols, rows, ...)
382
+ def get_sub_options(editoptions)
383
+ options = "{"
384
+ editoptions.each do |couple|
385
+ if couple[0] == :value # :value => [[1, "Rails"], [2, "Ruby"], [3, "jQuery"]]
386
+ options << %Q/value:"/
387
+ couple[1].each do |v|
388
+ options << "#{v[0]}:#{v[1]};"
389
+ end
390
+ options.chop! << %Q/",/
391
+ elsif couple[0] == :data # :data => [Category.all, :id, :title])
392
+ options << %Q/value:"/
393
+ couple[1].first.each do |obj|
394
+ options << "%s:%s;" % [obj.send(couple[1].second), obj.send(couple[1].third)]
395
+ end
396
+ options.chop! << %Q/",/
397
+ else # :size => 30, :rows => 5, :maxlength => 20, ...
398
+ if couple[1].instance_of?(Fixnum) || couple[1] == 'true' || couple[1] == 'false' || couple[1] == true || couple[1] == false
399
+ options << %Q/#{couple[0]}:#{couple[1]},/
400
+ else
401
+ options << %Q/#{couple[0]}:"#{couple[1]}",/
402
+ end
403
+ end
404
+ end
405
+ options.chop! << "}"
406
+ end
407
+ end
408
+ end
409
+
410
+ module JqgridJson
411
+ include ActionView::Helpers::JavaScriptHelper
412
+ include HandleAttributes
413
+
414
+ def to_jqgrid_json(attributes, current_page, per_page, total)
415
+ json = %Q({"page":"#{current_page}","total":#{total/per_page.to_i+1},"records":"#{total}")
416
+ if total > 0
417
+ json << %Q(,"rows":[)
418
+ each do |elem|
419
+ elem.id ||= index(elem)
420
+ json << %Q({"id":"#{elem.id}","cell":[)
421
+ couples = elem.attributes.symbolize_keys
422
+ attributes.each do |atr|
423
+ value = get_atr_value(elem, atr, couples)
424
+ value = escape_javascript(value) if value and value.is_a? String
425
+ json << %Q("#{value}",)
426
+ end
427
+ json.chop! << "]},"
428
+ end
429
+ json.chop! << "]}"
430
+ else
431
+ json << "}"
432
+ end
433
+ end
434
+ end
435
+
436
+ class Array
437
+ include JqgridJson
438
438
  end