Class | WWW::Mechanize::Form::MultiSelectList |
In: |
lib/www/mechanize/form/multi_select_list.rb
|
Parent: | Field |
This class represents a select list where multiple values can be selected. MultiSelectList#value= accepts an array, and those values are used as values for the select list. For example, to select multiple values, simply do this:
list.value = ['one', 'two']
Single values are still supported, so these two are the same:
list.value = ['one'] list.value = 'one'
options | [RW] |
# File lib/www/mechanize/form/multi_select_list.rb, line 15 15: def initialize(name, node) 16: value = [] 17: @options = WWW::Mechanize::List.new 18: 19: # parse 20: (node/'option').each do |n| 21: option = Option.new(n, self) 22: @options << option 23: end 24: super(name, value) 25: end
# File lib/www/mechanize/form/multi_select_list.rb, line 27 27: def query_value 28: value ? value.collect { |v| [name, v] } : '' 29: end
Select all options
# File lib/www/mechanize/form/multi_select_list.rb, line 38 38: def select_all 39: @value = [] 40: options.each { |o| o.tick } 41: end
Select no options
# File lib/www/mechanize/form/multi_select_list.rb, line 32 32: def select_none 33: @value = [] 34: options.each { |o| o.untick } 35: end
Get a list of all selected options
# File lib/www/mechanize/form/multi_select_list.rb, line 44 44: def selected_options 45: @options.find_all { |o| o.selected? } 46: end
# File lib/www/mechanize/form/multi_select_list.rb, line 60 60: def value 61: value = [] 62: value.push(*@value) 63: value.push(*selected_options.collect { |o| o.value }) 64: value 65: end