返回有效的JSON字符串时,jQuery select2无法正常工作

时间:2016-06-22 23:51:25

标签: jquery codeigniter select2

我正在使用CodeIgniter中的一个项目,并且在将25,000行加载到选择框时遇到了问题(很惊讶吗?)。因此,我想要一个可搜索的选择框,并在输入搜索查询时加载到远程数据集中。我发现这个library名为select2,它似乎是我的问题的解决方案。

然而,即使我返回一个有效的JSON字符串,我也似乎无法工作。

[{"Model_Name":"1340"},{"Model_Name":"1640"},{"Model_Name":"1645"},{"Model_Name":"1647"},{"Model_Name":"435MT"},{"Model_Name":"435T"},{"Model_Name":"7100"},{"Model_Name":"8000"},{"Model_Name":"8100"},{"Model_Name":"9000"},{"Model_Name":"9100"}]

以下代码中是否有任何明显错误?我的console.log是上面的字符串来自的地方。

 $("#part").select2({
ajax: {
  url: "home/populateModel/25",
  dataType: 'json',
  delay: 250,
  data: function (params) {
    return {
      q: params.term, // search term
      page: params.page
    };
  },
  processResults: function (data, params) {
    // parse the results into the format expected by Select2
    // since we are using custom formatting functions we do not need to
    // alter the remote JSON data, except to indicate that infinite
    // scrolling can be used
    params.page = params.page || 1;
    console.log(JSON.stringify(data));
    return {
      results: JSON.stringify(data),
      pagination: {
        more: (params.page * 30) < data.total_count
      }
    };
  },
  cache: true
},
escapeMarkup: function (markup) { 
  //console.log(markup);
  return markup; 
}, // let our custom formatter work
minimumInputLength: 1
//templateResult: formatRepo, // omitted for brevity, see the source of this page
//templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});

0 个答案:

没有答案