1

This question is about extending the solution provided here. How do I allow the user to change x outside the TogglerGrid function so that the TogglerBar shows the new values of x? I've set it up inside a Manipulate with a row of buttons to indicate how I want to use it. I need to retain the multi-row feature. Also here, and here.

Imgur

DynamicModule[{TogglerGrid, x, x0, g0, gr},

TogglerGrid[Dynamic[var_], list_List, n_] := DynamicModule[{k, this, len, set, ref, temp},

var = Sort[var]; ref = Evaluate@list; temp = {#} & /@ ref;

set = TogglerBar[ Dynamic[temp[[#]], Function[{$x}, temp[[#]] = $x; var = Sort@(Join @@ temp)]], {list[[#]]}] & /@ Range@Length@list;

Grid[Partition[set, n, n, {1, 1}, {}], Alignment -> {Center, Center}, Spacings -> {0, 0}] ];

x = x0 = Range[10];

g0 = Graphics[{Black, Thick, Line[10 {{-1, -1}, {-1, +1}, {+1, +1}, {+1, -1}, {-1, -1}}]}]; gr = { Graphics[Circle[]], Graphics[Disk[{1, 0}], 1], Graphics[{Red, Circle[{5, 6}, 1]}], Graphics[{Blue, Circle[{-5, -3}, 5]}], Graphics[Disk[{1, 4}], 3], Graphics[{Green, Circle[{5, -6}, 4]}], Graphics[{Yellow, Circle[{-5, -3}, 4]}], Graphics[Disk[{8, -4}], 2], Graphics[{Orange, Circle[{-7, 6}, 5]}], Graphics[{Blue, Disk[{-8, -3}, 4]}] };


Manipulate[
 Column[{
   Row[{"x = ", Dynamic[x]}],
   Show[{gr[[x]], g0}, PlotRange -> 10.1 {{-1, +1}, {-1, +1}}]
   }],

Column[{ Row[{Button["None", x = {}], " ", Button["All", x = x0], " ", Button["These", x = {1, 7}]}], TogglerGrid[Dynamic[x], x0, 5] }] ] ]

creidhne
  • 5,055
  • 4
  • 20
  • 28
user48879
  • 91
  • 5

2 Answers2

1

The following fixes the issue by rewriting the TogglerBar grid function. Dynamic@ solves the issue of making n (Columns per row) work

DynamicModule[{TogglerTableFnc01, x, x0, g0, gr, n},
TogglerTableFnc01[Dynamic[varArg_], listArg_List, 

Dynamic[colsArg_]] := Module[{lenH, rH, rowsH, extraH, thisH, thatH, theseH, passH},

lenH = Length[listArg]; rowsH = IntegerPart[lenH/colsArg // N]; extraH = lenH - colsArg*rowsH;

thisH = Table[TogglerBar[Dynamic[varArg], Range[rH, rH]], {rH, lenH}]; If[extraH == 0, thatH = thisH, thatH = Join[thisH, ConstantArray[" ", colsArg - extraH]];]; theseH = Partition[thatH, colsArg]; passH = Grid[theseH];

passH ];

x = x0 = Range[10];

g0 = Graphics[{Black, Thick, 
Line[10 {{-1, -1}, {-1, +1}, {+1, +1}, {+1, -1}, {-1, -1}}]}];

gr = { Graphics[Circle[]], Graphics[Disk[{1, 0}, 1]], Graphics[{Red, Circle[{5, 6}, 1]}], Graphics[{Blue, Circle[{-5, -3}, 5]}], Graphics[Disk[{1, 4}, 3]], Graphics[{Green, Circle[{5, -6}, 4]}], Graphics[{Yellow, Circle[{-5, -3}, 4]}], Graphics[Disk[{8, -4}, 2]], Graphics[{Orange, Circle[{-7, 6}, 5]}], Graphics[{Blue, Disk[{-8, -3}, 4]}] };

n = 4;

Manipulate[ Column[{ Row[{"x = ", Dynamic[x]}], Show[{gr[[x]], g0}, PlotRange -> 10.1 {{-1, +1}, {-1, +1}}] }],

Column[{ Row[{"Columns per row: ", SetterBar[Dynamic[n], Range[10]]}], Row[{Button["None", x = {}], " ", Button["All", x = x0], " ", Button["These", x = {1, 7}]}], Dynamic@TogglerTableFnc01[Dynamic[x], x0, Dynamic[n]]}] ] ]

user48879
  • 91
  • 5
1

We can get the desired result without need for custom controls as follows:

We can use the option Appearance -> "Horizontal" -> {Automatic, n} to have n columns per row in TogglerBar. To get the value of n dynamically updated we wrap TogglerBar[..] with Dynamic:

Manipulate[Column[{PromptForm["x", x],
   Show[{gr[[x]], g0}, PlotRange -> 10.1 {{-1, +1}, {-1, +1}}]}],
 "Columns per row: ",
 {{n, 4, ""}, x0, SetterBar},
 Delimiter,
 {{x, x0, ""}, {{} -> "None", x0 -> "All", {1, 7} -> "These"}, Appearance -> "Button"},
 Delimiter,
 {{x, x0, ""}, 
    Dynamic @ TogglerBar[#, x0, Appearance -> "Horizontal" -> {Automatic, n}] &}]

enter image description here

where x0, g0 and gr are defined in OP above.

kglr
  • 394,356
  • 18
  • 477
  • 896