Sri*_*Sri 70 javascript jquery jquery-select2
我正在用select2进行标记
我对select2有这些要求:
这两个场景都是独立运作的 但加在一起的aJax值只是填充.如果我们输入不在列表中的任何其他值,则表示"找不到匹配项"
我的场景如果用户键入列表中没有的任何新值,请允许他们组成自己的标记.
有什么办法使这项工作?
Chr*_*rds 97
Select2具有"createSearchChoice"功能:
从用户的搜索词创建一个新的可选择的选项.允许创建通过查询功能不可用的选项.当用户可以动态创建选项时很有用,例如用于"标记"用例.
您可以通过以下方式实现您的目标:
createSearchChoice:function(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term)===0;
}).length===0) {
return {id:term, text:term};
}
},
multiple: true
Run Code Online (Sandbox Code Playgroud)
这是一个更完整的答案,它将JSON结果返回给ajax搜索,并允许从该术语创建标记,如果该术语返回没有结果:
$(".select2").select2({
tags: true,
tokenSeparators: [",", " "],
createSearchChoice: function(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
multiple: true,
ajax: {
url: '/path/to/results.json',
dataType: "json",
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {
results: data
};
}
}
});
Run Code Online (Sandbox Code Playgroud)
Ale*_*rov 34
http://jsfiddle.net/8qL47c1x/2/
HTML:
<select multiple="multiple" class="form-control" id="tags" style="width: 400px;">
<option value="tag1">tag1</option>
<option value="tag2">tag2</option>
</select>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
$('#tags').select2({
tags: true,
tokenSeparators: [','],
ajax: {
url: 'https://api.myjson.com/bins/444cr',
dataType: 'json',
processResults: function(data) {
return {
results: data
}
}
},
// Some nice improvements:
// max tags is 3
maximumSelectionLength: 3,
// add "(new tag)" for new tags
createTag: function (params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: term,
text: term + ' (new tag)'
};
},
});
Run Code Online (Sandbox Code Playgroud)
有一些改进的例子:
HTML:
<input type="hidden" id="tags" value="tag1,tag2" style="width: 400px;">
Run Code Online (Sandbox Code Playgroud)
JS:
$('#tags').select2({
tags: true,
tokenSeparators: [','],
createSearchChoice: function (term) {
return {
id: $.trim(term),
text: $.trim(term) + ' (new tag)'
};
},
ajax: {
url: 'https://api.myjson.com/bins/444cr',
dataType: 'json',
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {
results: data
};
}
},
// Take default tags from the input value
initSelection: function (element, callback) {
var data = [];
function splitVal(string, separator) {
var val, i, l;
if (string === null || string.length < 1) return [];
val = string.split(separator);
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
return val;
}
$(splitVal(element.val(), ",")).each(function () {
data.push({
id: this,
text: this
});
});
callback(data);
},
// Some nice improvements:
// max tags is 3
maximumSelectionSize: 3,
// override message for max tags
formatSelectionTooBig: function (limit) {
return "Max tags is only " + limit;
}
});
Run Code Online (Sandbox Code Playgroud)
JSON:
[
{
"id": "tag1",
"text": "tag1"
},
{
"id": "tag2",
"text": "tag2"
},
{
"id": "tag3",
"text": "tag3"
},
{
"id": "tag4",
"text": "tag4"
}
]
Run Code Online (Sandbox Code Playgroud)
更新2015-01-22:
修复jsfiddle:http://jsfiddle.net/X6V2s/66/
更新2015-09-09:
使用Select2 v4.0.0 +它变得更容易.
https://jsfiddle.net/59Lbxvyc/
HTML:
<select class="tags-select" multiple="multiple" style="width: 300px;">
<option value="tag1" selected="selected">tag1</option>
<option value="tag2" selected="selected">tag2</option>
</select>
Run Code Online (Sandbox Code Playgroud)
JS:
$(".tags-select").select2({
// enable tagging
tags: true,
// loading remote data
// see https://select2.github.io/options.html#ajax
ajax: {
url: "https://api.myjson.com/bins/444cr",
processResults: function (data, page) {
return {
results: data
};
}
}
});
Run Code Online (Sandbox Code Playgroud)
createSearchChoice : function (term) { return {id: term, text: term}; }
Run Code Online (Sandbox Code Playgroud)
只需添加此选项