callback on initSelection

1,533 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 PMAug 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.

Phill Jones

unread,
Oct 16, 2025, 2:20:29 PMOct 16
to select2
Los requisitos de la Beca Rita Cetina buscan garantizar que el apoyo llegue a estudiantes comprometidas con su educación. Para postularse, las candidatas deben estar inscritas en una institución educativa, mantener un buen promedio y demostrar necesidad económica. Asimismo, deben entregar la documentación requerida y respetar los plazos establecidos en la convocatoria oficial.

Brahim Diaz

unread,
Nov 8, 2025, 12:32:59 PMNov 8
to select2
GTA Mzansi APK is a popular mobile adaptation of the classic Grand Theft Auto game, tailored for South African players. It offers open-world gameplay, missions, and vehicles with local-themed elements. Players can enjoy an immersive action experience right on their Android devices.

Isaac Glover

unread,
Nov 14, 2025, 4:55:23 AM (9 days ago) Nov 14
to select2
 The issue you’re facing with Select2’s initSelection is similar to ensuring data renders correctly when pulled from dynamic sources. Platforms like thefameplanet show how properly structured and mapped JSON can display cleanly once processed. Likewise, your callback must correctly translate the returned {id, text} pair so the <span> element reflects the selected value without remaining empty.  

Joseph

unread,
Nov 22, 2025, 9:05:20 PM (19 hours ago) Nov 22
to select2
  I've been really impressed with the quality and variety available at Gander Mountain products. Whether you're looking for camping essentials, hunting gear, or just reliable outdoor equipment, they have such a great selection. I recently picked up some items for a weekend trip and everything held up perfectly. It's nice to find a place that truly understands what outdoor enthusiasts need!  
Reply all
Reply to author
Forward
0 new messages