Class: Fancygrid::Grid
Instance Attribute Summary collapse
-
#ajax_type ⇒ Object
The request method for the ajax callback e.g.
-
#ajax_url ⇒ Object
Url for the ajax callback.
-
#components ⇒ Object
Array of fancygrid component names that are going to be rendered.
-
#hide_search ⇒ Object
If true then hides the search bar, but keeps it enabled.
-
#leafs ⇒ Object
Collection of all defined columns.
-
#options ⇒ Object
Options that have been passed to initialize the fancygrid.
-
#orm ⇒ Object
The class of the database connector implementation to be used.
-
#page_count ⇒ Object
If pagination is enabled this value holds the total number of available data pages.
-
#paginate ⇒ Object
Specifies whether pagination is enabled or not.
-
#per_page_value ⇒ Object
The default value for the per page drop down.
-
#per_page_values ⇒ Object
Array of select options for per page drop down.
-
#record_count ⇒ Object
Number of matching records.
-
#records ⇒ Object
The result of the database query.
-
#search_operator ⇒ Object
The default search operator.
-
#search_operators ⇒ Object
Array of enabled search operators.
-
#select ⇒ Object
Specifies whether sql select optimization should be performed.
-
#view_state ⇒ Object
The grids view state.
Attributes inherited from Node
#children, #name, #parent, #resource_class, #root, #table_name
Instance Method Summary collapse
-
#apply_options(options) ⇒ Object
Applies the given options and sets default values.
-
#bottom_control? ⇒ Boolean
Determines whether the bottom control bar component is enabled.
-
#collect_columns ⇒ Object
Reorganizes the defined columns and post fixes some options.
-
#complex_search? ⇒ Boolean
Determines whether the complex search component is enabled.
-
#component?(name) ⇒ Boolean
Determines whether the given component is enabled or not.
-
#dump_records ⇒ Object
Dumps the fetched records into an array of hashes that can be rendered as xml or cvs.
-
#dynamic? ⇒ Boolean
Determines whether ajax callbacks are enabled or not.
- #find(&block) ⇒ Object
-
#hidden_columns ⇒ Object
Collects and returns all columns that are marked to be hidden.
-
#initialize(name, options = {}) ⇒ Grid
constructor
Initializes the fancygrid.
-
#js_options ⇒ Object
Builds the javascript options for the javascript part of fancygrid.
-
#operator_human_name(name) ⇒ Object
Gets the human readable name for given comparison operator.
-
#search_filter(column, collection) ⇒ Object
Sets the search options for given column.
-
#select_column_options ⇒ Object
Generates options for dropdown selection to select a column.
-
#select_operator_options ⇒ Object
Generates options for dropdown selection to select a comparison operator.
-
#simple_search? ⇒ Boolean
Determines whether the simple search component is enabled.
-
#sort_window? ⇒ Boolean
Determines whether the sort window component is enabled.
- #table_class ⇒ Object
- #table_class=(value) ⇒ Object
- #table_id ⇒ Object
- #table_id=(value) ⇒ Object
- #td_class(record = nil) ⇒ Object
- #td_class=(value) ⇒ Object
- #td_id(record = nil) ⇒ Object
- #td_id=(value) ⇒ Object
-
#top_control? ⇒ Boolean
Determines whether the top control bar component is enabled.
- #tr_class(record = nil) ⇒ Object
- #tr_class=(value) ⇒ Object
- #tr_id(record = nil) ⇒ Object
- #tr_id=(value) ⇒ Object
-
#visible_columns ⇒ Object
Collects and returns all columns that are marked to be visible.
Methods inherited from Node
#attributes, #column, #columns, #columns_for, #name_chain, #root?
Constructor Details
#initialize(name, options = {}) ⇒ Grid
Initializes the fancygrid
61 62 63 64 65 |
# File 'lib/fancygrid/grid.rb', line 61 def initialize(name, = {}) self. = Fancygrid..merge() super(nil, name, self.) self.(self.) end |
Instance Attribute Details
#ajax_type ⇒ Object
The request method for the ajax callback e.g. GET or POST.
26 27 28 |
# File 'lib/fancygrid/grid.rb', line 26 def ajax_type @ajax_type end |
#ajax_url ⇒ Object
Url for the ajax callback.
23 24 25 |
# File 'lib/fancygrid/grid.rb', line 23 def ajax_url @ajax_url end |
#components ⇒ Object
Array of fancygrid component names that are going to be rendered.
29 30 31 |
# File 'lib/fancygrid/grid.rb', line 29 def components @components end |
#hide_search ⇒ Object
If true then hides the search bar, but keeps it enabled.
35 36 37 |
# File 'lib/fancygrid/grid.rb', line 35 def hide_search @hide_search end |
#leafs ⇒ Object
Collection of all defined columns.
6 7 8 |
# File 'lib/fancygrid/grid.rb', line 6 def leafs @leafs end |
#options ⇒ Object
Options that have been passed to initialize the fancygrid.
57 58 59 |
# File 'lib/fancygrid/grid.rb', line 57 def end |
#orm ⇒ Object
The class of the database connector implementation to be used.
12 13 14 |
# File 'lib/fancygrid/grid.rb', line 12 def orm @orm end |
#page_count ⇒ Object
If pagination is enabled this value holds the total number of available data pages.
54 55 56 |
# File 'lib/fancygrid/grid.rb', line 54 def page_count @page_count end |
#paginate ⇒ Object
Specifies whether pagination is enabled or not.
44 45 46 |
# File 'lib/fancygrid/grid.rb', line 44 def paginate @paginate end |
#per_page_value ⇒ Object
The default value for the per page drop down.
50 51 52 |
# File 'lib/fancygrid/grid.rb', line 50 def per_page_value @per_page_value end |
#per_page_values ⇒ Object
Array of select options for per page drop down.
47 48 49 |
# File 'lib/fancygrid/grid.rb', line 47 def per_page_values @per_page_values end |
#record_count ⇒ Object
Number of matching records. This is displayed in the pagination.
20 21 22 |
# File 'lib/fancygrid/grid.rb', line 20 def record_count @record_count end |
#records ⇒ Object
The result of the database query. This is the data that is going to be rendered.
17 18 19 |
# File 'lib/fancygrid/grid.rb', line 17 def records @records end |
#search_operator ⇒ Object
The default search operator.
41 42 43 |
# File 'lib/fancygrid/grid.rb', line 41 def search_operator @search_operator end |
#search_operators ⇒ Object
Array of enabled search operators.
38 39 40 |
# File 'lib/fancygrid/grid.rb', line 38 def search_operators @search_operators end |
#select ⇒ Object
Specifies whether sql select optimization should be performed
32 33 34 |
# File 'lib/fancygrid/grid.rb', line 32 def select @select end |
#view_state ⇒ Object
The grids view state.
9 10 11 |
# File 'lib/fancygrid/grid.rb', line 9 def view_state @view_state end |
Instance Method Details
#apply_options(options) ⇒ Object
Applies the given options and sets default values
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fancygrid/grid.rb', line 69 def () self.leafs = [] self.view_state = Fancygrid::ViewState.new(.fetch(:state_hash, {})) self.records = [] self.record_count = 0 self.orm = [:orm].classify.constantize self.ajax_url = nil self.ajax_type = [:ajax_type] self.components = [:components] self.hide_search = [:hide_search] self.search_operators = [:search_operators] self.search_operator = [:search_operator] self.per_page_values = [:per_page_values] self.per_page_value = self.view_state.pagination_per_page([:per_page_value]) self.paginate = true self.select = true self.page_count = 0 end |
#bottom_control? ⇒ Boolean
Determines whether the bottom control bar component is enabled.
144 145 146 |
# File 'lib/fancygrid/grid.rb', line 144 def bottom_control? dynamic? && component?(:bottom_bar) end |
#collect_columns ⇒ Object
Reorganizes the defined columns and post fixes some options
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/fancygrid/grid.rb', line 225 def collect_columns leafs.clear super(leafs) leafs.each do |leaf| leaf.position = self.view_state.column_option(leaf, :position, leaf.position).to_i leaf.width = self.view_state.column_option(leaf, :width, leaf.width) leaf.visible = self.view_state.column_option(leaf, :visible, leaf.visible).to_s == "true" leaf.search_operator = self.view_state.column_condition(leaf, :operator, leaf.search_operator) leaf.search_value = self.view_state.column_condition(leaf, :value, leaf.search_value) end leafs.sort! { |a, b| a.position <=> b.position } end |
#complex_search? ⇒ Boolean
Determines whether the complex search component is enabled.
132 133 134 |
# File 'lib/fancygrid/grid.rb', line 132 def complex_search? dynamic? && component?(:search) end |
#component?(name) ⇒ Boolean
Determines whether the given component is enabled or not.
120 121 122 |
# File 'lib/fancygrid/grid.rb', line 120 def component?(name) self.components.include?(name) end |
#dump_records ⇒ Object
Dumps the fetched records into an array of hashes that can be rendered as xml or cvs.
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/fancygrid/grid.rb', line 242 def dump_records result = [] self.records.each do |record| result << (dump = {}) self.visible_columns.each do |col| dump[col.identifier] = col.fetch_value_and_format(record) end end return result end |
#dynamic? ⇒ Boolean
Determines whether ajax callbacks are enabled or not
114 115 116 |
# File 'lib/fancygrid/grid.rb', line 114 def dynamic? self.ajax_url.present? end |
#find(&block) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fancygrid/grid.rb', line 92 def find &block self.collect_columns = {} [:grid] = self [:conditions] = self.view_state.conditions [:operator] = self.view_state.operator if self.select [:select] = self.leafs.select { |leaf| leaf.selectable }.map{ |leaf| leaf.identifier } end if self.paginate && self.dynamic? [:pagination] = self.view_state.(1, self.per_page_value) end self.records, self.record_count = self.orm.new().execute(resource_class, &block) self.page_count = (self.record_count.to_f / self.per_page_value.to_f).ceil end |
#hidden_columns ⇒ Object
Collects and returns all columns that are marked to be hidden. The collection is sorted by the column position attribute.
168 169 170 171 172 173 174 |
# File 'lib/fancygrid/grid.rb', line 168 def hidden_columns @hidden_columns ||= leafs.select { |leaf| leaf.hidden }.sort { |a, b| a.position <=> b.position } end |
#js_options ⇒ Object
Builds the javascript options for the javascript part of fancygrid.
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/fancygrid/grid.rb', line 212 def { :ajaxUrl => self.ajax_url, :ajaxType => self.ajax_type, :name => self.name, :page => self.view_state.pagination_page, :perPage => self.per_page_value, :searchVisible => !self.hide_search || !self.view_state.conditions.empty? }.to_json.gsub("<|>", "") end |
#operator_human_name(name) ⇒ Object
Gets the human readable name for given comparison operator.
196 197 198 199 200 201 |
# File 'lib/fancygrid/grid.rb', line 196 def operator_human_name(name) I18n.t(:"search.operator.#{name}", { :default => name.humanize, :scope => Fancygrid.i18n_scope }) end |
#search_filter(column, collection) ⇒ Object
Sets the search options for given column.
205 206 207 208 |
# File 'lib/fancygrid/grid.rb', line 205 def search_filter column, collection node = self.children.select { |leaf| leaf.respond_to?(:identifier) && leaf.identifier == column }.first node. = Array(collection).map { |v| v.is_a?(Array) ? v : [v.to_s, v.to_s]} end |
#select_column_options ⇒ Object
Generates options for dropdown selection to select a column.
178 179 180 181 182 183 184 |
# File 'lib/fancygrid/grid.rb', line 178 def leafs.select { |leaf| !leaf.hidden && leaf.searchable }.map { |leaf| [leaf.human_name, leaf.identifier] } end |
#select_operator_options ⇒ Object
Generates options for dropdown selection to select a comparison operator.
188 189 190 191 192 |
# File 'lib/fancygrid/grid.rb', line 188 def ||= self.search_operators.map do |op| [ self.operator_human_name(op.to_s), op.to_s ] end end |
#simple_search? ⇒ Boolean
Determines whether the simple search component is enabled.
126 127 128 |
# File 'lib/fancygrid/grid.rb', line 126 def simple_search? dynamic? && component?(:search_bar) end |
#sort_window? ⇒ Boolean
Determines whether the sort window component is enabled.
150 151 152 |
# File 'lib/fancygrid/grid.rb', line 150 def sort_window? dynamic? && component?(:sort_window) end |
#table_class ⇒ Object
304 305 306 |
# File 'lib/fancygrid/grid.rb', line 304 def table_class @table_class end |
#table_class=(value) ⇒ Object
301 302 303 |
# File 'lib/fancygrid/grid.rb', line 301 def table_class=value @table_class = value end |
#table_id ⇒ Object
311 312 313 |
# File 'lib/fancygrid/grid.rb', line 311 def table_id @table_id end |
#table_id=(value) ⇒ Object
308 309 310 |
# File 'lib/fancygrid/grid.rb', line 308 def table_id=value @table_id = value end |
#td_class(record = nil) ⇒ Object
280 281 282 283 284 285 286 287 |
# File 'lib/fancygrid/grid.rb', line 280 def td_class(record=nil) @td_class = Proc.new if block_given? if @td_class.is_a? Proc @td_class.call(record) if record else @td_class end end |
#td_class=(value) ⇒ Object
277 278 279 |
# File 'lib/fancygrid/grid.rb', line 277 def td_class=value @td_class = value end |
#td_id(record = nil) ⇒ Object
292 293 294 295 296 297 298 299 |
# File 'lib/fancygrid/grid.rb', line 292 def td_id(record=nil) @td_id = Proc.new if block_given? if @td_id.is_a? Proc @td_id.call(record) if record else @td_id end end |
#td_id=(value) ⇒ Object
289 290 291 |
# File 'lib/fancygrid/grid.rb', line 289 def td_id=value @td_id = value end |
#top_control? ⇒ Boolean
Determines whether the top control bar component is enabled.
138 139 140 |
# File 'lib/fancygrid/grid.rb', line 138 def top_control? dynamic? && component?(:top_bar) end |
#tr_class(record = nil) ⇒ Object
256 257 258 259 260 261 262 263 |
# File 'lib/fancygrid/grid.rb', line 256 def tr_class(record=nil) @tr_class = Proc.new if block_given? if @tr_class.is_a? Proc @tr_class.call(record) if record else @tr_class end end |
#tr_class=(value) ⇒ Object
253 254 255 |
# File 'lib/fancygrid/grid.rb', line 253 def tr_class=value @tr_class = value end |
#tr_id(record = nil) ⇒ Object
268 269 270 271 272 273 274 275 |
# File 'lib/fancygrid/grid.rb', line 268 def tr_id(record=nil) @tr_id = Proc.new if block_given? if @tr_id.is_a? Proc @tr_id.call(record) if record else @tr_id end end |
#tr_id=(value) ⇒ Object
265 266 267 |
# File 'lib/fancygrid/grid.rb', line 265 def tr_id=value @tr_id = value end |
#visible_columns ⇒ Object
Collects and returns all columns that are marked to be visible. The collection is sorted by the column position attribute.
157 158 159 160 161 162 163 |
# File 'lib/fancygrid/grid.rb', line 157 def visible_columns @visible_columns ||= leafs.select { |leaf| leaf.visible }.sort { |a, b| a.position <=> b.position } end |