1
$\begingroup$

I have some trouble evolving from this

Manipulate[
 Grid[{{"date: ", date}, {"dates: ", dates}}],

 {{dates, {1 -> "A", 2 -> "B", 3 -> "C"}}, None},
 {{date, 1}, dates, PopupMenu},
 Button["Add", 
  AppendTo[dates, 
   dates[[-1, 1]] + 1 -> 
    FromCharacterCode[ToCharacterCode[dates[[-1, 2]]] + 1]]]
 ]

Mathematica graphics

I whish to attach an "handler" so that when i change the PopupMenu I can do something. I tried this

Manipulate[
 If[updated,
  Print["updated"];
  updated = False
  ];
 Grid[{{"date: ", date}, {"dates: ", dates}}],

 {{dates, {1 -> "A", 2 -> "B", 3 -> "C"}}, None},
 {{updated, False}, None},

 {{date, 1}, dates, 
  PopupMenu[Append[#, (date = #; updated = True) &], #2[[1]]] &},
 Button["Add", 
  AppendTo[dates, 
   dates[[-1, 1]] + 1 -> 
    FromCharacterCode[ToCharacterCode[dates[[-1, 2]]] + 1]]]
 ]

but the PopupMenu is not displayed.

Mathematica graphics

The following code display the PopupMenu but it's not linked to the variable.

Manipulate[
 If[updated,
  Print["updated"];
  updated = False
  ];
 Grid[{{"date: ", date}, {"dates: ", dates}}],

 {{dates, {1 -> "A", 2 -> "B", 3 -> "C"}}, None},
 {{updated, False}, None},

 {{date, 1}, {1 -> "A", 2 -> "B", 3 -> "C"}, 
  PopupMenu[Append[#, (date = #; updated = True) &], #2[[1]]] &},
 Button["Add", 
  AppendTo[dates, 
   dates[[-1, 1]] + 1 -> 
    FromCharacterCode[ToCharacterCode[dates[[-1, 2]]] + 1]]]
 ]

If possible I whish to use Manipulate and declare the local variable date and consume it at the same place.

$\endgroup$
1
$\begingroup$

Probably some "magic" happens transparently when the items list is dynamic so the first

{{date, 1}, dates, PopupMenu}

is not equivalent to

{{date, 1}, dates, PopupMenu[#, #2[[1]]]&}

but to

{{date, 1}, dates, Dynamic@PopupMenu[#, #2[[1,1]]]&}

when #2[[1]] is a _Dynamic as in this case.

Si I found this way:

Manipulate[
 If[updated,
  Print["updated"];
  updated = False
  ];
 Grid[{{"date: ", date}, {"dates: ", dates}}],

 {{dates, {1 -> "A", 2 -> "B", 3 -> "C"}}, None},
 {{updated, False}, None},

 {{date, 1}, dates, 
  Dynamic@PopupMenu[
     Append[#, (date = #; updated = True) &], #2[[1, 1]]] &},
 Button["Add", 
  AppendTo[dates, 
   dates[[-1, 1]] + 1 -> 
    FromCharacterCode[ToCharacterCode[dates[[-1, 2]]] + 1]]]
 ]

Mathematica graphics

Maybe obvious, and/or already discussed, but I haven't found anything related. Hope someone find it useful.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.