callback on initSelection

1,496 views
Skip to first unread message

Brad Lilly

unread,
Jul 9, 2012, 10:09:04 AM7/9/12
to sel...@googlegroups.com
Hi There,

A bit stuck on using a callback on initSelection where I need to retrieve a single value back from an ajax call.

I added this patch here.

https://github.com/alanho/select2/commit/af1c39807611538db65886a227e9bf7227f4adba

All seems OK and I get a valid JSON response (below) however the <span> tag is empty.

{"results":[{"id":"4255","text":"Party Tour"}]}


I was working off the sample in the above url for a result.

My code sample is below. Any help would be appreciated.

$('#activitycats').select2({
placeholder: 'Search for an activity',
ajax: {
url: "/ajax/categoriesactivities.php",
dataType: 'json',
quietMillis: 100,
data: function (term, page) {
return {
term: term, //search term
page_limit: 10 // page size
};
},
results: function (data, page) {
return { results: data.results };
}

},
initSelection: function(element, callback) {
return $.getJSON("/ajax/categoriesactivities.php?id=" + (element.val()), null, function(data) {
if ($.isFunction(callback)) {
return data;
}
});
}

});













Igor Vaynberg

unread,
Jul 9, 2012, 10:12:27 AM7/9/12
to sel...@googlegroups.com
you need to return just the array portion, no need for the "results" key.

-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/IoxeDWvz-7cJ.
> To post to this group, send email to sel...@googlegroups.com.
> To unsubscribe from this group, send email to
> select2+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/select2?hl=en.

Brad Lilly

unread,
Jul 9, 2012, 10:41:17 AM7/9/12
to sel...@googlegroups.com
Thanks Igor I  tried that as below. Still no luck.


[{"id":"4255","text":"Party Tour"}]


Igor Vaynberg

unread,
Jul 9, 2012, 10:51:24 AM7/9/12
to sel...@googlegroups.com
this part is also wrong:

if ($.isFunction(callback)) {
return data;
}

should instead be

callback(data);

-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/RJQTe7zBoScJ.

Brad Lilly

unread,
Jul 9, 2012, 11:17:22 AM7/9/12
to sel...@googlegroups.com
Thanks Igor tried that as well and not working for me. Quite tired now and off for some sleep. But if you have any more ideas I would welcome them. A fresh start in the morning might give me results.


On Tuesday, 10 July 2012 00:09:04 UTC+10, Brad Lilly wrote:

Brad Lilly

unread,
Jul 9, 2012, 6:41:39 PM7/9/12
to sel...@googlegroups.com
Thanks Igor a bit of sleep did the trick. I still had an array of values and after running firebug worked out the problem.
Needed it as below without []
{"id":"4252","text":"Abseiling"}


I did see some people asking for PHP examples of server side did you want some of these?




On Tuesday, 10 July 2012 00:09:04 UTC+10, Brad Lilly wrote:

Igor Vaynberg

unread,
Jul 10, 2012, 12:50:44 AM7/10/12
to sel...@googlegroups.com
On Tue, Jul 10, 2012 at 1:41 AM, Brad Lilly <chickp...@gmail.com> wrote:
> Thanks Igor a bit of sleep did the trick. I still had an array of values and
> after running firebug worked out the problem.
> Needed it as below without []
> {"id":"4252","text":"Abseiling"}
>
> I did see some people asking for PHP examples of server side did you want
> some of these?

would be cool to have some on the wiki, yes.

thanks,
-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/QT98I7rmQnoJ.

Brad Lilly

unread,
Jul 11, 2012, 6:48:52 AM7/11/12
to sel...@googlegroups.com
Thanks Igor. This works for me... Hope it can help others.


On Tuesday, 10 July 2012 00:09:04 UTC+10, Brad Lilly wrote:
select2_sample.php

Nico Williams

unread,
Aug 26, 2025, 2:08:24 PM (11 days ago) Aug 26
to select2
Guideatour is a convenient platform for travelers to discover guided tours and unique experiences around the world. With just a few clicks, users can find and book the perfect activities for their trips. It makes every journey more organized, exciting, and personalized.
Reply all
Reply to author
Forward
0 new messages