Class: Aurita::GUI::Selection_List_Field
- Inherits:
-
Options_Field
- Object
- Array
- Element
- Form_Field
- Options_Field
- Aurita::GUI::Selection_List_Field
- Defined in:
- lib/aurita-gui/form/selection_list.rb
Overview
A selection list maintains a list of available options and a select field of further available options.
Example:
Selection_List_Field.new(:name => :the_list,
:value => ['10','30' ] # Active selection from options
:options => { '10' => :blue, # List of all options
'20' => :red,
'30' => :green }
In the example, any combination of ‘blue’, ‘red’ and ‘green’ could be selected, here it is ‘blue’ and ‘green’. The select field contains 20 => ‘red’, the only additionally available option (as it is not already set in value).
Use case: Assign user to categories. Options is all available categories, value is an array of category ids already assigned to this user.
Customization
You can override the Form_Field class rendering the option list elements, as well as the Form_Field class rendering the select field.
Use #option_field_decorator to override rendering of option fields. Default is Selection_List_Option_Field.
Example for custom option field decorator:
class Delete_Option_Decorator < Form_Field
def element
HTML.div {
HTML.img(:src => '/images/delete.png') + @label
}
end
end
selection_list.option_field_decorator = Delete_Option_Decorator
Example for custom select field class:
Note that Selection_List_Field will initialize key / label map of available options.
class Ajax_Selection_Field < Form_Field
def initialize(params={})
super(params)
params[:onchange] = js.do_something.ajaxian.with(:this, )
end
def element
HTML.div {
Input_Field.new(@attrib)
}
end
end
selection_list.select_field_class = Ajax_Selection_Field
Instance Attribute Summary collapse
-
#option_field_decorator ⇒ Object
Returns the value of attribute option_field_decorator.
-
#select_field_class ⇒ Object
Returns the value of attribute select_field_class.
-
#selectable_options ⇒ Object
Returns the value of attribute selectable_options.
Attributes inherited from Options_Field
#option_labels, #option_values, #value
Attributes inherited from Form_Field
#data_type, #form, #hidden, #hint, #invalid, #label, #required, #type, #value
Attributes inherited from Element
#attrib, #force_closing_tag, #parent, #tag
Instance Method Summary collapse
-
#element ⇒ Object
Renders list of active options, as well as select field element containing additionally available options.
-
#initialize(params = {}) ⇒ Selection_List_Field
constructor
A new instance of Selection_List_Field.
-
#option_elements ⇒ Object
Returns array of available options, decorated by @option_field_decorator (see comments on Selection_List_Field).
-
#options=(options) ⇒ Object
Set list of all options as value / label hash.
-
#value=(value) ⇒ Object
(also: #set_value)
Set list of active selection options as array.
Methods inherited from Options_Field
#[], #[]=, #add_option, #content, #option_hash, #options
Methods inherited from Form_Field
#disable!, #disabled=, #editable!, #enable!, #hidden?, #hide!, #invalid!, #invalid?, #optional!, #readonly!, #readonly=, #readonly?, #readonly_element, #required!, #required?, #show!, #to_hidden_field, #to_s
Methods inherited from Element
#+, #<<, #[], #[]=, #add_class, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #method_missing, #recurse, #remove_class, #set_content, #string, #swap, #to_ary, #type=
Methods included from Marshal_Helper_Class_Methods
Methods included from Marshal_Helper
Constructor Details
#initialize(params = {}) ⇒ Selection_List_Field
Returns a new instance of Selection_List_Field.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/aurita-gui/form/selection_list.rb', line 93 def initialize(params={}) @option_field_decorator = params[:option_field] @select_field_class = params[:select_field] @option_field_decorator ||= Selection_List_Option_Field @select_field_class ||= Select_Field = params[:selectable_options] ||= [] params.delete(:option_field) params.delete(:select_field) params.delete(:selectable_options) super(params) set_value(@value) add_css_class(:selection_list_field) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Aurita::GUI::Element
Instance Attribute Details
#option_field_decorator ⇒ Object
Returns the value of attribute option_field_decorator.
91 92 93 |
# File 'lib/aurita-gui/form/selection_list.rb', line 91 def option_field_decorator @option_field_decorator end |
#select_field_class ⇒ Object
Returns the value of attribute select_field_class.
91 92 93 |
# File 'lib/aurita-gui/form/selection_list.rb', line 91 def select_field_class @select_field_class end |
#selectable_options ⇒ Object
Returns the value of attribute selectable_options.
91 92 93 |
# File 'lib/aurita-gui/form/selection_list.rb', line 91 def end |
Instance Method Details
#element ⇒ Object
Renders list of active options, as well as select field element containing additionally available options.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/aurita-gui/form/selection_list.rb', line 152 def element = {} .each { |v| [v] = @option_labels[v] } id_prefix = "#{@attrib[:id]}_" if @attrib[:id] if @value && @value.length > 0 then HTML.div(@attrib) { HTML.ul(:id => "#{id_prefix}#{@attrib[:name]}") { option_elements() } + @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select", :options => , :name => "#{@attrib[:name]}" ) } else HTML.div(@attrib) { @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select", :options => , :name => "#{@attrib[:name]}" ) } end end |
#option_elements ⇒ Object
Returns array of available options, decorated by @option_field_decorator (see comments on Selection_List_Field).
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/aurita-gui/form/selection_list.rb', line 132 def option_elements id_prefix = "#{@attrib[:id]}_" if @attrib[:id] elements = [] ().each_pair { |opt_value, opt_label| selected = @value.map { |v| v.to_s }.include?(opt_value.to_s) if selected then elements << HTML.li(:id => "#{id_prefix}#{@attrib[:name]}_#{opt_value}") { @option_field_decorator.new(:name => @attrib[:name], :value => opt_value, :label => opt_label, :parent => self) } end } elements end |
#options=(options) ⇒ Object
Set list of all options as value / label hash.
110 111 112 113 114 115 116 117 |
# File 'lib/aurita-gui/form/selection_list.rb', line 110 def () super() if ! || .length == 0 then ().each_pair { |value, name| << value unless (@value.is_a?(Array) && @value.map { |v| v.to_s }.include?(value.to_s)) } end end |
#value=(value) ⇒ Object Also known as: set_value
Set list of active selection options as array.
120 121 122 123 124 125 126 |
# File 'lib/aurita-gui/form/selection_list.rb', line 120 def value=(value) super(value) = [] ().each_pair { |value, name| << value unless @value.map { |v| v.to_s }.include? value.to_s } end |