select2 不显示选项
select2 not displaying options
我正在使用 select2
库发送 ajax 请求:
当我搜索某些内容时,我正在取回数据,但由于某种原因,结果没有显示在下拉列表中。
$('.js-example-basic-single').select2({
ajax: {
url:"/search/search",
type:"POST",
data: function (params) {
// Query parameters will be ?search=[term]&type=public
var query = {
search: params.term,
type: 'public'
}
return query;
},
processResults: function (data) {
// Transforms the top-level key of the response object from 'items' to 'results'
data = JSON.parse(data)
console.log(data)
return {
results: data.studentNumber
};
}
// Additional AJAX parameters go here; see the end of this chapter for the full code of this example
}
});
这是我可以在控制台中看到的数据:
[
{
studentNumber :"12324"
},
{
studentNumber :"12324"
},
]
这是文档的作用:
$('#mySelect2').select2({
ajax: {
url: '/example/api',
processResults: function (data) {
// Transforms the top-level key of the response object from 'items' to 'results'
return {
results: data.items
};
}
}
});
你得到的data
变形后是一个array
。调用 data. studentNumber
会产生 undefined 因为数据不是对象。
只是 return 数据本身,因为 results
无论如何都应该是 array
。
简单来说,改变这个:
return {
results: data. studentNumber
};
到
return {
results: data
};
我正在使用 select2
库发送 ajax 请求:
当我搜索某些内容时,我正在取回数据,但由于某种原因,结果没有显示在下拉列表中。
$('.js-example-basic-single').select2({
ajax: {
url:"/search/search",
type:"POST",
data: function (params) {
// Query parameters will be ?search=[term]&type=public
var query = {
search: params.term,
type: 'public'
}
return query;
},
processResults: function (data) {
// Transforms the top-level key of the response object from 'items' to 'results'
data = JSON.parse(data)
console.log(data)
return {
results: data.studentNumber
};
}
// Additional AJAX parameters go here; see the end of this chapter for the full code of this example
}
});
这是我可以在控制台中看到的数据:
[
{
studentNumber :"12324"
},
{
studentNumber :"12324"
},
]
这是文档的作用:
$('#mySelect2').select2({
ajax: {
url: '/example/api',
processResults: function (data) {
// Transforms the top-level key of the response object from 'items' to 'results'
return {
results: data.items
};
}
}
});
你得到的data
变形后是一个array
。调用 data. studentNumber
会产生 undefined 因为数据不是对象。
只是 return 数据本身,因为 results
无论如何都应该是 array
。
简单来说,改变这个:
return {
results: data. studentNumber
};
到
return {
results: data
};