create tokens in jquery token input
Asked Answered
O

1

5

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
Osbert answered 18/4, 2013 at 19:13 Comment(0)
P
8

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.

Parapet answered 27/6, 2013 at 14:44 Comment(10)
Yay! You have saved me from having to rebuild this!Merger
@bwheeler96 Glad it helped someone, if not the OP!Parapet
Definitely. I had to do a little extra digging, I added my findings to your post. Thanks!Merger
@bwheeler96 - Unnecessary, no? When new tags are created the ID and Name are by default set to the same value, so for your free tags, the name value will be returned to the server regardless of whether tokenValue is set to name or id. It is of course possible that you could want to return name values instead of id's for your normal tags, but that's completely separate functionality, and not related to this question. Can you clarify why you'd specifically need to do this here? Thought I'd ask before rolling back, I've probably misunderstood you! =)Parapet
Hmmm... in my experience with this, the jquery token-input was sending only the ID, making it impossible to generate new tags on the server side. You can roll back if you feel it necessary, but I think that it's more explicit this way.Merger
Yes, but for a 'freetag' - the id is the same as the name, isn't it?Parapet
Yes, but then how do you distinguish between ID's and names? For example what if a user tags 365, or 24/7. This would make type identifying impossible right? Best let the DB decide the ID IMHOMerger
Ah, I see. I personally detect this by if (name == id) - as I already use the tokenValue attribute to store other information for each token. I see what you're getting at now though!Parapet
Yea, while what I added may not be 100% necessary, it fits nicely within the Principles Of Least SurpriseMerger
Hey buddy thank you so much i did lot of RND for customize plugin for my regular need and finally I got some part of it from here.Celom

© 2022 - 2024 — McMap. All rights reserved.