BigW Consortium Gitlab

select2_helper.rb 627 Bytes
Newer Older
Johannes Schleifenbaum committed
1
# Select2 ajax programmatic helper
2 3 4 5 6 7 8 9 10 11 12 13
# It allows you to select value from select2
#
# Params
#   value - real value of selected item
#   opts - options containing css selector
#
# Usage:
#
#   select2(2, from: '#user_ids')
#

module Select2Helper
14
  def select2(value, options = {})
15
    raise ArgumentError, 'options must be a Hash' unless options.kind_of?(Hash)
16

17
    selector = options.fetch(:from)
18 19

    if options[:multiple]
20
      execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');")
21
    else
22
      execute_script("$('#{selector}').select2('val', '#{value}').trigger('change');")
23 24 25
    end
  end
end