8

Background: In a module containing an outer / inner manipulate I select the key of data with a dropdownlist in the outer manipulate. Data is read and displayed in the inner manipulate for edit purposes. The outer manipulate contains buttons, one of which is a save button. I want to get the edited data back at that level so that it can be saved.

Consider the following simplified example:

  Manipulate[
   Column[{j,
   Manipulate[k, {k, 1, 5, 1}],
    Button["Check", Print[k]]
   }],
  {j, 1, 10, 1}]

the question then becomes: "What is the best way to get the variable k scoped to the first / outer manipulate" ?

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
nilo de roock
  • 9,657
  • 3
  • 35
  • 77

2 Answers2

7

How about this:

DynamicModule[{k},
  Manipulate[
   Column[{j,
     Manipulate[k, {k, 1, 5, 1}, LocalizeVariables -> False],
     Button["Check", Print[k]]
   }],
   {j, 1, 10, 1}]
]
Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
  • Just out of curiosity, is there a particular reason why you put the DynamicModule on the outside of the outer Manipulate? – Heike Apr 18 '12 at 16:22
  • @Heike It is not strictly necessary, but in this way, we don't generate a new symbol/variable every time when external Manipulate recomputes its body. This is the same logic as when we use external Module to create persistent local variables, as for example I discussed here. – Leonid Shifrin Apr 18 '12 at 17:02
5

Using LocalizeVariables -> False as in Leonid's answer is probably the easiest, but the downside is that it makes all controls in the inner Manipulate visible to the outer Manipulate and beyond. If you have multiple controls in the inner Manipulate but you want the outer Manipulate to only see some of them to be visible to the outer Manipulate you could also do something like

Manipulate[
 DynamicModule[{k1}, 
  Column[{j, Manipulate[k1 = k, {k, 1, 5, 1}], 
    Button["Check", Print[k1]]}]], {j, 1, 10, 1}]
Heike
  • 35,858
  • 3
  • 108
  • 157
  • Heike, I have accepted Leonid's answer because what you refer to as a downside works out rather advantageous in my code. – nilo de roock Apr 18 '12 at 18:21