Updating dropdown selection on menu options change

758 views
Skip to first unread message

Andrew Stratton

unread,
Jun 28, 2016, 5:17:42 AM6/28/16
to Blockly
Hi Everyone
  After many false starts, I almost have a 'linked' update working between one block and another.  The issue I now have (left) is updating the visible dropdown selection on a change of the original block.  An example is likely to make more sense :)

  e.g. here there are two Vitrine blocks (for museum displays) called Entrance and Exit. Each can have many 'labels' (buttons) that allow the visitors to view a different Vitrine (display). IN this case, the Entrance Display would show a button linking to the Exit, and vica versa. The Viteines can also have more to display - e.g. text, images, etc., but that is outside the scope of the issue.

    As the Vitrines are updated, their text is shown in the labels automatically, so adding another Vitrine, say called 'Excavations' already will add a new (unselected) 'Excavations' option tot he Label To dropdown - I have this working fine (ish).

  I have also intercepted the change event for Vitrines and then updated any changes to their name, so if 'Entrance is changed to 'Welcome', the drop down updates - but the Label to the visible selection is UNCHANGED. e.g.



This is the problem - and choosing the new option from the drop down doesn't update the display - a different option has to be selected and then chosen.

Is there a (simple) way to just 'redraw' or layout again the Label to  block so that the changed menu option is immediately visible?

Best wishes
  Andy Stratton

Adrian Buzea

unread,
Jun 28, 2016, 9:01:58 AM6/28/16
to Blockly
You probably update the menuGenerator_ of the FieldDropdown right ? I faced a very similar problem . When you update the menuGenerator_ the selected value remains the same, and if you look at the source code for the FieldDropdown the text of the element is changed within the setValue function with a call to the setText function. But the setValue function just returns if the new value is equal to the previous one, so you will have to call setText yourself in order to change the text.

Code fragment taken from the source code:

var options = this.getOptions_();
for (var x = 0; x < options.length; x++) {
 
if (options[x][1] == newValue) {
 
this.setText(options[x][0]);
 
return;
 
}
}
This finds the option corresponding to the selected value and sets the correct text. 

Andrew Stratton

unread,
Jun 28, 2016, 3:44:31 PM6/28/16
to Blockly
Perfect - thank you - Andy
Reply all
Reply to author
Forward
0 new messages