Boxing token box with auto padding with ajax not working on remote

Html code:
input type="text" class="form-control" id="tokenfield-typeahead" value="red,green,blue" />

script:
var engine = new Bloodhound({
  local: [{value: 'red'}, {value: 'blue'}, {value: 'green'} , {value: 'yellow'}, {value: 'violet'}, {value: 'brown'}, {value: 'purple'}, {value: 'black'}, {value: 'white'}],
  datumTokenizer: function(d) {
    return Bloodhound.tokenizers.whitespace(d.value);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace
});

engine.initialize();

$('#tokenfield-typeahead').tokenfield({
  typeahead: [null, { source: engine.ttAdapter() }]
});

      

the above bootstrap token field works in local mode, but when accessed with remote it doesn't work.

+3


source to share


1 answer


I faced the same problem and got a solution for the product I am working on.

I assume you should add these css and js files to your view file

= 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"

      

View file: Since I was using Haml, added the line below to the view file where you need to add the token field functionality

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

      



Step 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();

      

Step 2: Then initialize the field token 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







All Articles