0

create options in choice field from variable/loop

It's easy to fill a dynamic Choice field with options from a table/query but how to dynamically create options through something like

var options:=[]
for i from 1 to 3 do
options:=options + i
end;
options

resulting in options 1,2,3.

17 replies

null
    • Fred
    • 9 mths ago
    • Reported - view

    To the best of my limited knowledge dynamic fields are table based, either through a select or reference field.

    If you want to script the creation of choices in your dynamic field then you can just use the same script to create new records in the table you use as the base.

    • Alain_Fontaine
    • 9 mths ago
    • Reported - view

    A Choice (dynamic) field is defined by an array of records. Any way to build an array of records may be used. Of, course, a "select" statement, or the dereferencing of a table reference are obvious ways to produce an array of records. But there are lots of alternatives, like for example making use of the fact that a "for" loop returns a array For example:

    let t := Select Table1;
    for f in unique(t.Field) do
        first(t[Field = f])
    end order by Field
    

    In this example, "Table1" has many records, but "Field" has only a few different values. The formula builds an array of records from Table1, containing one record for each value of "Field".

      • szormpas
      • 9 mths ago
      • Reported - view

        Hi, How can we build an array of records from scratch? Can you give us an example without using the "select" function?

      I am also interested in populating a dynamic choice field with novel items.

      • Fred
      • 9 mths ago
      • Reported - view

      Dynamic fields are based on table records (however you want to get/filter your records). All you need to do is create a table and create records with whatever you want and then reference it in your dynamic fields.

      • szormpas
      • 9 mths ago
      • Reported - view

        Hi, thanks. I was just wondering whether I can bypass the creation of records into a temporary table. For example, after calling an API endpoint be able to get the responce directly into a dynamic choice field.

      • Fred
      • 9 mths ago
      • Reported - view

      I'm not familiar with API calls. Maybe there is a way to trick Ninox into thinking it is getting record data. I tried creating JSON tables and that didn't work.

    • Digital Humanities Researcher
    • Christoph.2
    • 9 mths ago
    • Reported - view

    In my case, the options are numeric and calculated, so the may be 1,2,3,4 another time 33,45,556,24667. I don't think it's efficient to do this with a table that has all potential numbers and then filter your way back...

      • Alain_Fontaine
      • 9 mths ago
      • Reported - view

       You don’t need that many records, just the minimum number to hold your set of values. Also, you don’t need to delete and create records, you can reuse them once they have been created.

      • Rafael Sanchis
      • Rafael_Sanchis
      • 9 mths ago
      • Reported - view

       

      Hi Alain I'm playing with your Test, there are any way when delete some items on Value field, delete on the Choices Table.

      the other question I try it with text not with number, how can I chage the result field to see Text.

      Thanks

      • Alain_Fontaine
      • 9 mths ago
      • Reported - view

       First question: the script does not delete any records in the "Choices" table. They are kept for later reuse. One should not intervene in the automatic management of that table. Second question: the script works with any type of values: just change the "Value" field in the "Choices" table to another type, like for example "Text".

      • Rafael Sanchis
      • Rafael_Sanchis
      • 9 mths ago
      • Reported - view

       Thanks Alain

    • Alain_Fontaine
    • 9 mths ago
    • Reported - view

    Here is variation, with a better separation of the preparation of the dynamic choice field and its actual use. It also corrects a rather stupid error (a "let" in the wrong place…).

      • Rafael Sanchis
      • Rafael_Sanchis
      • 9 mths ago
      • Reported - view

       

      A very simple example for use your script.

    • Digital Humanities Researcher
    • Christoph.2
    • 9 mths ago
    • Reported - view

    that's quite a smart solution, thnx

    • Digital Humanities Researcher
    • Christoph.2
    • 9 mths ago
    • Reported - view

      This is a related follow-up, but slightly different.

    I need to parse through a JSON array [{"IO":5},{"IO":6}]. A dynamic choice should show only those records of table IOs that have the Id 5 or 6 (i.e. all values of the key IO in the JSON).

    IOs[for option in my_JSON do
    IOs.Id = number(item(option, "IO"))
    end]

    This does not work but returns all records of IO. How can the filter map against an array?

      • Digital Humanities Researcher
      • Christoph.2
      • 9 mths ago
      • Reported - view

      I assume contains(any,any) will do the trick but I don't manage

      • Digital Humanities Researcher
      • Christoph.2
      • 9 mths ago
      • Reported - view

      I managed

Content aside

  • Status Answered
  • 9 mths agoLast active
  • 17Replies
  • 280Views
  • 5 Following