Any idea not to select the name that is already ch

2019-08-09 11:27发布

问题:

Is there any idea not to select the name that is already chosen in token input? For example In first I select mango when I search for m. Next time again when I search for m Mango should not be displayed as mango is already chosen. Is there any idea?

 <%= f.text_field :plan_tokens, data: {load: @customer.plans} %>

I have my js like this jQuery ->

 $('#customer_plan_tokens').tokenInput '/plans.json'
    theme: 'facebook'
    prePopulate: $('#customer_plan_tokens').data('load')

where to put preventDuplicates:true. I did try but error arise!

I study this from

http://railscasts.com/episodes/258-token-fields

http://railscasts.com/episodes/258-token-fields-revised


回答1:

When you initialize tokeninput in your JavaScript, you can add preventDuplicates: true and this will prevent duplicates from being selected, but they'll still be shown in the results (demo).

So something like:

$(...).tokenInput("url", { preventDuplicates: true, otherOptionsHere });

If you don't want them shown at all, it would probably be possible to write some code for the onResult callback that checks the value of the field and removes the ones that are already there.

edit: since you use CoffeeScript, try

$('#customer_plan_tokens').tokenInput '/plans.json'
   theme: 'facebook'
   prePopulate: $('#customer_plan_tokens').data('load')
   preventDuplicates: true