Dynamic popup menu

J’ai besoin de cette commande et consulté https://doc.4d.com/4Dv18/4D/18/Dynamic-pop-up-menu.301-4505524.fe.htmlla page de Dynamic popup menu>.
L’exemple collé dans ma base ne fonctionne pas…

J’ai donc fouillé la doc, cherché des exemples et fini par en trouver un qui fonctionne.
<code 4D>
vRef:=Create menu
APPEND MENU ITEM(vRef;“MaLigne 1;MaLigne 2”)
SET MENU ITEM PARAMETER(vRef;1;“1”)
SET MENU ITEM PARAMETER(vRef;2;“2”)
$Position:=Num(Dynamic pop up menu(vRef))

</code 4D>
Bizarre, SET MENU ITEM PARAMETER ne figure pas dans l’exemple qui ne fonctionne pas ; y a-t-il un lien ?
On dirait, car si je l’enlève dans l’exemple qui fonctionne, il ne fonctionne plus.
A quoi sert SET MENU ITEM PARAMETER ? dans l’exemple de la https://doc.4d.com/4Dv18/4D/18/SET-MENU-ITEM-PARAMETER.301-4505502.fe.htmlpage de SET MENU ITEM PARAMETER>, je comprends qu’on récupère le numéro de la fenêtre plutôt que le libellé, sinon à quoi bon…

J’aime bien quand ça fonctionne mais j’aime encore mieux quand je comprends !

Bernard,
(apologies for writing in English)

I use dynamic menus a lot. I create them on-the-fly and use them instead of popup lists in almost all cases.

The example on the page (https://doc.4d.com/4Dv18/4D/18/Dynamic-pop-up-menu.301-4505524.fe.html) is a bit - ambitious. Perhaps not the best example.

To your question, " What is the SET MENU ITEM PARAMETER for? " it allows you set a string (255 chars) that is returned by the Dynamic Popup Menu command if that menu item is selected. It can be any kind of string it seems.

Code :
$menu:=Create menu
$subMenu:=Create menu

APPEND MENU ITEM($subMenu;"MyLine 2")
SET MENU ITEM PARAMETER($subMenu;-1;"1")

APPEND MENU ITEM($subMenu;"MyLine 3")
SET MENU ITEM PARAMETER($subMenu;-1;"go_do_something")

APPEND MENU ITEM($menu;"MyLine 1";$subMenu)
RELEASE MENU($subMenu)

$result:=Dynamic pop up menu($menu)
RELEASE MENU($menu)

If ($result#"")
	//  do something
End if 

This is a typical pattern I use for popups on list boxes or called from buttons on a form. It is very fast so there’s no reason to persist the menu. Notice I use RELEASE MENU when done with one. And the $submenu is released as soon as it’s attached. This prevent memory leaks.

I do this even for simple lists:
Code :
$menu:=Create menu
$subMenu:=Create menu

APPEND MENU ITEM($subMenu;"James")
SET MENU ITEM PARAMETER($subMenu;-1;"James")

APPEND MENU ITEM($subMenu;"Mike")
SET MENU ITEM PARAMETER($subMenu;-1;"Mike")

APPEND MENU ITEM($menu;"4D Developer...";$subMenu)
RELEASE MENU($subMenu)

APPEND MENU ITEM($menu;"---")

APPEND MENU ITEM($menu;"Some action")
SET MENU ITEM PARAMETER($menu;-1;"action")

$result:=Dynamic pop up menu($menu)
RELEASE MENU($menu)

If ($result#"")
	ALERT($result)
End if 

This is a good approach when you have a long list of items and want to break it down by alphabetical group.

Code :
$temp_c:=New collection

$temp_c.push(New object("name";"John Smith";"id";"12k4kmmen"))
$temp_c.push(New object("name";"Sally Noes";"id";"asdlkjwernm"))
$temp_c.push(New object("name";"Robert Bruce";"id";"58r845f2"))
$temp_c.push(New object("name";"Wilma Anderson";"id";"bbk03j7s"))
$temp_c.push(New object("name";"Susie Boxer";"id";"asd87987234n"))
$temp_c.push(New object("name";"Randal Graham";"id";"8s9idf78432"))

$temp_c:=$temp_c.orderBy("name asc")

$menu:=Create menu
$subMenu:=""
$letter:=""

For each ($person;$temp_c)
	
	If ($person.name[1]#$letter)
		If ($letter#"")  // append to the menu
			APPEND MENU ITEM($menu;Uppercase($letter);$subMenu)
			RELEASE MENU($subMenu)
		End if 
		
		$subMenu:=Create menu
		$letter:=$person.name[1]
		
	End if 
	
	APPEND MENU ITEM($subMenu;$person.name)
	SET MENU ITEM PARAMETER($subMenu;-1;$person.id)
	
End for each 

APPEND MENU ITEM($menu;Uppercase($letter);$subMenu)
RELEASE MENU($subMenu)

$result:=Dynamic pop up menu($menu)
RELEASE MENU($menu)

ALERT($result)

In this example I used a collection but that could also be an entity selection and return the entity id.

I must confess I did not know you build the menu like in your example. I thought that was only an option for POPUP MENU. It doesn’t collapse, though.

Just some ideas. Hope they help.

: Bernard ESCAICH

A quoi sert SET MENU ITEM PARAMETER ?
Ça sert à dissocier libellé et valeur retournée, par ex. un menu affiche “A, B, C” et retourne “1, 2, 3”.

À un moment j’ai cru comprendre qu’il fallait passer à Dynamic popup menu pour cause d’obsolescence programmée à venir de Popup menu. Je me suis forcé à n’utiliser que la “moderne”. Maintenant j’utilise celle qui me convient.

Thanks Kirk for this long answer, English is not an issue !

: Kirk BROOKS

To your question, " What is the SET MENU ITEM PARAMETER for? " it
allows you set a string (255 chars) that is returned by the Dynamic
Popup Menu command if that menu item is selected. It can be any kind
of string it seems.

In basic case, SET MENU ITEM PARAMETER does not seem very useful.

But, when I build yesterday an example that works, I though for a real useful example :

  • create a menu for users in French, English,… with xliff
  • return a keyword usable in the method
    I found that used in 4D Pop.
: Kirk BROOKS

$temp_c:=New collection

$temp_c.push(New object(“name”;“John Smith”;“id”;“12k4kmmen”))
$temp_c.push(New object(“name”;“Sally Noes”;“id”;“asdlkjwernm”))

$temp_c:=$temp_c.orderBy(“name asc”)

$menu:=Create menu
$subMenu:=""
$letter:=""

For each ($person;$temp_c)

If ($person.name[1]#$letter)
If ($letter#"") // append to the menu
APPEND MENU ITEM($menu;Uppercase($letter);$subMenu)
RELEASE MENU($subMenu)
End if

Nice solution designed for the future !
I combine those two use solutions.