Skip to content

Commit 1d8db6c

Browse files
committed
Merge pull request #2 from mikz/tags
add select2 tags input
2 parents cf3a6ec + dc580a5 commit 1d8db6c

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
$(document).on 'has_many_add:after', '.has_many_container', (e, fieldset) ->
2-
fieldset.find('.select2-input').select2({allowClear: true })
1+
'use strict';
2+
3+
initSelect2 = (inputs, extra = {}) ->
4+
inputs.each ->
5+
item = $(this)
6+
# reading from data allows <input data-select2='{"tags": ['some']}'> to be passed to select2
7+
options = $.extend(allowClear: true, extra, item.data('select2'))
8+
# because select2 reads from input.data to check if it is select2 already
9+
item.data('select2', null)
10+
item.select2(options)
311

12+
$(document).on 'has_many_add:after', '.has_many_container', (e, fieldset) ->
13+
initSelect2(fieldset.find('.select2-input'))
414

515
$(document).ready ->
6-
$(".select2-input").select2 placeholder: "", allowClear: true
16+
initSelect2($(".select2-input"), placeholder: "")
717
return

lib/activeadmin-select2.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
require 'activeadmin/inputs/filter_select2_multiple_input'
44
require 'formtastic/inputs/select2_input'
55
require 'formtastic/inputs/select2_multiple_input'
6+
require 'formtastic/inputs/select2_tags_input'
67

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'formtastic/inputs/string_input'
2+
3+
module Formtastic
4+
module Inputs
5+
6+
class Select2TagsInput < Formtastic::Inputs::StringInput
7+
def input_html_options
8+
{
9+
class: 'select2-input select2-tags-input',
10+
data: { select2: { tags: options[:collection] }}
11+
}.merge(super)
12+
end
13+
end
14+
15+
end
16+
end

0 commit comments

Comments
 (0)