select2-4 remote data pre-select

173 views
Skip to first unread message

yohan...@gmail.com

unread,
Jul 23, 2015, 9:54:55 AM7/23/15
to select2
Hi guys I have a problem with the new select2 version n.4.

I can not select a value default..this is my code


$("#select").select2({

ajax: {
url: urlajax,
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page,
term: params.term,
};
},
processResults: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
//results: data.items
};
},
cache: true
},

escapeMarkup: function (markup) { return markup; },
,

allowClear: true,
// templateResult: formatRepo, // omitted for brevity, see the source of this page
//templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
})


I would like to select a value default but with this version without initSelection I can't

t

Kevin Brown

unread,
Jul 23, 2015, 1:25:20 PM7/23/15
to select2
The following Stack Overflow question may be helpful


With a few exceptions, complex data objects being one of them, you can typically avoid `initSelection` with a `<select>` in 4.0.0. There is a ticket on GitHub about improving the documentation around this [1] if you are interested in helping out or have suggestions.

It's also turned into the ticket for ranting about the documentation, but let's avoid that.

--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.

David

unread,
Aug 4, 2015, 6:01:03 PM8/4/15
to select2, ke...@kevin-brown.com
Hi,

I had the same issue as the OP, and this code snippet, found in a comment in the ticket that you mentioned (https://github.com/select2/select2/issues/3116) was very helpful:
https://jsfiddle.net/o45d0td6/

I find this a lot dirtier than simply having the possibility to pass default selected IDs as an option (cf what can be done for instance with immybox and it's "defaultValue" option - https://rawgit.com/immense/immybox/master/demo.html), but it's still reasonably short and simple IMO.
Reply all
Reply to author
Forward
0 new messages