Class: HtmlCom::Dropdown

Inherits:
InputElement show all
Defined in:
lib/htmlcom.rb

Instance Method Summary collapse

Methods inherited from Element

#build, #html_element, #to_doc

Constructor Details

#initialize(rawoptions = [], options: rawoptions, label: nil, id: nil) ⇒ Dropdown

Returns a new instance of Dropdown.



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
# File 'lib/htmlcom.rb', line 384

def initialize(rawoptions=[], options: rawoptions, label: nil, id: nil)

  @tag = 'dropdown'
  @htmltag = 'select'
  @id = id
  @label = label

  if options.is_a? Array then

    list = options.map {|option| {option: option}}
    super({_select: list})

    values = options.map(&:downcase)
    @doc.root.xpath('dropdown/select/option').each.with_index do |e,i|
      e.attributes[:value] = values[i]
    end

  elsif options.is_a? Hash then

    list = options.values.map {|option| {option: option}}
    values = options.keys
    super({_select: list})

    @doc.root.xpath('dropdown/select/option').each.with_index do |e,i|
      e.attributes[:value] = values[i]
    end


  end
end