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 = {})
|
2017-02-22 12:25:50 -05:00
|
|
|
raise ArgumentError, 'options must be a Hash' unless options.is_a?(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
|
|
|
|
2017-10-16 13:37:52 -04:00
|
|
|
first(selector, visible: false)
|
2013-03-14 04:16:27 -04:00
|
|
|
if options[:multiple]
|
2016-10-20 04:48:38 -04:00
|
|
|
execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');")
|
2013-03-14 04:16:27 -04:00
|
|
|
else
|
2016-10-20 04:48:38 -04:00
|
|
|
execute_script("$('#{selector}').select2('val', '#{value}').trigger('change');")
|
2013-03-14 04:16:27 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-30 08:14:32 -04:00
|
|
|
|
|
|
|
def open_select2(selector)
|
|
|
|
execute_script("$('#{selector}').select2('open');")
|
|
|
|
end
|
|
|
|
|
|
|
|
def scroll_select2_to_bottom(selector)
|
|
|
|
evaluate_script "$('#{selector}').scrollTop($('#{selector}')[0].scrollHeight); $('#{selector}');"
|
|
|
|
end
|
2013-03-14 04:16:27 -04:00
|
|
|
end
|