My Select2 3.4.5 does not work with JSON data.
Here is my HTML input field:
<input class='form-control col-lg-5 itemSearch' type='text' placeholder='select item' />
... and my javascript
$(".itemSearch").select2({ placeholder: "Search for an Item", minimumInputLength: 2, ajax: { url: "/api/productSearch", dataType: 'json', quietMillis: 100, data: function (term, page) { return { option: term }; }, results: function (data, page) { var more = (page * 10) < data.total; return { results: data.itemName, more: more }; } }, formatResult: function (data, term) { return data; }, formatSelection: function (data) { return data; }, dropdownCssClass: "bigdrop", escapeMarkup: function (m) { return m; } });
I created an API with Laravel 4 that returns a value whenever I type anything in my text box.
Here's the result if I type "test" in my text box:
[{"itemName":"Test item no. 1","id":5}, {"itemName":"Test item no. 2","id":6}, {"itemName":"Test item no. 3","id":7}, {"itemName":"Test item no. 4","id":8}, {"itemName":"Test item no. 5","id":9}, {"itemName":"Test item no. 6","id":10}, {"itemName":"Test item no. 7","id":11}]
I cannot add the result to the Select2 drop-down list. I think formatSelection and formatResult are causing the problem because I don't know which parameter should be placed on it. I donβt know where to get parameters such as the container, object and request, and the values ββthat it should return, or is my JSON response incorrect?
json javascript laravel jquery-select2
melvnberd Jan 04 2018-11-14T00: 00Z
source share