New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set data after init #3185
Comments
hello, |
there is a sample for version 2.3.5.2. for loading data via a function, $("#e10_4").select2({
data:function() { return { text:'tag', results: data }; },
formatSelection: format,
formatResult: format
}); |
+1 $('#my_select').select2({
data: [{id: 1, text: 'a'}, {id: 2, text: 'b'}, {id: 3, text: 'c'}] // load all the possible options
})
someExternalParameters.onChange = function () {
var activeOptions = defineBasedOnExternalParameters();
$('#my_select').find('option').each(function(){
if (-1 === activeOptions.indexOf($(this).val())) {
$(this).hide()
} else {
$(this).show()
}
});
} but it's not so good solution as: $('#my_select').select2({
data: function () {
var options = defineBasedOnExternalParameters();
return options;
}
}) |
Right now passing So a way that you can work around this, which is similar to what you would do if you were working with a native $("select option").remove(); or // .empty() If you need to preserve existing selections, then you can filter those out as well $("select option:not(:selected)").remove(); |
Can you add this code: // Set initial data
var initData = this.options.get('initData');
if (initData && $.isArray(initData)) {
initData.forEach(function(item) {
var $option = self.dataAdapter.option(item)[0];
$option.selected = true;
self.dataAdapter.addOptions($option);
});
} to project before: // Set the initial state
this.dataAdapter.current(function (initialData) {
self.trigger('selection:update', {
data: initialData
});
}); |
@kevin-brown thanks |
I found this answer on Stackoverflow by Kevin useful |
@MrHubble I can get the |
select2 4.0rc2
I need method to set data after init, like ajax search result with properties from processResults. Can you help me?
Example:
or
The text was updated successfully, but these errors were encountered: