4 Replies Latest reply on Jan 19, 2011 10:59 AM by jlanpheer

    How to dynamically set a form property based on control clicked.

    jlanpheer Level 1

      I am relatively new to Flex and am trying to add functionality to an existing application.  My basic scenario is this:

      I have an application where a user can enter search criteria which in turn, pulls matching data into a DataGrid.  The user can (1) press a "Create" button which pulls up a new form which allows a new record to be entered or (2) press a custom control button (of which there is one for every row in the DataGrid) which basically is an "Update" button which pulls up the same form, but populates the form with data from the DataGrid plus some.

       

      My business problem is simple:  One of the columns on the called form needs to be set to "readonly" if the control in #2 above is pressed (no updates allowed) or "enterable" if the "Create" button was pressed as in #1 above.

       

      I need some guidance into how to do this.  Basically, just before the entry/update form is presented to the user, i need to send a message to the form that is about to be displayed somehow that says:  "Hey, i am inserting!" or "Hey, i am updating!".  Both of the above controls already have their own separate clickHandler functions.  If you know of an example tutorial which illustrates how this would be done, i would appreciate it if you could point it out to me.

      cheers!

      Jim Lanpheer

        • 1. Re: How to dynamically set a form property based on control clicked.
          drkstr_1 Level 4

          You could do this a million different ways. It really depends on your current set up. Here is a basic example of one possible solution.

           

          [Bindable]
          public var isNew:Boolean
          
          <s:TextInput editable="{this.isNew}" />
          

           

          You would then set isNew to true or false according to the situation.

           

           

          Give more info (and code!) if you want an answer specific to your situation.

          • 2. Re: How to dynamically set a form property based on control clicked.
            jlanpheer Level 1

            Thank you, this actually works for my scenario and its much simpler than i thought.  Which is always good! 

             

            What i THOUGHT that i might be trying to code is basically a listener that is listening for the "click" event of one of two controls.  So, i was envisioning something like this:

            <mx:Button id="Button1" click="insertNewCheck()" label="Create"/>

             

            <nsComp:DataGridColumnRendererEditButton id="detailButton" click="detailButtonClickHandler()" />

             

            private function insertNewCheck():void {
                    ac.selectedIndex=1;  // "ac" is the canvas that i'm going to insert or update a record.
                    fill_onClick();
                }


            and for the detail (update) button click handler:

                private function detailButtonClickHandler():void {
                    var rwPermissions:Boolean = rwSecurity();   
               
                    if ( rwPermissions ) {
                        ac.selectedIndex=1;
                    }

                 }

            I didn't include code because my code is using some non-standard Flex stuff, a DataForm object from Farata Systems which is a extension to a Form object which isn't standard action script, but was defined like this:

            <fx:DataFormItem dataField="BANK_ACCT_NUM" readOnly="{ThisWasTheProblem}" required="true" label="Bank Acct Num:"/>

             

            Just to learn, i am still wondering if it is possible to write an event listener in the function "ThisWasTheProblem" that would basically listen for the click events on the two buttons above, and if it detected a "click" on the Create button, set the DataFormItem to "updateable" and if it detected a "click" on the Update button, set the DataFormItem to read-only.  If anybody would care to answer this in order to help me learn, i'd appreciate it (you did suggest that there's a million ways to do this  : ), i would appreciate it.  But, your answer DOES solve my problem, and THANKS!

            cheers,

            Jim Lanpheer

            • 3. Re: How to dynamically set a form property based on control clicked.
              drkstr_1 Level 4

              If I understand you correctly, you can give your DataFormItem an id and reference it directly in the click handler.

               

              protected function openButton_clickHandler(event:MouseEvent):void
              {
                   this.bankActNumFormItem.readOnly = false;
                   //...do other stuff
              }
              
              protected function editButton_clickHandler(event:MouseEvent):void
              {
                   this.bankActNumFormItem.readOnly = true;
                   //...do other stuff
              }
              
              
              <fx:DataFormItem id="bankActNumFormItem" dataField="BANK_ACCT_NUM"
                               readOnly="{ThisWasTheProblem}" required="true" label="Bank Acct Num:"/>
              
              • 4. Re: How to dynamically set a form property based on control clicked.
                jlanpheer Level 1

                OK, thank you for your time.  Obviously, i was making this problem WAY more complex than it was!

                 

                cheers,

                jim.