9 Replies Latest reply on Sep 14, 2007 8:24 PM by Newsgroup_User

    datagrid even

    Level 7
      Hi All,
      How can I fire an event in my datagrid?
      Or maybe create my own event so I can use it anywhere.
      Any samples or links?
      Thanks

      JFB


        • 2. Re: datagrid even
          Level 7
          Thanks...
          I try this but doesn't work for me. Maybe I have to approach my problem in
          different way.
          I have a datagrid to calculate amounts, also inside the datagrid I have a
          comboBox that contains my rate types.
          When I create a new rows no problem everything works, I defined a function
          in my datagrid "change" method where I do the calculations after I setup the
          type of rate manually. After that I save this on my database.
          Now if I get the record for update, I'm able to get the info into the
          datagrid but the calculations doesn't fire automatic.
          How can I fire "change" method?
          Also any idea how can I set the value of the comboBox?
          Since I'm creating a comboBox everytime I create a row, it's not a way to
          reach the comboBox from outside...
          Tks

          JFB

          "ProfessorC" <webforumsuser@macromedia.com> wrote in message
          news:fc77vu$ori$1@forums.macromedia.com...
          > You would create your own class that extends DataGrid, and add your own
          > events.
          >
          > See:
          > http://flexblog.faratasystems.com/?p=170
          >
          > http://www.adobe.com/devnet/flex/quickstart/building_components_using_code_behin
          > d/
          >
          > http://blog.paranoidferret.com/index.php/2007/07/27/flex-friday-feature-datagrid
          > -component/
          > http://blog.olivermerk.ca/index.cfm/2007/5/23/Flex-Custom-Events
          >
          >


          • 3. Re: datagrid even
            ProfessorC Level 1
            Can we see your code?
            Are you using a custom cell editor for the combobox?
            If you are, I don't believe the change event fires, as you said. But the DataGrid has a series of non-inherited events - itemEditBegin, itemEdtiEnd, itemEditBeginning, itemEditFocusIn and itemEditFocusOut - that you can hook a hander onto. The itemEditEnd should do exactly what you're looking to do. Hook onto that, rather than (or in addition to) the change event for all your processing when a cell's value has been changed via a custom cell editor.

            Check out "Working with Item Editors" in LiveDocs for more info and some great examples http://www.adobe.com/livedocs/flex/201/html/celleditor_073_01.html

            • 4. Re: datagrid even
              Level 7
              Thanks for you reply and help.
              Let me put some code here (I have a long form)
              I'm new in flex and I will check the documentation that you mention but if
              you can explain me I will really help with my time.
              Rgds

              JFB


              public function calculateRateTotal(event:Event):void{
              Here I do all calculations...
              }

              public function getEstimateQtyRO_Handler(event:ResultEvent):void{
              acEstimateQty = ArrayCollection(event.result);
              for(var i:Number=0; i< acEstimateQty.length; i++) {
              //Here I build my Array collection
              acPrice.addItem({qty:acEstimateQty .estimateQty,
              perpiece:acEstimateQty
              .estimatePerPiece,
              setupfee:acEstimateQty .estimateSetupFee});
              //Here I need to fire the event
              //Also need to find the way to update rate_cd.selectedIndex
              }

              price_dg.dataProvider = acPrice;
              }

              <mx:DataGrid id="price_dg" change="calculateRateTotal(event)" x="10" y="28"
              width="590" height="147" editable="true">
              <mx:columns>
              <mx:DataGridColumn headerText="Quantity" dataField="qty"/>
              <mx:DataGridColumn headerText="Rate Type" dataField="rateid"
              editable="false" width="120">
              <mx:itemRenderer>
              <mx:Component>
              <mx:VBox>
              <mx:ComboBox id="rate_cb" dataProvider="{outerDocument.acRate}"
              labelField="label" change="outerDocument.setRate(rate_cb)" width="120"/>
              </mx:VBox>
              </mx:Component>
              </mx:itemRenderer>
              </mx:DataGridColumn>
              <mx:DataGridColumn headerText="per Piece" dataField="perpiece"
              editable="false"/>
              <mx:DataGridColumn headerText="per 1000" dataField="per1000"
              editable="false" labelFunction="formatNumber"/>
              <mx:DataGridColumn headerText="Setup Fee" dataField="setupfee" />
              <mx:DataGridColumn headerText="Total" dataField="total"
              editable="false" />
              </mx:columns>
              </mx:DataGrid>




              "ProfessorC" <webforumsuser@macromedia.com> wrote in message
              news:fc9c39$a5d$1@forums.macromedia.com...
              > Can we see your code?
              > Are you using a custom cell editor for the combobox?
              > If you are, I don't believe the change event fires, as you said. But the
              > DataGrid has a series of non-inherited events - itemEditBegin,
              > itemEdtiEnd,
              > itemEditBeginning, itemEditFocusIn and itemEditFocusOut - that you can
              > hook a
              > hander onto. The itemEditEnd should do exactly what you're looking to do.
              > Hook
              > onto that, rather than (or in addition to) the change event for all your
              > processing when a cell's value has been changed via a custom cell editor.
              >
              > Check out "Working with Item Editors" in LiveDocs for more info and some
              > great
              > examples
              > http://www.adobe.com/livedocs/flex/201/html/celleditor_073_01.html
              >
              >
              >


              • 5. Re: datagrid even
                ProfessorC Level 1
                Basically... instead of

                <mx:DataGrid ... change="calculateRateTotal(event)" ...>
                make it
                <mx:DataGrid ... itemEditEnd="calculateRateTotal(event)" ...>

                then, in calculateRateTotal, check that it's a field/column that you care about

                public function calculateRateTotal(event:DataGridEvent):void {
                if(event.dataField == "rateid") {
                var chosenItem:Object= ComboBox(event.currentTarget.itemEditorInstance).selectedItem;
                var chosenRateId:int=chosenItem.rateid;
                :
                // do whatever calculations you need to, using chosenRateId as the rate the user chose in the combobox, or whatever other fields you need to use

                }
                }

                As per the documentation, you might also need to check that the reason that the item editor is closing is not because the user simply cancelled out of it

                if (event.reason <> DataGridEventReason.CANCELLED)

                I would also remove the change event handler on the combobox itself.
                • 6. Re: datagrid even
                  Level 7
                  I got the idea.. tks for you help.
                  The problem now is... after I setup manually the rateID I go to the database
                  and find the correct % to do the calculation, I have to do this because it's
                  base on qty and rateid together.
                  When the results come back from the Database I fire the function to
                  calculate the amounts.
                  Now I got error there...

                  public function getRateTableRO_Handler(event:ResultEvent):void
                  {
                  acRateTable = ArrayCollection(event.result);
                  calculateRateTotal(event); // Here is the error now
                  }

                  How can I fixed this?
                  Maybe other way to do this?
                  Rgds

                  JFB



                  "ProfessorC" <webforumsuser@macromedia.com> wrote in message
                  news:fc9hji$gc5$1@forums.macromedia.com...
                  > Basically... instead of
                  >
                  > <mx:DataGrid ... change="calculateRateTotal(event)" ...>
                  > make it
                  > <mx:DataGrid ... itemEditEnd="calculateRateTotal(event)" ...>
                  >
                  > then, in calculateRateTotal, check that it's a field/column that you care
                  > about
                  >
                  > public function calculateRateTotal(event:DataGridEvent):void {
                  > if(event.dataField == "rateid") {
                  > var chosenItem:Object=
                  > ComboBox(event.currentTarget.itemEditorInstance).selectedItem;
                  > var chosenRateId:int=chosenItem.rateid;
                  > :
                  > // do whatever calculations you need to, using chosenRateId as
                  > the
                  > rate the user chose in the combobox, or whatever other fields you need to
                  > use
                  >
                  > }
                  > }
                  >
                  > As per the documentation, you might also need to check that the reason
                  > that
                  > the item editor is closing is not because the user simply cancelled out of
                  > it
                  >
                  > if (event.reason <> DataGridEventReason.CANCELLED)
                  >
                  > I would also remove the change event handler on the combobox itself.
                  >
                  >


                  • 7. Re: datagrid even
                    Level 7
                    I got more careful and looks like the itemEditEnd doesn't fire when I change
                    anything on my comboBox.
                    What can I do?
                    Tks

                    JFB



                    "ProfessorC" <webforumsuser@macromedia.com> wrote in message
                    news:fc9hji$gc5$1@forums.macromedia.com...
                    > Basically... instead of
                    >
                    > <mx:DataGrid ... change="calculateRateTotal(event)" ...>
                    > make it
                    > <mx:DataGrid ... itemEditEnd="calculateRateTotal(event)" ...>
                    >
                    > then, in calculateRateTotal, check that it's a field/column that you care
                    > about
                    >
                    > public function calculateRateTotal(event:DataGridEvent):void {
                    > if(event.dataField == "rateid") {
                    > var chosenItem:Object=
                    > ComboBox(event.currentTarget.itemEditorInstance).selectedItem;
                    > var chosenRateId:int=chosenItem.rateid;
                    > :
                    > // do whatever calculations you need to, using chosenRateId as
                    > the
                    > rate the user chose in the combobox, or whatever other fields you need to
                    > use
                    >
                    > }
                    > }
                    >
                    > As per the documentation, you might also need to check that the reason
                    > that
                    > the item editor is closing is not because the user simply cancelled out of
                    > it
                    >
                    > if (event.reason <> DataGridEventReason.CANCELLED)
                    >
                    > I would also remove the change event handler on the combobox itself.
                    >
                    >


                    • 8. Re: datagrid even
                      Level 7
                      Hi,
                      I create my comboBox in a separate component mxml now looks like it fire if
                      I click somewhere else but I'm getting an error in this line

                      var chosenItem:Object =
                      ComboBox(event.currentTarget.itemEditorInstance).selectedItem;
                      Error: cannot convert components::itemComboBoxRate@c51d479 to
                      mx.controls.ComboBox.

                      Please help
                      Rgds

                      JFB


                      "ProfessorC" <webforumsuser@macromedia.com> wrote in message
                      news:fc9hji$gc5$1@forums.macromedia.com...
                      > Basically... instead of
                      >
                      > <mx:DataGrid ... change="calculateRateTotal(event)" ...>
                      > make it
                      > <mx:DataGrid ... itemEditEnd="calculateRateTotal(event)" ...>
                      >
                      > then, in calculateRateTotal, check that it's a field/column that you care
                      > about
                      >
                      > public function calculateRateTotal(event:DataGridEvent):void {
                      > if(event.dataField == "rateid") {
                      > var chosenItem:Object=
                      > ComboBox(event.currentTarget.itemEditorInstance).selectedItem;
                      > var chosenRateId:int=chosenItem.rateid;
                      > :
                      > // do whatever calculations you need to, using chosenRateId as
                      > the
                      > rate the user chose in the combobox, or whatever other fields you need to
                      > use
                      >
                      > }
                      > }
                      >
                      > As per the documentation, you might also need to check that the reason
                      > that
                      > the item editor is closing is not because the user simply cancelled out of
                      > it
                      >
                      > if (event.reason <> DataGridEventReason.CANCELLED)
                      >
                      > I would also remove the change event handler on the combobox itself.
                      >
                      >


                      • 9. Re: datagrid even
                        Level 7
                        Hi,
                        I fixed the error:
                        var chosenItem:Object=
                        ComboBox(event.currentTarget.itemEditorInstance.rate_cb).selectedItem;
                        Now I got an error after click out of the datagrid.
                        Error #1069: Property selectedItem not found on NewEstimate_inlineComponent1
                        and there is no default value.

                        But If I fill the ArrayCollection from my database and set the datagrid
                        dataprovider with this array, the itemEditEnd doesn't fire... so I still
                        stack here.

                        Any other ideas?

                        Tks

                        JFB




                        "ProfessorC" <webforumsuser@macromedia.com> wrote in message
                        news:fc9hji$gc5$1@forums.macromedia.com...
                        > Basically... instead of
                        >
                        > <mx:DataGrid ... change="calculateRateTotal(event)" ...>
                        > make it
                        > <mx:DataGrid ... itemEditEnd="calculateRateTotal(event)" ...>
                        >
                        > then, in calculateRateTotal, check that it's a field/column that you care
                        > about
                        >
                        > public function calculateRateTotal(event:DataGridEvent):void {
                        > if(event.dataField == "rateid") {
                        > var chosenItem:Object=
                        > ComboBox(event.currentTarget.itemEditorInstance).selectedItem;
                        > var chosenRateId:int=chosenItem.rateid;
                        > :
                        > // do whatever calculations you need to, using chosenRateId as
                        > the
                        > rate the user chose in the combobox, or whatever other fields you need to
                        > use
                        >
                        > }
                        > }
                        >
                        > As per the documentation, you might also need to check that the reason
                        > that
                        > the item editor is closing is not because the user simply cancelled out of
                        > it
                        >
                        > if (event.reason <> DataGridEventReason.CANCELLED)
                        >
                        > I would also remove the change event handler on the combobox itself.
                        >
                        >