Unable to select a result from the select2 search results

后端 未结 4 966
庸人自扰
庸人自扰 2020-12-06 11:05

I am using the select2 for on of my search boxes. I\'m getting the results from my URL but I\'m not able to select an option from it. I want to use the \'product.productName

4条回答
  •  悲&欢浪女
    2020-12-06 11:13

    Since I was using AJAX, what worked for me was returning something as the ID on processResults:

    $(field).select2({
       ajax: {
            // [..] ajax params here
            processResults: function(data) {
                return {
                    results: $.map(data, function(item) {
                        return {
                            // proccessResults NEEDS the attribute id here
                            id: item.code,
                            // [...] other attributes here
                            foo: item.bar,
                        }
                    })
                }
            },
        },
    });
    

提交回复
热议问题