creating form items on the fly

I need to create an unknown number of form fields (based on a resultset) and add then add these to a dynamic form. I thought I had it figured out, but I get an error in the logs stating:

18:40:53.279:XRP5:WARN:SelectItem:Unsupported call to SelectItem.create(). FormItems must be created by their containing form. To create form items, use the 'items' property of a DynamicForm instance. See documentation for more details.

What is another way to accomplish this? Below is my code:

                var fieldArray = new Array();
                for (var i = 0; i < CSRC_RS.getLength(); i++) {
                    var sel = isc.SelectItem.create({
                        ID:"csrc_item_" + i,
                        valueField:"valueHere",
                        displayField:"displayTextHere",
                        type:"comboBox",
                        autoFetchData:true,
                        optionDataSource:"dsDS",
                        title: CSRC_RS.get(i).name
                    })
                    fieldArray[i] = sel;
                }


                isc.DynamicForm.create({
                    ID:"OnTheFlyForm",
                    items: fieldArray
                })

As the log says, just don’t call SelectItem.create(). Pass ordinary JavaScript Objects instead:

                    var sel = {
                        ID:"csrc_item_" + i,
                        valueField:"valueHere",
                        displayField:"displayTextHere",
                        type:"comboBox",
                        autoFetchData:true,
                        optionDataSource:"dsDS",
                        title: CSRC_RS.get(i).name
                    };
                    fieldArray[i] = sel;

That does the trick! Thanks!

How can an application user add fields?

I would like to offer my clients the ability to (repeatedly) initiate the addition of a form field via click or key press e.g. TAB, TAB, Enter.
Below is my attempt, which does not work if the isc.say() calls are removed.

<html>
  <head>
    <title>Test runtime addition of data-entry fields</title>
    <SCRIPT>var isomorphicDir = "isomorphic.7.0.rc/";</SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Core.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Foundation.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Containers.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Grids.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Forms.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_DataBinding.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/skins/TreeFrog/load_skin.js"></SCRIPT>
  </head>
<body>
  <script>
  function addNewDataEntryFieldsToForm(form) {
    var items = isc.clone(form.items);
    var counter = items.length;
    var countOfNewFieldsToAdd = 1;
    items.addAll([
      {name:"level" + counter,
       type:"text",
       title: "Subsequent Level(" + (counter + 1) + ")",
       icons: [{
          src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
          click: function() { 
            var expectedNumberOfFormFields = counter + countOfNewFieldsToAdd;
            //isc.say('form.items.length: ' + form.items.length);
            if (form.items.length == expectedNumberOfFormFields) {
              //isc.say('First thing to say');
              addNewDataEntryFieldsToForm(form2);
            } else {
              isc.say('Last thing to say');
            }
          } 
       }],
        endRow: true
      }
    ]);
    form.setFields(items);
    //form.markForRedraw();
  }
          
isc.DynamicForm.create({
  ID: "form1",
  autoDraw: false,
  fields: [
    { name: "level", type: "text", title: "Level", defaultValue: "38.4",
      icons: [{
        src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
        click: function() { 
          var expectedNumberOfFormFields = 0;
          if (form2.items.length == expectedNumberOfFormFields) {
            addNewDataEntryFieldsToForm(form2);
          }
        }
     }]
    }
  ]
});

isc.DynamicForm.create({
  ID: "form2",
  autoDraw: false,
  fields: []
});

var overallVLayout = isc.VLayout.create({
    width: 900,
    height: "100%",
    dragAppearance: "target",
    overflow: "auto", 
    canDragResize: true,
    layoutMargin: 3,
    membersMargin: 10,
    members : [
      form1,
      form2
    ]
});
  </script>
</body>
</html>

TIA

Another attempt fails also

In the implementation below I tried to kick-off the addition of a form to the layout via clicking on the text field’s icon but it similarly fails with the Firebug message: this.form is null
i.e. if(!_1){if(this.parentItem!=null)_1=this…m)_1=this.containerWidget.isDisabled()}}

Launching isc.say() does NOT impede the error as it does in the previous example.

<html>
  <head>
    <title>Test runtime addition of data-entry fields</title>
    <SCRIPT>var isomorphicDir = "isomorphic.7.0.rc/";</SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Core.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Foundation.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Containers.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Grids.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Forms.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_DataBinding.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/skins/TreeFrog/load_skin.js"></SCRIPT>
  </head>
<body>
  <script>
  function addNewDataEntryFormToLayout() {
    var forms = isc.clone(overallVLayout.members);
    var counter = forms.length;
    //var countOfNewFieldsToAdd = 1;
    forms.add([
      isc.DynamicForm.create({
        ID: "form" + counter,
        autoDraw: false,
        fields: [
          { name: "level", type: "text", title: "Level", defaultValue: "66.6",
            icons: [{
              src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
              click: function() { 
                isc.say('This dialog never arises and does NOT impede the isDisabled() failure on null == this.form, e.g. if(!_1){if(this.parentItem!=null)_1=this...m)_1=this.containerWidget.isDisabled()}}');
                addNewDataEntryFormToLayout();
              }
           }]
          }
        ]
      })
    ]);
    overallVLayout.members = forms;
    //form.markForRedraw();
  }
          
isc.DynamicForm.create({
  ID: "form0",
  autoDraw: false,
  fields: [
    { name: "level", type: "text", title: "Level", defaultValue: "38.4",
      icons: [{
        src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
        click: function() { 
            addNewDataEntryFormToLayout();
        }
     }]
    }
  ]
});

var overallVLayout = isc.VLayout.create({
    width: 900,
    height: "100%",
    dragAppearance: "target",
    overflow: "auto", 
    canDragResize: true,
    layoutMargin: 3,
    membersMargin: 10,
    members : [
      form0
    ]
});
  </script>
</body>
</html>

This attempt sorta works except…

… the form’s text fields end up visually stacked upon one another, still not right.
Is this the ‘best’ way to add form items from user interaction?

<html>
  <head>
    <title>Test runtime addition of data-entry fields</title>
    <SCRIPT>var isomorphicDir = "isomorphic.7.0.rc/";</SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Core.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Foundation.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Containers.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Grids.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Forms.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_DataBinding.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/skins/TreeFrog/load_skin.js"></SCRIPT>
  </head>
<body>
  <script>
  
  // javascript:isc.showConsole();
  
  function addNewDataEntryFormToLayout() {
    var counter = overallVLayout.members.length;
    
    var newForm = isc.DynamicForm.create({
        ID: "form" + counter,
        width: "100%",
        height: 25,
        autoDraw: true,
        fields: [
          { name: "level", type: "text", title: "Level", defaultValue: "66.6",
            icons: [{
              src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
              click: function() { 
                addNewDataEntryFormToLayout();
              }
           }]
          }
        ]
      })
    
    overallVLayout.members.add([newForm]);
    newForm.markForRedraw();
  }
          
isc.DynamicForm.create({
  ID: "form0",
  autoDraw: false,
  fields: [
    { name: "level", type: "text", title: "Level", defaultValue: "38.4",
      icons: [{
        src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
        click: function() { 
          addNewDataEntryFormToLayout();
        }
     }]
    }
  ]
});

var overallVLayout = isc.VLayout.create({
    width: 900,
    height: "100%",
    dragAppearance: "target",
    overflow: "auto", 
    canDragResize: true,
    layoutMargin: 3,
    membersMargin: 10,
    members : [
      form0
    ]
});
  </script>
</body>
</html>

Layout.layoutMargin affects the result…

…the added form appears ABOVE the original form; I was hoping the VLayout would automagically append the new form (vertically) but it doesn’t…

<html>
  <head>
    <title>Test runtime addition of data-entry fields</title>
    <SCRIPT>var isomorphicDir = "isomorphic.7.0.rc/";</SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Core.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Foundation.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Containers.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Grids.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_Forms.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/system/modules/ISC_DataBinding.js"></SCRIPT>
    <SCRIPT SRC="isomorphic.7.0.rc/skins/TreeFrog/load_skin.js"></SCRIPT>
  </head>
<body>
  <script>
  
  // javascript:isc.showConsole();
  
  function addNewDataEntryFormToLayout() {
    var counter = overallVLayout.members.length;
    
    var newForm = isc.DynamicForm.create({
        ID: "form" + counter,
        width: "100%",
        height: 25,
        autoDraw: true,
        fields: [
          { name: "level", type: "text", title: "Level_" + counter, defaultValue: "66.6",
            icons: [{
              src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
              click: function() { 
                addNewDataEntryFormToLayout();
              }
           }]
          }
        ]
      })
    
    overallVLayout.members.add([newForm]);
    //newForm.markForRedraw();
    //overallVLayout.markForRedraw();
  }
          
isc.DynamicForm.create({
  ID: "form0",
  autoDraw: false,
  fields: [
    { name: "level", type: "text", title: "Level_0", defaultValue: "38.4",
      icons: [{
        src: "http://ns3.x-client.com/v/6.5/isomorphic/system/reference/exampleImages/icons/16/icon_add_files.png",
        click: function() { 
          addNewDataEntryFormToLayout();
        }
     }]
    }
  ]
});

var overallVLayout = isc.VLayout.create({
    width: 900,
    height: "100%",
    dragAppearance: "target",
    overflow: "auto", 
    canDragResize: true,
    layoutMargin: 30,
    membersMargin: 10,
    members : [
      form0
    ]
});
  </script>
</body>
</html>

Dev Console Watch…

…shows form0 indented below the Layout but the (generated) forms1-n appear at the same level as the Layout.

There’s an addMember() call on Layout, use that instead of directly modifying the members Array, which will cause many problems.

Overall, sometimes it make sense to re-create a form with a dynamically generated set of items, sometimes to use a stacking approach like you’re doing. It really depends on what format you ultimately want the data to be in, whether you’re using validation, and other factors.

Thank you for the help.