php - How to autocomplete bootstrap tokenfield with ajax?

424

I want to use the tokenfield for bootstrap: http://sliptree.github.io/bootstrap-tokenfield/ but I can't seem to find any documentation on how to do it using AJAX. I have a .php file with json data like this{"Hello", "Helium", "Hell"} and I want it to be the autocomplete values. Please note that the .php file only returns values that are similar to what is being typed. Can anyone find a way to do this? Any help would be highly appreciated. I just wanna use that gorgeous bootstrap tokenfield to autocomplete tags and disallow autocomplete if the word's don't exist there.

Thanks.

330

Answer

Solution:

You need to implement either autocomplete fromjquery-ui ortypeahead from twitter, and then apply specific options.

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

Answer

Solution:

Bootstrap tokenfield - typeahead 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 HAML View File:

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

1) Initialize the 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 tokenfield 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)
        }
    });
150

Answer

Solution:

It's OK ... I find the 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)
        }

People are also looking for solutions to the problem: php - DOM Xpath wordpress Grab content

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.