Search code examples
javascriptgoogle-earth-engine

setValue not workin on dependant Earth Engine ui.Select widget


I am working on an Earth Engine application where I have 2 widgets of type ui.Select that are dependent on each other (bidirectionally dependent). That is, when I make a change in one of them, it changes the list in the other and vice versa.

The behavor is correct, except that when I want to reselect the option that was before the change, it does not work. Due to the items generator script that populate the widgets is allways sure that the previous selection is in the new (modified) items list.

Here are the two widgets implementations

var select_date_init = ui.Select({
  items: Object.keys(dates_init),
  onChange:function(key) {
    var selected_end = select_date_end.getValue();
    var selected_init_end_date = dates_init[key][1];
    var new_list = Object.keys(get_dates_list(selected_init_end_date,''));
    select_date_end.items().reset(new_list);
    //select_date_end.items = new_list
    
    if(selected_end !== null){
      select_date_end.setValue(selected_end,false)
    }else{
      select_date_end.setValue(null,false)
    }
  },
  placeholder: 'Seleccionar semestre 1...'
});

var select_date_end = ui.Select({
  items: Object.keys(dates_end),
  onChange: function(key) {
    var selected_init = select_date_init.getValue();
    var selected_end_init_date = dates_init[key][0];
    var new_list = Object.keys(get_dates_list(system_date_init,selected_end_init_date));
    select_date_init.items().reset(new_list);
    //select_date_init.items = new_list;
    
    if(selected_init !== null){
      select_date_init.setValue(selected_init,false)
    }else{
      select_date_init.setValue(null,false)
    }
  },
  placeholder: 'Seleccionar semestre 2...'
});

The problem seems to be on the setValue() part of the script. The items of the two widgets are correctly changed, but it allways show the 'Seleccionar semestre 1...' or 'Seleccionar semestre 2...' default text after the change action.

The action I need is:

First step:
Select from 1 -> this changes the list on 2 (originally empty so no pre-selection needed)
Second step:
Select from 2 -> this changes the list on 1 (but keep the selected option in 1)
If the user wants:
Change selection from 1 (or 2) -> update the other list -> keep pre-selected options

Here is the link to the full working script https://code.earthengine.google.com/b251b24ffbc37775e4e5eede8dcbd11f


Solution

  • The onChange function of your button #2 is resetting your button #1 every time. You only need to uncomment lines 209-216 of your code, so the onChange function of your variable var select_date_end looks like this:

    var select_date_end = ui.Select({
      items: Object.keys(dates_end),
      onChange: function(key) {
        var selected_init = select_date_init.getValue();
        var selected_end_init_date = dates_init[key][0];
        var new_list = Object.keys(get_dates_list(system_date_init,selected_end_init_date));
      },
      placeholder: 'Seleccionar semestre 2...'
    });
    

    See code: https://code.earthengine.google.com/9e2d4f13e9bfa57e1df3673a6d82684e