ajax processresults,javascript-select2中的processResult不起作用

我使用select2加载远程数据,我发送了一个ajax请求并正确获取了响应,但是processResult不运行,并且不会显示任何内容

var formatProduct=

function(product) {

console.log("formatProduct");

if (product.loading) return product.text;

var markup = '

' + product.name + '
' ;

return markup;

}

var formatProductSelection =

function (product) {

console.log("formatProductSelection");

return product.name || product.text;

}

$(".js-data-example-ajax").select2({

placeholder: "Search for product",

minimumInputLength: 2,

ajax: {

url: '/product/ajax_product_list/',

dataType: 'json',

quietMillis: 300,

data: function (params) {

var id = $('#product-no-1').attr('data') ;

return {

key: params,

id: id

};

},

processResults: function (data) {

return {

results: data.items

};

},

cache: true

},

escapeMarkup: function (markup) { return markup; }, // let our custom formatter work

minimumInputLength: 1,

templateResult: formatProduct, // omitted for brevity, see the source of this page

templateSelection: formatProductSelection // omitted for brevity, see the source of this page

});

以及控制器返回的JSON作为响应:

{

"items": [

{"id": "55dd980c8242b630cfaf4788", "name": "sallll"},

{"id": "55d58d7a8242b62c6b88504e", "name" : "inja"},

{"id": "55d58d558242b62c6b88504d", "name": "salam"}

]

}

解决方法:

您应该重命名JSON以返回文本而不是名称.

请注意,如果您使用的是select2(< 4)的旧版本,则应使用结果而不是processResults.

标签:jquery-select2,html,javascript

来源: https://codeday.me/bug/20191119/2038978.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值