0

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?

martin
  • 8,678
  • 4
  • 23
  • 70
  • probably related: 33452, 72796 – Kuba Mar 08 '17 at 18:52
  • @Kuba updated now – martin Mar 08 '17 at 20:50
  • 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. – Kuba Mar 09 '17 at 07:20
  • @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! – martin Mar 09 '17 at 18:43

1 Answers1

2

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.

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • 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. – martin Mar 09 '17 at 18:44