TransWikia.com

Resetting ui.Select in Google Earth Engine

Geographic Information Systems Asked on December 18, 2021

Below I have shared the link.
The issue – ui.Select remebers previous selections – even after being reset.
Consequentially, if the user picks the same value twice – onChange is not activated.
I am working in code editor with JavaScript

https://code.earthengine.google.com/4341dde5f526ef48f2eae20be42eb2b0

2 Answers

I found an answer by Noel Gorelick from developers forum, just add a line to onclick function, select.setValue(null, false) //JUST ADD THIS LINE :

//Try the following sequence:
//Select MTV (will print Registerd a change); then click reset dropdown (select will reset to placeholder); then select MTV again
//And nothing will happen; whereas if u select PEK or ZRH, it will print (registered a change)


var places = {
  MTV: [-122.0849, 37.3887],
  PEK: [116.4056, 39.9097],
  ZRH: [8.536, 47.376]
};

var select = ui.Select({
  items: Object.keys(places),
  placeholder: 'Choose a location...',
  onChange: function(key) {
   Map.setCenter(places[key][0], places[key][1]);
   print('Registered a change');
  }
});

print(select);

var Reset=ui.Button({
 label: "Reset Dropdown",
 onClick: function () {
    select.setValue(null, false) //JUST ADD THIS LINE
 }
});

print(Reset);

Answered by Mehmet C ARSLANOGLU on December 18, 2021

ui.Select does not change its value when a placeholder text is changed. Therefore, within Reset button while resetting the items (according to me unnecessary) and changing the placeholder text to "Choose a location2..." of the select button, you are not changing the value of select, but merely changing the text that is displayed on the select button. This is why a change is not registered when you click on the same location again. You can verify this by printing select.getValue() after you click on Reset.

I think you want to set the location value to default (0,0) when you click on reset. Then, if you again click on the original location, a change will be registered. To achieve that, you can change your reset function as follows:

var Reset=ui.Button({
  label: "Reset Dropdown",
  onClick: function () {
    select.setValue("Choose a location...")
    // select.items().reset();
    // select.items().reset(Object.keys(places));
    // select.setPlaceholder('Choose a location2...');
    }
    
});

And explicitly set the default location within the select button.

Link to complete code.

Answered by kkrao on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP