Class: StaticDataListWidget
- Inherits:
-
BaseList
- Object
- Cuca::Widget
- BaseList
- StaticDataListWidget
- Defined in:
- lib/cuca/stdlib/listwidget/staticdatalist.rb
Overview
Displays data from memory
Example:
StaticDataList(‘list_name’,
:columns => [ { :id => 'id', :display=>'ID' },
{ :id => 'name', :display=>'Name' } ],
:data => [[ 1, 'Jack'],
[ 2, 'Elvis'],
[ 3, 'Alice']])
Instance Attribute Summary
Attributes inherited from BaseList
Instance Method Summary collapse
- #apply_filters(query_def) ⇒ Object
- #col_idx_by_id(col) ⇒ Object
- #columns ⇒ Object
- #filter(data, column_name, value) ⇒ Object
- #output(list_name = "noname", data_setup = {}) ⇒ Object
- #query(query_def) ⇒ Object
- #setup ⇒ Object
Methods inherited from BaseList
#add_rewrite_hook, #check_query_def, #load_query_definition, #rewrite_field
Methods included from Cuca::Generator::Markaby
Methods included from Cuca::Generator::View
#view, #view_p, #viewtext, #viewtext_p
Methods inherited from Cuca::Widget
#app, #cgi, #clear, clear_hints, #content, #content=, #controller, define_attr_method, #escape, #escapeHTML, #get_assigns, #hints, #initialize, #log, #params, #query_parameters, #request_method, #request_parameters, run_attr_method, #session, #to_s, #unescape, #unescapeHTML
Constructor Details
This class inherits a constructor from Cuca::Widget
Instance Method Details
#apply_filters(query_def) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 50 def apply_filters(query_def) query_def.filters.each_pair do |k,v| @data = filter(@data, k,v) end # $stderr.puts "Done filtering: #{@data.inspect}" end |
#col_idx_by_id(col) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 70 def col_idx_by_id(col) @sd_columns.each_index do |idx| if @sd_columns[idx][:id] == col then return idx end end return nil end |
#columns ⇒ Object
29 30 31 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 29 def columns return @sd_columns end |
#filter(data, column_name, value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 33 def filter(data, column_name, value) cidx = col_idx_by_id(column_name) return data if cidx.nil? return data if value.strip.empty? new_data = [] data.each_index do |didx| if data[didx][cidx].instance_of?(String) then # $stderr.puts "Filter: #{@data[didx][cidx]} on #{value}" new_data << data[didx] if data[didx][cidx].include?(value) # $stderr.puts @new_data.inspect end end # $stderr.puts "Filter done(#{column_name}, #{value}): #{new_data.inspect}" return new_data end |
#output(list_name = "noname", data_setup = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 19 def output(list_name = "noname", data_setup = {}) @sd_columns = data_setup[:columns] || [] @sd_data = data_setup[:data] || [] @list_name = list_name setup @sd_columns.freeze @sd_data.freeze super(@list_name) end |
#query(query_def) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 59 def query(query_def) cidx = col_idx_by_id(query_def.order_by) || 0 @data = @sd_data.sort { |a,b| a[cidx] <=> b[cidx] } @data = @data.reverse if query_def.order == 'DESC' apply_filters(query_def) @total_rows = @data.size @data = @data[query_def.range] end |
#setup ⇒ Object
15 16 17 |
# File 'lib/cuca/stdlib/listwidget/staticdatalist.rb', line 15 def setup super end |