create tokens in jquery token input

2019-05-03 09:31发布

问题:

Presently my jquery token input is working perfectly fine.

Am not able to create token, which is not in the list

I have seen here, that this functionality is implemented. But there is no documentation on how we i can use this.

Can any one help me with documentation or demo

js_js.js

    $(document).ready(function () {
        $("#job_skills").tokenInput("/jobs/search_job_skills", {
            theme: "facebook",
            preventDuplicates: true,
            hintText: 'Add skills need for job',
            searchingText: 'searching skills...',
            allowCreation: true,
            creationText: 'Add new element'
        });

    });

cons_controller.rb

  def search_job_skills
    search_for_json(Skill)
  end

  def search_for_json(model_search)
    @hash = []

    @search_res = model_search.where(['name LIKE ?', "#{params[:term]}%"])

    @search_res.each do |tag|
      @hash << { id: tag.id,
                 name: tag.name}
    end
    render json: @hash
  end

回答1:

Include allowFreeTagging: true when you initiate.

Unfortunately, the documentation hasn't been updated in a few years.

Also note that if you set allowFreeTagging to true, you will want to change the tokenValue to "name", because when you save the tag on your server, you probably want to save the name, not the id.

Here is a look at my token options

tokenOptions = {
    allowFreeTagging: true,
    tokenValue: 'name'
}

$('input#tag-input').tokenInput('/tags.json', tokenOptions);

This way, when a user selects tags, the names are sent to the server, and if there are any new tag names, I simply create them server-side.