Announcement

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

    reuse formItem in dynamicForm.setItems()

    Hi there,

    I have a formItemA, and use dynamicForm.setItems(formItemA) to set it into a dyanmicForm.

    And then, for the same formItemA, reuse dynamicForm.setItems(fromItemA) again, I will got the warning "FormItems cannot be re-used with different DynamicForms", but the operation on the UI is ok.

    Can I ignore this warning? are there potential problems for ignoring this warning?

    Thanks.

    #2
    Well you could ignore it, but you could type "A" in form one, then "B" in form two, but guess what, Form1.formItemA.getValue() == "B" not "A"

    Comment


      #3
      Thanks svjard.

      In my case, I only have 1 dynamicForm, for this form, I used setItems() twice (or more), for every setItems(), some of the input items are different, some are the same.

      By doing this, I want to realize dynamically change the dyanmicform's items, are there any potential problem for this?

      Thanks

      Comment


        #4
        I see what your doing, well calling setItems after you've called draw will cause the item javascript array to delete all its items and then re-add them, setting the destroyed flag on them. The warning appears cause the destroyed flag is found on the item, but this isn't necessarily mean you in trouble. But to throw you a bone, try this code out.
        Code:
        public class MyForm extends DynamicForm {
        	public MyForm() {
        		
        	}
        	
        	public void addItems(FormItem... items) {
        		if (items != null) {
                    for (FormItem field : items) {
                    	_addItems(field);
                    }
                }
        	}
        
        	public native void _addItems(FormItem item) /*-{
        		var self = this.@com.smartgwt.client.widgets.BaseWidget::getOrCreateJsObj()();
        		var formItemJS = item.@com.smartgwt.client.core.DataClass::getJsObj()();
        		self.addItems(formItemJS, self.items.length);
        	}-*/;
        };
        Make a MyForm, and then at somepoint make a call like myForm.addItems(new SpinnerItem())

        Comment


          #5
          Thank you very much.

          I tried it, which can realize the adding itmes step by step, but how can I get the items added? It looks like all the items are "new" in the MyForm.

          In the following code, if I click the button to get the item's value, exception happened:

          "com.google.gwt.core.client.JavaScriptException: (TypeError): self.getValue is not a function"

          Any suggestion? thanks.

          Code:
          public void onModuleLoad() {
          		final IButton b1 = new IButton("click");
          		final TextItem item1 = new TextItem();
          		final MyForm myForm = new MyForm();
          		final VLayout layout = new VLayout(8);
          		
          		item1.setShowTitle(false);
          		item1.setValue("item1");
          		myForm.addItems(item1);
          		layout.addMember(myForm);
          		
          		b1.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
          			
          			@Override
          			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
          				SC.say(item1.getValue().toString());
          			}
          		});
          		layout.addMember(b1);
          		layout.draw();
          }

          Comment


            #6
            Let me play with it a bit, I am just experimenting with this. I'll post my results.
            Last edited by svjard; 25 Jun 2010, 13:51.

            Comment


              #7
              Just a quick note that as the warning states, reusing FormItems is not supported and bad things could happen if you do so. Create a helper method that returns new instances of the same FormItem definitions instead.

              Comment


                #8
                So I got it to work after a number of tries, but your gonna to have to actually modify source code and create your own build. The modification isn't too hard, but if you don't want to go that route you really out of luck. Gotta recreate the form with all new items or use hide/show type idea. If there was an easy mechanism to submit back the modification I would, though I'd have to test is solidly.
                Last edited by svjard; 25 Jun 2010, 17:08.

                Comment


                  #9
                  Hi Svjard,

                  Are there any easy mechanism you found?

                  (I really don't want to use my own build, and recreate the form with all new items or use hide/show are not suitable for my case.)

                  Thanks.

                  Comment


                    #10
                    Depends on the functions your using. Is your form databound? I'll provide you the code when I have access to my system which won't require modifying source but the true solution requires source modification.

                    Comment


                      #11
                      Thanks for your response.

                      I am not using databound, I just use the basic ones.

                      Comment


                        #12
                        Here is the code to use:
                        Code:
                        public class MyForm extends DynamicForm {
                        	public MyForm() {}
                        		
                        	public void addItems(FormItem... items) {
                        		if (items != null) {
                        			for (FormItem field : items) {
                        				if (field != null) {
                        					_myAddItems(field);
                        					JavaScriptObject fieldJS = getFieldJS(field.getName());
                        					field.setJsObj(fieldJS);
                        	            }
                        	        }
                        	    }
                        	}
                        	
                        	private native JavaScriptObject getFieldJS(String fieldName) /*-{
                                var self = this.@com.smartgwt.client.widgets.BaseWidget::getOrCreateJsObj()();
                                var fieldJS = self.getField(fieldName);
                        		return fieldJS;
                            }-*/;
                        
                        	private native void _myAddItems(FormItem item) /*-{
                        		var self = this.@com.smartgwt.client.widgets.BaseWidget::getOrCreateJsObj()();
                        		var formItemJS = item.@com.smartgwt.client.core.DataClass::getJsObj()();
                        		self.addItems(formItemJS, self.items.length);
                        	}-*/;
                        };
                        But just a disclaimer, I when you add items to the form, store those items as variables and then access them directly (getValue(), setValue(), etc...) If you use form functions like getFields(), getField(), etc.. it won't work correctly unless you modify the source. I have the patch you could apply to the latest svn if you want to do your own build. For simple use cases, the above code should work.

                        Comment

                        Working...
                        X