Update options of dropdown menu

I created the following menu in r184:

let folder = gui.addFolder("A Folder");

const props = {
    "text field": "",
    "dropdown": 0,
    "slider": 0.5
};

let textfield = folder.add(props,"text field");
let dropdown = folder.add(props,"dropdown",[0]).onChange(val => { selectNumber(val) });
let slider = folder.add(props,"slider",0,1,0.1);

Later I want to add more options to the dropdown (e.g. [0,1,2]) but keep it as menu item 2.

According to the docs for the options function (link), you should be able to do that with the following, as long as the original controller is already a dropdown:

folder.add(props,"dropdown",[0,1,2]);

But this adds a forth menu item called “dropdown” at the end (with the options 0, 1 and 2). The originals still only has 0.

I also tried the following, which updated the controller but also moved it to the end:

dropdown.options([0,1,2]);

How do I update the controller without moving it or creating a duplicate?

The controller moving to the end is a bug/feature of lil-gui.
Try the OG dat.gui

Managed to do it with lil-gui by inserting the options directly in its internal structure. I’m not sure whether there are some side effects, especially lost event listeners and other hooks.

function addOptions( obj, values ) {
    values.forEach( value => {
        obj.$select.add( new Option(value) );
        obj._values.push( value );
    }); // forEach
} // addOptions

addOptions( dropdown, [4, 5, 6, 7, 8] );

Demo: https://editor.sbcode.net/df3e7dd111c33e259c24dc1367672d8a436d4115

No idea what version is included in Three.js r184 but the issue about it on github says that this shouldn’t happen. Does this mean that it’s a new bug or is anything missing?
Do you have to add onChange every time you change the options?

I thought dat.gui is outdated and shouldn’t be used anymore?

Thanks! I did a little bit of testing and onChange still works as expected.

A short explanation for the code:

add adds the actual list item to the dropdown.
push adds its value.

E.g. If you only add the number 2 but don’t push it, then “2” will still be in the dropdown but if you select it, then it’ll turn into “undefined” and the onChange function will also return “undefined”.
The order you add items in is important! If you add “1” and “2” but push “2” and “1”, then “1” will turn into “2” (and vice versa) if you select it. You also can’t push an array, push([1,2]) turns into “1,2” in the list and onChange.

You can also remove an element from the dropdown with:

dropdown.$select.remove(0);
//dropdown._values.shift(); //removes the first element
dropdown._values.splice(0,1); //removes one element starting at index 0

The element isn’t in the list anymore if I click on the dropdown but it still appears as the default selected element while the dropdown is closed, until I select another one. $select seems to be a HTMLSelectElement and dropdown.$select.value correctly changes from “0” to “1” if “0” is removed (actual value, not index!) but I haven’t found a way to remove the last remnants of this 0th element yet (=change this wrong “label”). Any idea?

Threejs includes the older version lil-gui v0.17

So here is an example using latest lil-gui@0.21

If you need to use the old version, you can put any value when you remove the currently selected value:

dropdown.$select.selectedIndex = -1;
dropdown.$display.innerHTML = 'hoho';

But, yet, it is much better to use the new library, as @seanwasere pointed out.

@PavelBoytchev
Thanks, dropdown.$display.innerHTML = dropdown.$select.valuedid it!
No need to changed the selectedIndex, it automatically selects another one, as long as there’s still one element left (if(index<dropdown._values.length && dropdown._values.length>1)). At least, if I delete the very first one, it automatically selects the second=new first element.

@seanwasere
Ah, that explains it, thanks!
I currently can’t update anything, so I have to use the HTML version. Do you know if there are any plans to update lil-gui to the latest version in a future Three.js version?