Background
Background
Posting this for posterity.
My brief search on community didn’t turn up anything related to this, though I’m not convinced it’s a unique problem, please comment if you find additional examples, thanks!
I’ve got a dataset where each record has a value and a checkbox. The checkbox determines whether a function will be applied to that record’s value when a button is clicked. After a rigamarole through Dynamic syntax, I’ve come up with a solution. Which of course, is simpler than anything I’ve tried so far!
There are two key features to making this work.
My brief search on community didn’t turn up anything related to this, though I’m not convinced it’s a unique problem, please comment if you find additional examples, thanks!
I’ve got a dataset where each record has a value and a checkbox. The checkbox determines whether a function will be applied to that record’s value when a button is clicked. After a rigamarole through Dynamic syntax, I’ve come up with a solution. Which of course, is simpler than anything I’ve tried so far!
There are two key features to making this work.
1
.Ensure the Dynamic wrapped symbol given to Checkbox is unique
1
.1
.With[{sym=Unique["$cbox"]},Checkbox[Dynamic[sym]]]
2
.When pattern matching the Checkbox remember to include Dynamic
2
.1
.Position[data,HoldPattern[Checkbox[Dynamic[_?TrueQ]]]]
+1 | Add one to selected records | |
CS | Set selected records to 0 | |
CA | Set all records to 0 |
Out[]=
Code
Code
In[]:=
DynamicModule[{data=AssociationThread[{"Value","Selector"}->{#,(With[{sym=Unique["$cbox"]},Checkbox[Dynamic[sym]]])}]&/@Range[5],func=Function[#+1],cfun=Function[#0],pos},Panel[Column[{ButtonBar[{("+1":>(data=MapAt[func,data,Position[data,HoldPattern[Checkbox[Dynamic[_?TrueQ]]]]/.Key["Selector"]->Key["Value"]])),("CS":>(data=MapAt[cfun,data,Position[data,HoldPattern[Checkbox[Dynamic[_?TrueQ]]]]/.Key["Selector"]->Key["Value"]])),("CA":>(data//=Query[All,ReplacePart[Key["Value"]->0]]))},ImageSize->{44,Automatic}],Panel[Dynamic[Dataset[data]],Background->White]}]]]
Out[]=
Note that in the cloud notebook version of this, the output example doesn't work initially, but if you copy/paste the GUI into a new cell, it will update, albeit slowly.
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Efficient Dataset Record Updating
by Faizon Zaman
Wolfram Community, STAFF PICKS, August 18, 2023
https://community.wolfram.com/groups/-/m/t/2993382
by Faizon Zaman
Wolfram Community, STAFF PICKS, August 18, 2023
https://community.wolfram.com/groups/-/m/t/2993382