0
$\begingroup$

What is the simplest way I can add a reset / refresh option to dynamic module / manipulate? This seems like a bit of a hack, but it gives approxiamately desired output:

Manipulate[
   Grid[Partition[DynamicModule[{x = a}, 
   Button[Text[Style[#, 24]], x = Mod[x + 1/2, 1], ImageSize -> {60, 60}, 
   Background -> Dynamic[Hue[1, 2 x, 1]]]] & /@ Range@100, 10], 
   Spacings -> {0, 0}], Invisible@Control[{a, {10^(-6), 0}}], 
   Button["Reset", If[a == 0, a = 10^(-6), a = 0]]]

What is the best way to achieve this without the workaround?

$\endgroup$
  • $\begingroup$ probably related: 33452, 72796 $\endgroup$ – Kuba Mar 8 '17 at 18:52
  • $\begingroup$ @Kuba updated now $\endgroup$ – martin Mar 8 '17 at 20:50
  • $\begingroup$ Thanks for the update but it is till not clear what do you mean by reset here. There are 100 DynamicModules and one Manipulate, I suppose you refer to the outer Manipulate but we should not quess. Reseting particular DynamicMOdules is a reasonable question too. $\endgroup$ – Kuba Mar 9 '17 at 7:20
  • $\begingroup$ @Kuba Well, I wrapped the DynamicModule in Manipulate - not sure whether that's necessary, but keeps it self-contained. I supopose I want to reset the x values, but I am not certain. I want to reset the cells to white at any rate. I had no idea there were so many DynamicModules BTW! $\endgroup$ – martin Mar 9 '17 at 18:43
2
$\begingroup$

So it seems you just want to reset the initial values, which means your life is actually relatively easy. The trick is to just wrap everything in a single DynamicModule and the system will take care of the rest. This takes a small amount of rewriting, but it's really quite quick and easy:

DynamicModule[{
  vals = AssociationMap[False &, Range[100]],
  entries = 100,
  partitioning = 10
  },
 Panel@Column@{
    Button["Reset",
     vals = AssociationMap[False &, Range[entries]]],
    Deploy@
     Grid[
      Partition[
       Table[
        With[{i = i},
         Item[
          EventHandler[
           Pane[i, {35, 35}, Alignment -> Center],
           "MouseClicked" :> (vals[i] = Not@vals[i])
           ],
          Background ->
           Dynamic@If[vals[i], Red, White]
          ]
         ],
        {i, Range[entries]}
        ],
       UpTo@partitioning
       ],
      Dividers -> All
      ]
    }
 ]

Since it's all one DynamicModule the front-end will track the variables for you, and all you have to do is reset the initial state in a Button by a simple assignment.

$\endgroup$
  • $\begingroup$ this is great! Many thanks :) Much better that my attempt - will have to look into your construction when I get a moment. Will give it another 24 hrs before I mark the accept. $\endgroup$ – martin Mar 9 '17 at 18:44

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.