How to autocomplete boot token using ajax?

I want to use the token field for bootstrap: http://sliptree.imtqy.com/bootstrap-tokenfield/ , but I cannot find documentation on how to do this using AJAX. I have a .php file with json data like this {"Hello", "Helium", "Hell"}and I want these to be autocomplete values. Please note that the .php file returns only those values ​​that are similar to the entered. Can anyone find a way to do this? Any help would be greatly appreciated. I just want to use this great boot tokenfield to autocomplete tags and prevent autocomplete if the word does not exist there.

Thanks.

+4
source share
3 answers

Boot token field - autotomplete autocomplete with remote call using ajax

Prerequisitics:

= stylesheet_link_tag 'sales_crm/tokenfield-typeahead.css'
= stylesheet_link_tag 'sales_crm/bootstrap-tokenfield.css'


%script{:src => "//code.jquery.com/ui/1.10.3/jquery-ui.js", :type => "text/javascript"}
= javascript_include_tag  "sales_crm/bootstrap-tokenfield.js"
= javascript_include_tag  "sales_crm/typeahead.bundle.min.js"

In the HAML View File:

%input#tokenfield-typeahead.form-control.add_locality_data{:type => "text", :value => "", :identifier=> "Locality"}/

1) Initialize BloodHound Search Engine

    var engine = new Bloodhound({
                remote: {
                    url: '/sales_crm/leads/get_lead_associated_info?query=%QUERY&model_class='+$('.add_locality_data').attr("identifier"),
                    filter: function (response) {
                        var tagged_user = $('#tokenfield-typeahead').tokenfield('getTokens');
                        console.log(tagged_user)
                        return $.map(response.leads, function (user) {
                            console.log(user)
                            var exists = false;
        //                    console.log("velava saranam");
                            for (i=0; i < tagged_user.length; i++) {
                                if (user.value == tagged_user[i].value) {
                                    var exists = true;
                                }
                            }
                            if (!exists) {
                                return {
                                    value: user.value,
                                    label: user.label
                                };
                            }
                        });
                    }
                },
                datumTokenizer: function (d) {
                    return Bloodhound.tokenizers.whitespace(d.value);
                },
                queryTokenizer: Bloodhound.tokenizers.whitespace
            });

engine.initialize();

2) Then initialize the token field function

$('#tokenfield-typeahead').tokenfield({
    delimiter: false,
    typeahead: [
           {
            name: 'users',
            displayKey: 'label',
            source: engine.ttAdapter()
        }
    ]
})
    .on('tokenfield:createtoken', function (e) {
        var existingTokens = $(this).tokenfield('getTokens');
        if (existingTokens.length) {
            $.each(existingTokens, function(index, token) {
                console.log(token)
                if (token.value === e.attrs.value) {
                    e.preventDefault();
                }else{
                    console.log(e.attrs.value)
                }
            });
        }else{
            console.log(e.attrs.value)
        }
    });
+4
source

You need to implement either autocomplete from jquery-ui, or typeaheadfrom twitter, and then apply certain parameters.

$('#activity_tag_tokens').tokenfield({
  typeahead: {
    prefetch: '/tags.json',
    remote: '/tags.json?q=%QUERY',
  }
});
+2
source

Alright ... I found a solution

$('#tokenfield1').tokenfield();
            var mots_cles = "";
            $.each(e.valeur_tableau_infos_tutoriel.Mots_cles, function  (index,value){
                mots_cles += value.mots_cles+',';
            })
            $('#tokenfield1').tokenfield('setTokens', mots_cles)
        }
0
source

All Articles