2013-07-29 06:46:00 -04:00
|
|
|
# Select2 ajax programmatic helper
|
2013-03-14 04:16:27 -04:00
|
|
|
# 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
|
2016-08-05 22:03:01 -04:00
|
|
|
def select2(value, options = {})
|
2015-06-22 14:52:41 -04:00
|
|
|
raise ArgumentError, 'options must be a Hash' unless options.kind_of?(Hash)
|
2013-03-14 04:16:27 -04:00
|
|
|
|
2015-06-22 14:52:41 -04:00
|
|
|
selector = options.fetch(:from)
|
2013-03-14 04:16:27 -04:00
|
|
|
|
|
|
|
if options[:multiple]
|
2015-03-27 03:27:51 -04:00
|
|
|
execute_script("$('#{selector}').select2('val', ['#{value}'], true);")
|
2013-03-14 04:16:27 -04:00
|
|
|
else
|
2015-03-27 03:27:51 -04:00
|
|
|
execute_script("$('#{selector}').select2('val', '#{value}', true);")
|
2013-03-14 04:16:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|