I have a dataset or records, I want to select records with a GUI (manually ticking on the selected item). Then, I want to apply functions to those items (not in this question).
I think Im almost there, but missing final step for DynamicModule.
Can you please show what is missing in these two examples?
The Dataset
is called db
, with Keys
"name", "id". :
db = Dataset[Map[<|"name" -> 10 + #, "id" -> 20 + #, "T2" -> 30 + #|> &, Range[4]]]
1
I want to select with Checkbox
createViewDb[] := DynamicModule[{x = False, chosenRecords = {}},
(*original db is expanded with a column of Checkboxes, initialised to False * )
dbx = db[All, <|"selected" -> Checkbox[x], #|> &];
(* chosenRecords is a filter of records where Checkbox == True - selected *)
chosenRecords = Select[dbx, #["selected"] == True &];
Column[{dbx, Dynamic[chosenRecords]}]
]
Results: I tick my checkboxes, but no chosen records is returned.
2
I want to select with TogglerBar:
(* query column names by "name" first*)
dbColumnNames = Query[All, "name"]@db // Normal;
(* now create a toggle bar *)
TogglerBar[Dynamic[chosenRecords], dbColumnNames,
Appearance -> "Vertical"]
(* if I evaluate selected, it will return chosen records *)
(* though, dynamic module show an empty db *)
(* here I query the db against the selected items: *)
(* by checking if each db's row with key "name" is a Member in the selected list chosenRecords *)
DynamicModule[{chosenRecords}, Select[db, MemberQ[chosenRecords, #["name"]] &]]
Results: I toggle my column names, but no chosen records is returned.
Could you help showing what is missing in the two example above?
Question is about filtering in DynamicModule, as a bonus could you comment about applying a function to chosen records, after selection:
should the function be within DyamicModule, or could I call it after DynamicModule with something like:
myGUI = DynamicModule[... (*do something and return chosenRecords as result *)
f/@Evaluate[chosenRecords] ?
Edited
I also tried this approach (welcome suggestions to simplify: how to apply ListPicker to a Dataset in a smooth way that does not need to convert first to a list? ):
While the approach without Dynamics works,
{ListPicker[Dynamic[x], Query[All, "title"]@Normal@db],
Dynamic[x]] &]}
(* now evaluate *)
Select[db, MemberQ[x, #["title"]] &]
however, Dynamic[x] in Select does not work.
{ListPicker[Dynamic[x], Query[All, "title"]@Normal@db],
Select[db, MemberQ[Dynamic[x], #["title"]] &]}
db = Map[<|"selected" -> False, "id" -> #, "T1" -> #, "T2" -> # + 1|> &, Range[40]];
$\endgroup$