Back action in MenuTree

Is it possible to write a script in back action in a MenuTree?
I wrote a script that write the selected path in a custom property, but I would like that if the user press the back button the last item selected is removed from the path. I wrote my script in event onItemClicked but it seems that the back button doesn't react to this script and I cannot find any property in MenuTree that changes when back button is pressed.
Any idea?

Yes, in a roundabout way.

You need to actually supply a dummy item within the items list for the item, and then you need to use the onItemClicked script to invoke the back function by checking to see if your "back" item was the one clicked.

1 Like

Thanks for help, but I have still problems with this solution. On the top of the menu tree the back arrow is still showing, I cannot be sure that users don't press this arrow instead of the dummy item.

Add this in the advanced stylesheet:

.menu-back-action {
    display: none !important;

Thanks! I didn't think to hide it using styles, but it works perfect.

Keep in mind that hiding things with css is only client side and people can "unhide" them with the dev tools integrated to their browsers.

I don't really get how this navigateBack works in a menuTree. I'm adding a dummy item as the first element in the list, but I don't understand how to write the right code onItemClicked. So far I wrote this script that it is not working:

path = event["path"]
if len(path)>1:
	if path[1]==0:
		a = path.pop()
self.custom.path = path

Well actually... I don't think navigateBack does exactly the same thing as the back button.

Let's take a few steps back: What's your end goal here ?

My idea is to have 3 levels in the menuTree: department -> machine -> position (possible path [0,0,1]). When I choose position I change background for the selected position, for user to know which position is selected. But it is possible that the user regrets and wants to choose a position in another machine, so I need a back button to choose another machine (this was working well with the back button, problem here was that if I chose same machine the old selected position was remaining selected).
A half solution is to select for the dummy item the option resetOnClick, but it is sending me back to department not to machine level.

How do you know you're in the "position" menu ? Do you store a path ?
And how do you apply the background color ? Is the color the same for both "department" and "machine" menus ?

yes in my last script yo can see that in last line I'm saving the event path in a custom property. I have a change script on this property to change background:

if length>=3:
	department = path[0].value
	machine = path[1].value
	position = path[2].value
	items = self.props.items[department].items[machine].items
	for i,item in enumerate(items):
		if i>0:
			if i == position:
				item["style"]["classes"] = "foqus/item_selected"
				item["style"]["classes"] = ""

Machine and department have the same background, as they open a new list of items I don't need to distinguish them.
My idea was to remove from the path last element in list if I was pressing the button back so I could control the event and set all position with same background