Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Dynamic form to include only fields where canEdit=true

    I'm trying to find a simple way to construct a DynamicForm and include only the fields from the data source where canEdit="true". The reason for this, in part, is that when un-editable fields are included in the form they are also included in the update when the form is saved. I can step through the DataSourceFields and pick out the ones where canEdit="true" but then I have to create a new FormItem for each one, add it to an array and then setFields for the DynamicForm. Is there a simpler way. Maybe just a helper method to create a FormItem from a DataSourceField. There must be such a method somewhere since that is essential what a DynamicForm does. It just does it for all of the data source fields and I only want to do it for a subset.

    #2
    If you are specifying FormItems on a DynamicForm that has a DataSource, you can specify the name of the DataSource field only and the FormItem "inherits" all settings from the DataSource field. So this is basically minimal already - a one-liner per FormItem you want to use.

    This pattern applies across all DataBoundComponents. See also useAllDataSourceFields.

    Comment


      #3
      Is there a known problem with DataSource.getFields()? It is returning an array with one element with getName()=null. The DynamicForm shows all dozen or so fields, but the following code inserted just after the ds is assigned to the form ...

      itemForm.setDataSource(ipItemMasterDS);
      DataSourceField[] dsFields = ipItemMasterDS.getFields();
      System.out.println("Number of fields = " + dsFields.length);

      Shows "Number of fields = 1"

      ??
      Last edited by jay.l.fisher; 26 Nov 2009, 13:17.

      Comment


        #4
        Yes (already fixed). Use getFieldNames() and getField() instead if you need to iterate over them.

        Comment


          #5
          That helps, but now DataSourceField.getCanEdit() is returning false for every field even though many of them are certainly editable.

          Comment


            #6
            You're probably seeing that for fields where the value is unset, meaning that editability will be dynamically determined in the context of the actual databound component. The API needs to be changed to reflect this as null rather than false, but you can either set canEdit to true on editable fields or call a getAttribute() variant to get the underlying value.

            Comment


              #7
              I still can't seem to make this work. Here is the code (quite a bit more than the one line you suggested was needed). Can you spot the error? And is there a simpler way to do this? I just want a submit button and the fields from the data source where canEdit="true". The editable fields show up on the form now, but they are not editable. Just the label and the field value as text.
              Code:
              // Define the item edit form
              ArrayList<FormItem> formFields = new ArrayList<FormItem>();
              String[] dsFields = ipItemMasterDS.getFieldNames();
              for (int i=0; i<dsFields.length;i++) {
              	DataSourceField dsField = ipItemMasterDS.getField(dsFields[i]);
              	if (dsField.getCanEdit()) {
              		FormItem f = new FormItem();
              		f.setName(dsField.getName());
              		formFields.add(f);
              	}
              }
              FormItem[] editFieldsArray = new FormItem[formFields.size()+1];
              SubmitItem submit = new SubmitItem();
              submit.setTitle("Save Changes");
              editFieldsArray[0]=submit;
              for (int i=1; i<formFields.size()+1;i++) {
              	editFieldsArray[i]=formFields.get(i-1);
              }
              itemForm.setDataSource(ipItemMasterDS);
              itemForm.setFields(editFieldsArray);

              Comment


                #8
                One line per field specified on the form if you are specifying them by hand.

                It's not clear what's wrong now, it likely has something to do with your DataSource, which you did not show. If you were to, for example, specify editorTypes as StaticTextItem, you would get this effect.

                Comment


                  #9
                  There are two simple text fields and one with a valueMap, but none have an editorType specified. Here are the relevant fields from the data source. Those same fields also appear in a ListGrid and FilterBuilder and the look and work correctly there.
                  Code:
                  <field name="IDES" type="string" length="25" title="Description" canFilter="true" canEdit="true"/>
                  <field name="IVST" type="string" length="15" title="Vendor Style" canEdit="true"/>
                  <field name="IATT01" type="integer" title="Brand" detail="true"
                   align="left" canEdit="true">
                    <valueMap>
                      <value ID="1">Brand One</value>
                      <value ID="2">Brand Two</value>
                    </valueMap>
                  </field>

                  Comment


                    #10
                    A little more info ... For the text fields, if I add an explicit editorType="TextItem" to the datasource definition the DynamicForm does show them as editable TextItems. Same for the valueMap fields. If I explicitly set editorType="SelectItem" they show up as expected. From what I understand that should not be necessary.
                    Last edited by jay.l.fisher; 27 Nov 2009, 11:50.

                    Comment


                      #11
                      This has now become really bizarre, and is not what we see in equivalent tests. Can you post all the relevant code, even things that you would assume don't have an effect. Ideally, boil it down to a simplified test case.

                      Comment


                        #12
                        I haven't had time to create a simplified test cast but I have another odd bit of behavior that might help in understanding what the problem is. Since everything appeared to work having set the editorType explicitly I went to other things. Then I noticed that although the SelectItems appeared correct, they did not drop down when clicked to show the other available choices. Only the currently selected choice was shown. Re-reading the doc about DynamicForms I came across the setItems method and not knowing how that was different from setFields I decided to change itemForm.setFields(editFieldsArray); to itemForm.setItems(editFieldsArray); to see what happened. The SelectItems started working perfectly. However, when I compiled and deployed the project they no longer worked. They just showed the current selection, not the other choices. I went back and verified that in GWT hosted mode all works perfectly, but not when deployed, even via the Compile/Browse option. Again, these same fields from the same datasource show up on the same page in a ListGrid (where they are also editable) and in a FilterBuilder and in both cases they work perfectly. The DynamicForm is the only place where there is a problem. When I get some time I'll try to create a simpler test case.
                        Last edited by jay.l.fisher; 28 Nov 2009, 14:45.

                        Comment


                          #13
                          Originally posted by Isomorphic
                          This has now become really bizarre, and is not what we see in equivalent tests. Can you post all the relevant code, even things that you would assume don't have an effect. Ideally, boil it down to a simplified test case.
                          Here is the entire datasource trapped in debug after it is converted to js.
                          Code:
                          isc.DataSource.create({
                              dbName:"as400",
                              tableName:"IPITHDR",
                              ID:"IPItemMasterDS",
                              dataFormat:"iscServer",
                              fields:[
                                  {
                                      canEdit:true,
                                      canFilter:true,
                                      editorType:"TextItem",
                                      length:25,
                                      name:"IDES",
                                      title:"Description",
                                      type:"string"
                                  },
                                  {canEdit:false, canFilter:true, name:"IRET", title:"Retail", type:"currency"},
                                  {canEdit:false, canFilter:true, name:"IFRI", title:"1st Receipt", type:"date",
                                   useTextField:"true"},
                                  {canEdit:false, canFilter:true, name:"ITIU", title:"On Hand", type:"integer"},
                                  {canEdit:false, canFilter:true, name:"ISLU1", title:"Week", type:"integer"},
                                  {canEdit:false, canFilter:true, name:"ISLU2", title:"Month", type:"integer"},
                                  {canEdit:false, canFilter:true, name:"ISLU3", title:"Season", type:"integer"},
                                  {canEdit:false, canFilter:true, name:"ISLU4", title:"Year", type:"integer"},
                                  {
                                      canEdit:false,
                                      length:10,
                                      name:"ISKU",
                                      primaryKey:true,
                                      title:"SKU",
                                      type:"integer"
                                  },
                                  {
                                      canEdit:true,
                                      editorType:"TextItem",
                                      length:15,
                                      name:"IVST",
                                      title:"Vendor Style",
                                      type:"string"
                                  },
                                  {
                                      align:"left",
                                      canEdit:false,
                                      detail:true,
                                      displayField:"DNAM",
                                      foreignKey:"IPDIVSN.DDIV",
                                      length:2,
                                      name:"IDIV",
                                      optionDataSource:"IPDIVSN",
                                      title:"Division",
                                      type:"integer",
                                      valueField:"DDIV"
                                  },
                                  {
                                      align:"left",
                                      canEdit:false,
                                      detail:true,
                                      displayField:"DNAM",
                                      foreignKey:"IPDEPTS.DDPT",
                                      length:3,
                                      name:"IDPT",
                                      optionDataSource:"IPDEPTS",
                                      title:"Department",
                                      type:"integer",
                                      valueField:"DDPT"
                                  },
                                  {
                                      align:"left",
                                      canEdit:false,
                                      displayField:"CLNM",
                                      foreignKey:"IPCLASS.CCLS",
                                      length:4,
                                      name:"ICLS",
                                      optionDataSource:"IPCLASS",
                                      title:"Class",
                                      type:"integer",
                                      valueField:"CCLS"
                                  },
                                  {
                                      align:"left",
                                      canEdit:false,
                                      displayField:"VNAM",
                                      foreignKey:"IPMRVEN.VVEN",
                                      length:5,
                                      name:"IVEN",
                                      optionDataSource:"IPMRVEN",
                                      title:"Vendor",
                                      type:"integer",
                                      valueField:"VVEN"
                                  },
                                  {
                                      canEdit:false,
                                      length:4,
                                      name:"ISTY",
                                      title:"Style",
                                      type:"noZeroSuppress4"
                                  },
                                  {
                                      align:"left",
                                      canEdit:false,
                                      displayField:"CLRN",
                                      foreignKey:"IPCOLOR.CCLR",
                                      length:3,
                                      name:"ICLR",
                                      optionDataSource:"IPCOLOR",
                                      title:"Color",
                                      type:"integer",
                                      valueField:"CCLR"
                                  },
                                  {
                                      align:"left",
                                      canEdit:false,
                                      displayField:"SNAM",
                                      foreignKey:"IPSIZES.VVEN",
                                      length:4,
                                      name:"ISIZ",
                                      optionDataSource:"IPSIZES",
                                      title:"Size",
                                      type:"integer",
                                      valueField:"SSIZ"
                                  },
                                  {
                                      canEdit:false,
                                      detail:true,
                                      labelAsTitle:"true",
                                      name:"ISAL",
                                      title:"On sale?",
                                      type:"text",
                                      valueMap:{Y:"true", N:"false"}
                                  },
                                  {
                                      canEdit:false,
                                      detail:true,
                                      labelAsTitle:"true",
                                      name:"IMKD",
                                      title:"Marked Down?",
                                      type:"text",
                                      valueMap:{Y:"true", N:"false"}
                                  },
                                  {
                                      canEdit:false,
                                      detail:true,
                                      labelAsTitle:"true",
                                      name:"IHLD",
                                      title:"Item on hold?",
                                      type:"text",
                                      valueMap:{H:"true", " ":"false"}
                                  },
                                  {
                                      canEdit:false,
                                      detail:true,
                                      labelAsTitle:"true",
                                      name:"IREC",
                                      title:"Reclassed?",
                                      type:"text",
                                      valueMap:{Y:"true", N:"false"}
                                  },
                                  {canEdit:false, detail:true, name:"IPPS1", title:"Sales Week-1", type:"integer"},
                                  {canEdit:false, detail:true, name:"IPPS2", title:"Sales Week-2", type:"integer"},
                                  {canEdit:false, detail:true, name:"IPPS3", title:"Sales Week-3", type:"integer"},
                                  {canEdit:false, detail:true, name:"IPPS4", title:"Sales Week-4", type:"integer"},
                                  {canEdit:false, detail:true, name:"IPPS5", title:"Sales Week-5", type:"integer"},
                                  {
                                      align:"left",
                                      canEdit:true,
                                      editorType:"SelectItem",
                                      name:"IATT01",
                                      title:"Season",
                                      type:"enum",
                                      valueMap:{"1":"Autumn/Winter", "2":"Spring/Summer", "3":"Continuity", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      editorType:"SelectItem",
                                      name:"IATT02",
                                      title:"Status",
                                      type:"enum",
                                      valueMap:{"1":"Live", "2":"Phase Out", "3":"Discontinued", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      editorType:"SelectItem",
                                      name:"IATT03",
                                      title:"Pricing",
                                      type:"enum",
                                      valueMap:{"1":"Promotional", "2":"Regular", "3":"Sale", "4":"Clearance", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      editorType:"SelectItem",
                                      name:"IATT04",
                                      title:"Branding",
                                      type:"enum",
                                      valueMap:{"1":"Branded", "2":"Own Brand", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      editorType:"SelectItem",
                                      name:"IATT05",
                                      title:"Buyer",
                                      type:"enum",
                                      valueMap:{"1":"John Smith", "2":"Ann Brown", "3":"Susan Black", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      detail:true,
                                      editorType:"SelectItem",
                                      name:"IATT06",
                                      title:"Fabric",
                                      type:"enum",
                                      valueMap:{"1":"wool", "2":"cotton", "3":"natural", "4":"leather", "5":"synthetic", "6":"Silk",
                                       "7":"Other", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      detail:true,
                                      editorType:"SelectItem",
                                      name:"IATT07",
                                      title:"Pricing Tier",
                                      type:"enum",
                                      valueMap:{"1":"First", "2":"Second", "3":"Third", "4":"Fourth", "5":"Fifth", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      detail:true,
                                      editorType:"SelectItem",
                                      name:"IATT08",
                                      title:"B2C attribute",
                                      type:"enum",
                                      valueMap:{"1":"B2C attribute..", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      detail:true,
                                      editorType:"SelectItem",
                                      name:"IATT09",
                                      title:"Price Band",
                                      type:"enum",
                                      valueMap:{"1":"Under £10", "2":"Under £50", "3":"Under £100", "4":"Over £500", "999":"UNKNOWN",
                                       "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      detail:true,
                                      editorType:"SelectItem",
                                      name:"IATT10",
                                      title:"Hanging Type",
                                      type:"enum",
                                      valueMap:{"1":"FLAT", "2":"HANGING", "999":"UNKNOWN", "0":" "}
                                  },
                                  {
                                      align:"left",
                                      canEdit:true,
                                      detail:true,
                                      editorType:"SelectItem",
                                      name:"IATT11",
                                      title:"Season Year",
                                      type:"enum",
                                      valueMap:{"1":"SS08", "2":"SS09", "999":"UNKNOWN", "0":" "}
                                  }
                              ],
                              serverType:"sql"
                          })
                          And here is all of the code that deals with the DynamicForm, aside from the initial declaration which is just "private final DynamicForm itemForm = new DynamicForm();"
                          Code:
                          private void buildItemEdit() {
                          	// Define the item edit form
                          	ArrayList<FormItem> formFields = new ArrayList<FormItem>();
                          	String[] dsFields = ipItemMasterDS.getFieldNames();
                          	for (int i=0; i<dsFields.length;i++) {
                          		DataSourceField dsField = ipItemMasterDS.getField(dsFields[i]);
                          		if (dsField.getCanEdit()) {
                          			FormItem f = new FormItem();
                          			f.setName(dsField.getName());
                          			formFields.add(f);
                          		}
                          	}
                          	FormItem[] editFieldsArray = new FormItem[formFields.size()+1];
                          	SubmitItem submit = new SubmitItem();
                          	submit.setTitle("Save Changes");
                          	editFieldsArray[0]=submit;
                          	for (int i=1; i<formFields.size()+1;i++) {
                          		editFieldsArray[i]=formFields.get(i-1);
                          	}
                          	itemForm.setDataSource(ipItemMasterDS);
                          	itemForm.setItems(editFieldsArray);
                          	itemForm.setWidth100();
                          	itemForm.setNumCols(6);
                          	itemForm.setWrapItemTitles(false);
                          	itemForm.setSaveOnEnter(true);
                          }

                          Comment


                            #14
                            Any update? I'm not sure what a simpler test case would look like. The only thing I noticed in looking it over again is that the two text fields with canEdit="true" were type="string" and should have been type="text". I've corrected that but it made no difference. Does the fact that it works in GWT hosted mode and not when deployed make sense? I've tried it with Safari and Firefox browsers and there is no difference.

                            Comment


                              #15
                              Still not getting a reproducible case, and it may have to do with something in your application. Questions:

                              1. are you re-using DynamicForm instances? What if you do not?

                              2. are you causing the DynamicForm to be drawn (by adding it to a drawn Layout, for example) before calling setDataSource? What if you don't do this?

                              Comment

                              Working...
                              X