简体   繁体   English

select2不显示选项

[英]select2 not displaying options

I am using select2 library to send an ajax request:我正在使用select2库发送 ajax 请求:

I am getting the data back when I search for something but for some reason the results do not show up in the drop down.当我搜索某些内容时,我正在取回数据,但由于某种原因,结果没有显示在下拉列表中。

    $('.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
    }
  });

This is the data I can see in my console:这是我可以在控制台中看到的数据:

[
 {
   studentNumber :"12324"
 },
 {
   studentNumber :"12324"
 },

]

This is what the documentataion does:这就是文档所做的:

$('#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
      };
    }
  }
});

The data you have received after transformation is an array .转换后收到的data是一个array Calling data. studentNumber调用data. studentNumber data. studentNumber would yield undefined since data is not an object. data. studentNumber将产生未定义的数据,因为数据不是 object。

Just return the data itself since the results is anyway expected to be an array .只需返回数据本身,因为results无论如何都应该是一个array

In simple words, change this:简单来说,改变这个:

return {
    results: data. studentNumber
};

to

return {
    results: data
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM