user596502
user596502

Reputation: 427

Display values from a source in form of tags

I am using Bootstrap tags input plugin. I want to display some values in form of tags on a specified input field. These values are retreived from db most likely in json format. I followed to use add method as shown in the documentation https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples but could not make it work.

My fiddle demo is here: https://jsfiddle.net/Lh4jy9d4/90/ $('input #expertise').tagsinput('add', {id: 1, text: 'javascript' })

Upvotes: 0

Views: 1748

Answers (1)

n0m4d
n0m4d

Reputation: 832

As @Siavas mentioned, the filtering was not correct as per your markup. I changed $('input #expertise') to $('#expertise') because the input type had that id.

Your example didn't work because you forgot to initialize the tagsInput. Check this working snippet adapted from their git-hub example.

        var citynames = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            prefetch: {
                url: 'https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/assets/citynames.json',
                filter: function (list) {
                    return $.map(list, function (cityname) {
                        return { name: cityname };
                    });
                }
            }
        });
        citynames.initialize();

        $('input').tagsinput({
            typeaheadjs: {
                name: 'citynames',
                displayKey: 'name',
                valueKey: 'name',
                source: citynames.ttAdapter()
            }
        });
<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>


<div class="form-group">
    <label class="col-lg-3 control-label">Expertise</label>
    <div class="col-lg-5">
        <input type="text" name="cities" id="expertise" class="form-control"  />
    </div>
</div>
<div class="form-group">
    <label class="col-lg-3 control-label">Interests</label>
    <div class="col-lg-5">
        <input type="text" name="cities1" id="interests" class="form-control" value="" data-role="tagsinput" />
    </div>
</div>
   




<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>

Upvotes: 1

Related Questions