Passing data from a Text field or MultipleChoice field to a DynamicMultipleChoice field
If you have a MultipleChoice field, and want to pass the same chosen options to a DynamicMultipleChoice field, follow the steps below:
- Create a new reference table with the options written exactly the same ('Table2'):
- Add a DynamicMultiplechoice field ('DynamicMultiChoice') in the same table as you have the MultipleChoice field ('Table1').
- Add a button & insert the code below (adjusted to your case)
* The data needs to be entered in the DynamicMultiplechoice field as an array containing the same id's from the reference table;
do as server for t in select 'Table1' do let a := ; for i in t.split(concat(MultipleChoice), ",") do let c := [number(first((select 'Table2')[trim(i) = Text].Id))]; a := array(a, c) end; t.('DynamicMultiChoice' := a) end end
This will pass the information chosen in the MultipleChoice field to the DynamicMultipleChoice field for each record in the table
* Same logic applies if you want to pass the data from a Text field to a DynamicMultipleChoice field.
Have fun with Ninox!
Thank you very much Maria for this example of handling DynamicMultiChoice fields.
I would like to add here two other ways of doing the same thing, just to show the richness and diversity of possible approaches (knowing that these different approaches will speak more to people already advanced in terms of Ninox code):
do as server for t in select Table1 do t.(DynamicMultiChoice := for i in t.split(concat(MultipleChoice), ", ") do first(select Table2 where i = Text).ID end) end end
By replacing the argument "," (comma) with ", " (comma + space) in the split function, I no longer need to do a trim(t) because there are no spaces at the end of the labels in the array created by the split function.
Here I use the for loop as an ID array generator. The value returned by this loop is an array of IDs, just like a (select Table).ID would do.
It is interesting to see that a DynamicMultpleChoice accepts both an array of IDs (["A1" , "A2" , "A3"...]) and an array of numbers representing the ID number ([1,2,3...]).
It is also possible to optimise the ID search by doing only one select instead of X selects in a for loop.
do as server for t in select Table1 do t.(DynamicMultiChoice := (select Table2 where (var a := Text; count(t.split(concat(MultipleChoice), ", ")[= a]))).ID) end end
Here we also construct an array of IDs from a select of Table2 with a where filter on the presence of text in the split(MultipleChoice) array. We are obliged to create the intermediate variable a because the context of the array does not know the text field of Table2 directly.
I hope that this is clear enough and that it may be of use to some .