【发布时间】:2015-01-13 01:26:47
【问题描述】:
我在我的网站上实现了一个基本的 select2 ajax 元素:
$(function(){
$('#appbundle_marketplace_product_ingredient_barcode').select2({
minimumInputLength: 10,
multiple: false,
allowClear: true,
quietMillis: 50,
placeholder: 'Commencez à taper le code barre ici',
ajax: {
data: function (term) {
return {
q: term // search term
};
},
url: function(term) {
url="http://fr.openfoodfacts.org/api/v0/produit/" + term +".json";
return url;
},
dataType: 'json',
results: function(data) {
if (data.status!=1) {return;}
if (data.product.complete!=1) {return;}
return {results: [{
text: data.code + " - " + data.product.product_name,
slug: data.code,
id: data.code
}]};
},
}
});
});
我期待借助模板方法 formatResult、formatSelection 和 InitSelection 更好地显示。 我已经阅读了网上的文档(不完全理解)和示例。
虽然我能理解每种方法应该做什么,但我无法让它们正常工作。
- json 响应是否有必需的格式(在我的示例中,我得到一个对象,我只能通过将其转换为对象数组上的对象来使其工作?$ !!? 示例网址:http://fr.openfoodfacts.org/api/v0/produit/3029330003533.json
关于formatResult、formatSelection和InitSelection:
- 它们的属性的格式是/应该是什么(数组/对象/哪些键?)?
- 响应的格式是/应该是什么?
- select 2 如何调用它们以及如何处理它们的响应?
任何帮助理解这种行为将不胜感激!
【问题讨论】:
标签: jquery ajax json jquery-select2