我正在尝试使用匹配器来过滤掉特定的值,例如当用户键入“o”时,Omega 就会出现,但是使用匹配器时,我仍然得到所有 testdata.json 值,而不仅仅是过滤后的值。请帮我检查我哪里出错了。
// html file
<select type="search" placeholder="Search...." id="searchbar-top" class="form-control">
<option></option>
</select>
<script>
$(document).ready(function(){
$("#searchbar-top").select2({
matcher: function(term, text) {
console.log("this is text:" , text)
return text.toUpperCase().trim().indexOf(term.toUpperCase().trim())==0;
},
allowClear:true,
minimumInputLength: 1,
placeholder: "Search...",
templateResult: formatState,
width:'100%',
debug:true,
ajax:{
url:"/data",
type:"get",
dataType:"json",
data:function(params){
return {
searchTerm:params.term
};
},
processResults: function(data){
return{
results: $.map(data.content , function(item){
return {text:item.text , id:item.id}
})
}
},
},
});
function formatState (text) {
str ="";
str += "<p style='padding-left: 12px;'>"+ text.text + "</p>";
var $state = $(str);
return $state;
};
})
</script>
//testdata.json
{"content" : [
{ "id": 0, "text": "Rolex Daytona" },
{ "id": 1, "text": "Rolex GMT Master II " },
{ "id": 2, "text": "Rolex Submariner" },
{ "id": 3, "text": "Rolex Datejust" },
{ "id": 4, "text": "Rolex Day-Date" },
{ "id": 4, "text": "Rolex Morphin" },
{ "id": 4, "text": "Rolex Nalgene" },
{ "id": 4, "text": "Rolex AKG" },
{ "id": 5, "text": "Omega Speedmaster" },
{ "id": 6, "text": "Omega Seamaster" },
{ "id": 7, "text": "Omega Constellation" },
{ "id": 8, "text": "Omega Deville" },
{ "id": 9, "text": "Omega Deneve" },
{ "id": 10,"text": "Cartier Tank" },
{ "id": 11,"text": "Cartier Ballon Bleu" },
{ "id": 12,"text": "Cartier Santos" },
{ "id": 13,"text": "Cartier Pasha" },
{ "id": 14,"text": "Cartier Panthara" },
{ "id": 15,"text": "Tissot Heritage" },
{ "id": 16,"text": "Tissot T-Sport" },
{ "id": 17,"text": "Tissot T-Classic" },
{ "id": 18,"text": "Tissot Touch" },
{ "id": 19,"text": "Tissot Touch" }
]}
是的,所以基本上我使用 ajax 函数从 testdata.json 调用数据,我的匹配器函数有问题吗?