Announcement

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

    Make DynamicForm's addItemChangedHandler fire with programmatic change to FormItem

    Hi Isomorphic. Quick question: how can I get a programmatic change to a FormItem's value trigger the DynamicForm's addItemChangedHandler?

    As an example, this isn't working for me with one SelectItem:
    Code:
    public class TestCases implements EntryPoint {  
        
        private SelectItem selector; 
        
        public void onModuleLoad() {       
            DynamicForm form = new DynamicForm();
            form.addItemChangedHandler(new ItemChangedHandler() {
                @Override
                public void onItemChanged(ItemChangedEvent event) {
                    SC.say("Selected Items: " + selector.getValues().length);
                }
            });
            
            selector = new SelectItem();
            selector.setTitle("Select items");
            selector.setMultiple(true);
            selector.setValueMap("Apples", "Oranges", "Bananas");
            selector.addTitleClickHandler(new TitleClickHandler() {
                @Override
                public void onTitleClick(TitleClickEvent event) {
                    if (selector.getValues().length != 0) {
                        selector.setValues();
                    } else {
                        selector.setValues("Apples", "Oranges", "Bananas");  
                    }
                }
            });
            
            form.setFields(selector);
            form.show();
        }
    }
    I am using SmartGWT 6.0-p20160927.
    Thanks

    #2
    You can't fire it manually, instead, simply put your logic into a method so you can call it both from the changeHandler and directly.

    Comment


      #3
      Thanks Isomorphic for your response. Can you please elaborate on how to achieve that, since I want some automatic way to get the form to update a text item with a summary of all values selected on each FormItem, but in my real case, each form control is a child of FormItem that has some complex behavior attached to it.

      So basically, how could I get my form's field updated in this case?

      By the way, is there any particular reason why the programmatic value change doesn't trigger the changedHandler on the DynamicForm?

      Thanks

      Comment


        #4
        Hi Isomorphic. After reading your response again, I want to clarify something: I don't want to fire manually the ItemChangedHandler. I just want a way to detect programmatic changes to DynamicForm's FormItems. It could be other type of handler or a different approach.
        Is this posible?

        Thanks

        Comment


          #5
          Again, if your own code is making the programmatic change, simply call whatever logic you need to call from the same code that is changing the value.

          As far as the framework's internal calls, the pattern and timing of such calls is intentionally undocumented and subject to change without notice, so even if you could detect such calls, there would be nothing valid you could do with that information.

          The fact that you are trying to do something like this is a strong indication that you've started down the wrong path. What you should do (in this post and every post that is a how-to post) is describe what you need to do *from the end user's perspective* (not in terms of nuts and bolts like how to manually fire a change handler), then Isomorphic or others can show you an appropriate approach.

          Comment


            #6
            Thanks Isomorphic. Let me take a different approach then, according to your latest suggestion.

            My application is a simulator. I have a couple of multiple selection SelectItem sub classes, and each instance of these sub classes displays between 2 and 10 possible values/levels for each simulation setting. There are 7 of these SelectItems in my form (holding all user-customizable simulation settings). In order to let the user know how many simulations will be run, I multiply the amount of values selected so far on each of these SelectItem objects and display that value in some text control in the form.

            Each time the user selects/deselects values/levels on each of these controls, my text control is updated to show the new simulation count. This is working fine as long as the user performs the selection manually using the pick lists on each Selectitem sub class, since the DynamicForm's ItemChangedHandler fires correctly, triggering the method that calculates the new simulation count and refreshes the text element.

            But to make value/level selection easy for the end user, specially in simulations where the user wants to run all possible scenarios, I added a programmatic way select/deselect all items on each of these custom SelectItems. This is the part that's not working, as the form isn't notified of the custom SelectItems changes.

            Finally, in order to implement your suggestion above, I could pass my DynamicForm as a parameter to the constructor of my custom SelectItems, and use that reference to call the DynamicForm's simulation count update method. But this will increase the coupling of these classes, which is something I want to avoid if possible.

            Any ideas on how to approach this use case?

            As always, thanks for your patience and help!!

            Comment


              #7
              So the programmatic change is coming from your code - again simply call the logic to update the overall count at the same time.

              We can't see how this creates any new interedependencies or knowledge between the different components, since clearly your SelectItems are already triggering logic that updates a related control.

              Comment


                #8
                Ok. Thanks Isomorphic...

                Comment

                Working...
                X