Module: Datatable
- Defined in:
- lib/datatablesnet/datatable.rb
Defined Under Namespace
Classes: UrlHelper
Class Method Summary collapse
- .build_condition(klass, params = {}, options = {}) ⇒ Object
- .build_condition_internal(klass, grid_options, conditions) ⇒ Object
- .build_row_meta(row, grid_options) ⇒ Object
- .convert_param(klass, field_name, value) ⇒ Object
- .datatable_get_column_data(row, column) ⇒ Object
- .datatable_get_column_defs(options, columns) ⇒ Object
- .datatable_get_language_defs(options) ⇒ Object
-
.datatable_tag(id, columns = [], rows = nil, options = {}) ⇒ Object
The main method for generating the necessary javascript and html to display a datatable.
- .field_to_label(field) ⇒ Object
- .find(klass, params = {}, options = {}) ⇒ Object
- .get_field_data(obj, field) ⇒ Object
- .parse_params(klass, params) ⇒ Object
- .template_path ⇒ Object
Class Method Details
.build_condition(klass, params = {}, options = {}) ⇒ Object
326 327 328 329 |
# File 'lib/datatablesnet/datatable.rb', line 326 def build_condition(klass, params={}, = {}) = parse_params klass, params build_condition_internal(klass, , [:conditions].present? ? [:conditions] : nil) end |
.build_condition_internal(klass, grid_options, conditions) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/datatablesnet/datatable.rb', line 331 def build_condition_internal(klass, , conditions) field_filter_where = {} all_filter_where = {} search_filter_where = {} [:filter_by].each do |column, value| field_filter_where[column] = value end # Search all searchable columns if [:search].present? [:columns].each do |column| if column[:searchable].to_b all_filter_where[column[:field]] = [:search] end end end #process search_by [:search_by].each do |column, value| if value.instance_of?(Hash) if value[:to].present? search_filter_where[column] = (value[:from]..value[:to]) else search_filter_where[column] = value[:from] end else search_filter_where[column] = value end end if !field_filter_where.empty? or !all_filter_where.empty? or !search_filter_where.empty? sql_where = [field_filter_where.sql_like, all_filter_where.sql_or.sql_like, search_filter_where.sql_and].sql_and.sql_where if conditions if conditions.instance_of?(Array) condition_string = conditions[0] condition_params = conditions[1..-1] else condition_string = conditions condition_params = [] end condition_string << " and (" << sql_where[0] << ")" condition_params += sql_where[1..-1] conditions = [condition_string] + condition_params else conditions = sql_where end end conditions end |
.build_row_meta(row, grid_options) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/datatablesnet/datatable.rb', line 446 def row, url_helper = UrlHelper.new ={} [:urls] = {} [:columns].each do |column| if column[:view_link].present? view_link = column[:view_link] if view_link == "." url = url_helper.url_for(row) else link_obj = row.send(view_link) if link_obj url = url_helper.url_for(link_obj) else url = nil end end [:urls][column[:field]] = url end end return end |
.convert_param(klass, field_name, value) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/datatablesnet/datatable.rb', line 307 def convert_param klass, field_name, value case klass.columns_hash[field_name].type when :string klass.validators_on(field_name).each do |validator| if validator.instance_of?(ActiveModel::Validations::NumericalityValidator) if validator.[:only_integer] value = value.to_i end end end when :date, :datetime value = Date.parse(value) when :integer, :float, :decimal value = value.to_i end value end |
.datatable_get_column_data(row, column) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/datatablesnet/datatable.rb', line 219 def datatable_get_column_data row, column obj = get_field_data(row, column[:field]) if column[:view_link].present? if column[:view_link] == "." return link_to(obj, row) else link_obj = row.send(column[:view_link]) if link_obj return link_to(obj, link_obj) else return obj end end else return obj end end |
.datatable_get_column_defs(options, columns) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/datatablesnet/datatable.rb', line 162 def datatable_get_column_defs , columns column_defs = [] = {:width => "sWidth", :sortable => "bSortable", :searchable => "bSearchable", :class => "sClass"} column_index =0 columns.each do |column| = {} .each do |k,v| if column[k].present? [v] = column[k] unless k==:width and request.env['HTTP_USER_AGENT'] =~ /Firefox/ end end ["fnRender"] = NoEscape.new([:render]) if column[:render].present? ["fnRender"] = NoEscape.new("function (obj) {return #{column[:format]}(obj.aData[#{column_index}])}") if column[:format].present? if column[:view_link].present? and [:server_side] ["fnRender"] = NoEscape.new("function (obj) {if(obj.aData[#{columns.length}]['urls']['#{column[:field]}']!=null) {return '<a href = ' + obj.aData[#{columns.length}]['urls']['#{column[:field]}'] + '>' + obj.aData[#{column_index}] + '</a>'} else {return obj.aData[#{column_index}]} }") end unless .empty? column_defs << else column_defs << nil end column_index+=1 end if [:show_actions] column_defs << nil end if [:server_side] if request.env['HTTP_USER_AGENT'] =~ /Firefox/ column_defs << {"sClass" => "hidden"} else column_defs << {"bVisible" => false} end end return column_defs end |
.datatable_get_language_defs(options) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/datatablesnet/datatable.rb', line 147 def datatable_get_language_defs = {:info_empty => "sInfoEmpty", :info_filtered => "sInfoFiltered", :empty_table => "sEmptyTable", :zero_records => "sZeroRecords"} = {} .each do |k,v| if [k].present? [v] = [k] end end end |
.datatable_tag(id, columns = [], rows = nil, options = {}) ⇒ Object
The main method for generating the necessary javascript and html to display a datatable. This supports both ajax and non-ajax datatables.
The following ar the input parameters:
-
id
- The html id of the datatable -
columns
- Array of columns to be displayed. Each column is represented by a hash containing
column configuration
-
rows
- Rows to be displayed if the table is non-ajax -
options
- Table options
The following are the table options:
:sort_by :additional_data :search :search_label :processing :persist_state :per_page :no_records_message :auto_width :row_callback :show_actions :info_callback
The following are Column Options :name :label :render :format
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/datatablesnet/datatable.rb', line 71 def datatable_tag id, columns = [], rows = nil, = {} = {:sort_by => "aaSorting", :search => "bFilter", :search_label => "sSearch", :processing => "bProcessing", :persist_state => "bSaveState", :per_page => "iDisplayLength", :no_records_message => "sZeroRecords", :auto_width => "bAutoWidth", :dom => "sDom", :data_url => "sAjaxSource", :server_side => "bServerSide", :auto_scroll => "bScrollInfinite", :pagination_type => "sPaginationType", :paginate => "bPaginate", :save_state => "bStateSave", } = { :show_actions => false, :per_page => 25, :toolbar_external => false, :show_only_when_searched => false }.merge() if [:data_url].present? url = URI::split([:data_url]) end index = 0 columns.each do |column| column[:label] = field_to_label(column[:field]) unless column[:label].present? if [:data_url].present? [:data_url] << "?" if column == columns.first and url[7] == nil [:data_url] << "&" if column == columns.first and url[7] != nil [:data_url] << "column_field_#{index.to_s}=#{column[:field]}" if column[:view_link].present? [:data_url] << "&column_view_link_#{index.to_s}=#{column[:view_link]}" end [:data_url] << "&" unless column == columns.last end index += 1 end if [:show_only_when_searched] [:data_url] << "&show_only_when_searched=true" end if [:toolbar].present? or [:toolbar_external] [:dom]='<"toolbar">lfrtip' end = {} .each do |k,v| if [k].present? [v] = [k] end end if [:language].present? puts "language present" ["oLanguage"] = datatable_get_language_defs([:language]) end ["fnInfoCallback"] = NoEscape.new([:info_callback]) if [:info_callback].present? ["aoColumns"] = datatable_get_column_defs(,columns) config = {:options => , :columns =>columns, :table_options => } config.to_json render :partial => "datatablesnet/table", :locals => { :table_id => id, :columns => columns, :rows => rows, :config => config} end |
.field_to_label(field) ⇒ Object
238 239 240 241 |
# File 'lib/datatablesnet/datatable.rb', line 238 def field_to_label(field) label = field.gsub(/[_.]/, " ") label.to_s.gsub(/\b\w/){$&.upcase} end |
.find(klass, params = {}, options = {}) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/datatablesnet/datatable.rb', line 382 def find(klass, params={}, = {}) puts params.to_json = parse_params klass, params [:page] = [:page] [:per_page] = [:per_page] # Build order by [:order_by].each do |column, order| if column if [:order].present? [:order] << ", " else [:order] = "" end [:order] << "#{column} #{order}" end end if [:show_only_when_searched] if [:search_by].empty? json_data = {:sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => []} return json_data end end # Build filter by [:conditions] = build_condition_internal(klass, , [:conditions].present? ? [:conditions] : nil) puts [:conditions] rows = klass.paginate objects = [] rows.each do |row| object = [] [:columns].each do |column| object << get_field_data(row, column[:field]) end = row, object << objects << object end total_records = klass.count if [:conditions].present? total_display_records = klass.count(:conditions => [:conditions]) else total_display_records = total_records end json_data = {:sEcho => params[:sEcho], :iTotalRecords => total_records, :iTotalDisplayRecords => total_display_records, :aaData => objects } return json_data end |
.get_field_data(obj, field) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/datatablesnet/datatable.rb', line 207 def get_field_data obj, field attrs = field.split('.') attrs.each do |attr| if obj obj = obj.send attr else return "" end end return obj end |
.parse_params(klass, params) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/datatablesnet/datatable.rb', line 243 def parse_params klass, params = {} columns = [] order_by = {} filter_by = {} if params["show_only_when_searched"].present? [:show_only_when_searched] = true else [:show_only_when_searched] = false end (0..params[:iColumns].to_i-2).each do |index| column = {} column[:field] = params["column_field_#{index}"] column[:searchable] = params["bSearchable_#{index}"].to_b column[:sortable] = params["bSortable_#{index}"].to_b column[:index] = index if params["column_view_link_#{index}"].present? column[:view_link] = params["column_view_link_#{index}"] end columns << column if params["iSortCol_#{index}"].present? order_column_index = params["iSortCol_#{index}"] order_column = params["column_field_#{order_column_index}"] order_column_order = params["sSortDir_#{index}"] || "DESC" order_by[order_column] = order_column_order end if params["sSearch_#{index}"].present? filter_by[column[:field]] = params["sSearch_#{index}"] end end search_by = {} params.each do |key, value| if key =~ /query_.*/ and !value.empty? if key =~ /.*_to/ field_name = key[6..-4] search_by[field_name] = {} unless search_by[field_name].present? search_by[field_name][:to] = convert_param(klass, field_name, value) elsif key =~ /.*_from/ field_name = key[6..-6] search_by[field_name] = {} unless search_by[field_name].present? search_by[field_name][:from] = convert_param(klass, field_name,value) else field_name = key[6..-1] search_by[field_name] = convert_param(klass, field_name, value) end end end [:search_by] = search_by [:columns] = columns [:order_by] = order_by [:filter_by] = filter_by [:page] = (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i rescue 0)+1 [:per_page] = params[:iDisplayLength] if params["sSearch"].present? [:search] = params["sSearch"] end end |
.template_path ⇒ Object
32 33 34 35 36 |
# File 'lib/datatablesnet/datatable.rb', line 32 def template_path path = File.('..', __FILE__) $:.unshift(path) path end |