Hi,
Here's a fiddle to reproduce the issue, if it's an issue of course. I have doubts that I am doing something wrong here. But anyways...
Here's the problem:
menu items in dropdown change dynamically and we need to select a new value..dropdown("set selected", "new value") does not kick inMutation observers (which refresh the selector cache) are asynchronous event callbacks which must wait for the browser to fire mutation events. If you need to use a value immediately you will manually have to call
$('.ui.dropdown').dropdown('refresh');
Understood. Thanks @jlukic
seems like an undocumented feature
Yes, there's a few features like that which are just designed to allow developers to be lazy and everything can 'just work'.
The refresh-method doesn't seem to work if the source element was a select element:
http://jsfiddle.net/nfsfb87y/1/
Couldn't find any documentation on set selected.
If I use a timeout after I append options to the select box it works.
I think the request method sould maybe return a promise such as
$("[name=productIds]").dropdown("refresh").then(function() {
$("[name=productIds]").dropdown("set selected", "3");
});
I looked at the refreshSelectors method #4486 and to me it looks like a select element wouldn't be refreshed, is that possible?
I had the same problema as achimkoellner.
The "set selected" didn't work after dynamically fill a select element.
I tried the "refresh" before the "set selected" method but still didn't work.
The only solution was to use a setTimeout to call "set selected" async.
for (var id in championships) {
var $option = $("<option value=\"" + id + "\">" + championships[id].name + "</option>");
$select.append($option);
}
$select.dropdown('refresh');
if (defaultValueId)
setTimeout(function () {
$select.dropdown('set selected', defaultValueId);
}, 1);
same problem here with the
+1 for @sarbona workaround
+1 for @sarbona workaround.
Works like a charm
+1 for @sarbona . worked for me! :)
I had the same problem. I also couldn't dynamically add options with value="" (empty string).
+1 @sarbona's workaround
+1 @sarbona Isn't this a bug?
+1 @sarbona
Cool.. +1 @sarbona
+1 @sarbona saved my day
+1 @sarbona thanks, bro!
Edit: Just put the code inside your ajax callback, if your doing so.
This would be good to document, it's one of the many "gotchas" I've found when attempting to use semantic UI with angularjs.
Is setTimeout() still the only way to get this to work? Whenever I see setTimeout() as the accepted solution I want to jump off a cliff.
@Jameron no, if you look at earlier posts you'll find that you can use $(".ui.dropdown").dropdown("refresh");
What about if I just dynamically added a list item?
$('#entity').dropdown('refresh');
$('#entity').dropdown('set selected', newEntity[0].id);
The set selected is failing. If I run it in console it works, seems like it needs a timeout or promise to stop async?
Then it's setTimeout(), i guess. I myself have a bunch in the code already. Nowhere to run
Instead of setTimeout, you can also put your own mutation observer on the select element you're using as a dropdown like so: http://jsfiddle.net/XfxJu/2/. The link shows how mutation observer works, it's not a complete example.
I am also facing same issue as if I am using remote content in dropdown list dynamically in that case
$('.remote.ui.dropdown').dropdown({
apiSettings: {
url: '/api/call'
},
filterRemoteData: true
});
$('.remote.ui.dropdown').dropdown('refresh');
$('.remote.ui.dropdown').dropdown('set selected',"value");
is not working
Please help me to understand what I am doing wrong
I want to prepopulate the value when form is loading and also to give the option to the user to select the option from given list which is coming remotely using apiSetting parameters.
$('.remote.ui.dropdown').dropdown({
apiSettings: {
url: '/api/call'
},
filterRemoteData: true
});
$('.remote.ui.dropdown').dropdown('refresh');
$('.remote.ui.dropdown').dropdown('set selected',2);
+1 for @sarbona ! Thanks a lot !
I use Aurelia framework. Both refresh and timeout did not work for me due to async operations. I had to wrap my code with a Micro Task Queue to make it work. It allowed the 'set selected' method to run right after getting ajax data.
Most helpful comment
I had the same problema as achimkoellner.
The "set selected" didn't work after dynamically fill a select element.
I tried the "refresh" before the "set selected" method but still didn't work.
The only solution was to use a setTimeout to call "set selected" async.
for (var id in championships) {
var $option = $("<option value=\"" + id + "\">" + championships[id].name + "</option>");
$select.append($option);
}
$select.dropdown('refresh');
if (defaultValueId)
setTimeout(function () {
$select.dropdown('set selected', defaultValueId);
}, 1);