TransWikia.com

TogglerBar linked to Buttons that override input list

Mathematica Asked by kmutiny on January 4, 2021

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]
   }]
 ]
]

One Answer

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]]}]
  ]
 ]

Answered by kmutiny on January 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP