Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify tokenSeparator? #87

Closed
sepastian opened this issue May 6, 2014 · 4 comments
Closed

How to specify tokenSeparator? #87

sepastian opened this issue May 6, 2014 · 4 comments

Comments

@sepastian
Copy link

Hello,

I am using select2-rails with ActiveAdmin to edit a list of tags backed by acts_as_taggable_on. How do I specify a value for tokenSeparator, as described in the docs for select2 AutoTokenization?

See also my question on Stackoverflow!

@argerim
Copy link
Owner

argerim commented May 28, 2014

hum, can see your html ?

@sepastian
Copy link
Author

Hi, it is on Stackoverflow.

There is no HTML, in ActiveAdmin the form is build in the model like so:

# app/admin/model.rb
form do |f|
  f.inputs do
    f.input :keyword_list, as: :select2_tags
  end
end

So what I am trying to achieve is passing options to select2 on the line that says f.input :keyword_list, as: :select2_tags.

Any ideas?

@sepastian
Copy link
Author

Ok, so after creating the formatsic input with

f.input :keyword_list, as: :select2_tags, :input_html => { 'data-create-options' => "{tokenSeparators:[';']}" }

I end up with the following HTML <input> tag

<input class="select2-input" data-create-options="{&quot;width&quot;:&quot;76%&quot;,&quot;tags&quot;:[...]}" data-create-options="{tokenSeparators:[&#x27;;&#x27;]}" id="item_keyword_list" name="item[keyword_list]" type="text" value="1 2 3; 1,2,3,Paul: Dokumente" />

As you can see, two data-create-options attributes are created - which won't work, of course. How can I append my custom options (tokenSeparators) to the first data-create-options attribute?

@sepastian
Copy link
Author

Well, all my fault - haven't been working on this project for a while and forgot what I did back then (embarrasing). 😊

I am using a custom Formtastic::Inputs::StringInput for rendering select2 tags.

# app/inputs/select2_tags_input.rb
class Select2TagsInput < Formtastic::Inputs::StringInput
  def input_html_options
    default_options = { width: '76%', tags: ActsAsTaggableOn::Tag.all.map(&:name) }
    create_options = options[:create_options] || {}
    create_options.merge!(default_options)
    data = { create_options: create_options }
    super.merge(:class => 'select2-input', data: data)
  end
end

To specify custom create options, I do

f.input :keyword_list, as: :select2_tags, :create_options => { :tokenSeparators => [";"] }

which will now render correctly as

<input class="select2-input" data-create-options="{&quot;tokenSeparators&quot;:[&quot;;&quot;],&quot;width&quot;:&quot;76%&quot;,&quot;tags&quot;:[...]}" id="item_keyword_list" name="item[keyword_list]" type="text" value="Federn, Paul: Dokumente" />

Select2 is still refusing to use ';' as a token separator, though, but that has nothing to do with select2-rails. Thanks anyways!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants