create tokens in jquery token input

假装没事ソ 提交于 2019-12-10 14:29:16

问题


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.



来源:https://stackoverflow.com/questions/16091254/create-tokens-in-jquery-token-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!