Pet*_*ter 3 javascript typeahead.js bloodhound bootstrap-tags-input
我有一个由Bootstrap Tagsinput. 目前,此文本输入使用Typeahead自动完成中使用的单个标签列表进行初始化:
$('.tagsInput').tagsinput({
maxChars: 25,
maxTags: 5,
typeaheadjs: [{
minLength: 1,
highlight: true
},{
minlength: 1,
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: allTags
})
}],
freeInput: true
});
Run Code Online (Sandbox Code Playgroud)
与allTags作为字符串的JSON数组。
然而,我想要实现的是,不是拥有一个简单的标签列表,即allTags,我将拥有某种可以访问不同列表的功能,然后能够根据某些元素的值选择正确的列表。例如,会有一个下拉列表,根据所选的值,将使用不同的标签列表。我无法做到这一点。
不过,我找到了一种解决方法,基本上,我使用文本输入元素的类名,并在与列表一样多的类名上初始化尽可能多的 tagsInput。这有效,但它似乎过于体操而不是真正干净的实现。
有任何想法吗?
更新: 这是HTML:
<input id="tags" name="tags" data-role="tagsinput" class="form-control input-sm tagsInput typeahead" type="text" placeholder="Enter some tags" title="Please enter some tags" value="" required>
Run Code Online (Sandbox Code Playgroud)
这select是一个正常的select。
当select更改时,您调用一个函数来告诉 Bloodhound 使用哪个数组(或 json 文件/url),然后要求它重新运行其initialize函数以更新 typeahead 建议列表。
网址:
<h1>Typeahead Test </h1>
<select onchange="changeList(this.value)">
<option selected value="countries">Countries</option>
<option value="cities">Cities</option>
</select>
<input id="tags" name="tags" data-role="tagsinput" class="form-control input-sm tagsInput typeahead" type="text" placeholder="Enter some tags" title="Please enter some tags" value="" required>
<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://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
<script src="bootstrap-tagsinput.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
Javascript:
var country_list = ["Afghanistan", "Albania", "Algeria", "Andorra", "Auckland", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia & Herzegovina", "Botswana", "Brazil"];
var city_list = ["Amsterdam", "London", "Paris", "Washington", "New York", "Los Angeles", "Sydney", "Melbourne", "Canberra", "Beijing", "New Delhi", "Kathmandu", "Cairo", "Cape Town", "Kinshasa"];
var myData = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: $.map(country_list, function(item) {
return {
value: item
};
})
});
myData.initialize();
$('#tags').tagsinput({
typeaheadjs: {
name: 'value',
displayKey: 'value',
valueKey: 'value',
source: myData.ttAdapter()
}
});
// utilised some code from http://stackoverflow.com/a/23000444/1544886
changeList = function(val) {
source = (val === 'cities') ? city_list : country_list;
myData.clear(); // First remove all suggestions from the search index
myData.local = $.map(source, function(item) {
return {
value: item
};
});
myData.initialize(true); // Finally reinitialise the bloodhound suggestion engine
};
Run Code Online (Sandbox Code Playgroud)
这是一个plunker:http ://plnkr.co/edit/6mjDrgDwqzWeLJQOcA3D?p=preview
| 归档时间: |
|
| 查看次数: |
1864 次 |
| 最近记录: |