php - Select2 - Ajax Data - Populate The Dropdown based on query -
i'm using select2 populate dropdown of uk towns. uk towns db huge figured ajax call best way bring in data.
i have built post function , php (in codeigniter) catch query , parse it.
i can see data being posted , responded, select2 not populating data.
my jquery :
$("#areas").select2( { tags: [], ajax: { url: '/profile/get-towns', datatype: 'json', type: "post", quietmillis: 100, data: function (term) { return { query: term }; }, results: function (data) { return { results: data.town_id } }, cache: true }, escapemarkup: function (markup) { return markup; }, // let our custom formatter work minimuminputlength: 4, placeholder : "start typing town / city", maximumselectionsize: 2 } );
my response json (example) follows :
[{"town_id":"16994","town":"hartle"},{"town_id":"16995","town":"hartlebury"},{"town_id":"16996","town" :"hartlebury"},{"town_id":"16997","town":"hartlebury common"},{"town_id":"16998","town":"hartlepool" },{"town_id":"16999","town":"hartley"},{"town_id":"17000","town":"hartley"},{"town_id":"17001","town" :"hartley"},{"town_id":"17002","town":"hartley"},{"town_id":"17003","town":"hartley green"},{"town_id" :"17004","town":"hartley green"},{"town_id":"17005","town":"hartley mauditt"},{"town_id":"17006","town" :"hartley wespall"},{"town_id":"17007","town":"hartley wintney"},{"town_id":"27051","town":"new hartley" },{"town_id":"35891","town":"stowe-by-chartley"}]
where going wrong? ideally select dropdown have select value = town_id , select option town name.
thank you.
in select2
configuration:
results: function (data) { var res = []; for(var = 0 ; < data.length; i++) { res.push({id:data[i].town_id, text:data[i].town}); } return { results: res } },
becasue select2
wants results array of object keys id
, text
.
otherwise returns formed object
[ {"id":"16994","text":"hartle"}, {"id":"16995","text":"hartlebury"}, {"id":"16996","text":"hartlebury"}, {"id":"16997","text":"hartlebury common"} ]
then
results: function (data) { return { results: data } },
Comments
Post a Comment