6 Replies Latest reply on Dec 18, 2010 2:58 PM by nordinkhm

    ArrayCollection and XML

    nordinkhm

      I have an arraycollection which contains data that is loaded from a XML file.

      The objects in that Arraycollection contain a field named "price".

      I want to calculate the total price of all those items in my ArrayCollection.

       

      I used the following code but it doesn't work :

       

      var num:Number;
                      for each(var item:Object in myArrayCollection){
                         
                          num = num + Number(item.price);
                     
                      }

       

      Can someone help me please ?

        • 1. Re: ArrayCollection and XML
          saisri2k2 Level 4

          Initialize the num =0 before you loop.

           

           

          If this answer helped you, Please mark it as such.

          • 2. Re: ArrayCollection and XML
            drkstr_1 Level 4

            Post the output of:

             

            mx.util.ObjectUtil.toString(myArrayCollection);
            • 3. Re: ArrayCollection and XML
              nordinkhm Level 1

              Thx for answering, but initializing the Number variable to 0 doesn't work.

              I am trying to bind my array collection to the datagrid using this code and the calculating the total price:

               

              [Bindable] public var actotalprice:ArrayCollection = new ArrayCollection();

               

              <s:List id="mypizzas" dragEnabled="true" labelField="item" dataProvider="{acpizzas}" />
                      <s:List id="mydesr" dragEnabled="true" labelField="item" dataProvider="{acdessert}"/>
                      <s:List id="mydrink" dragEnabled="true" labelField="item" dataProvider="{acdrinks}" />
                      </s:HGroup>
                      <mx:DataGrid dataProvider="{actotalprice}" dragEnabled="true" id="dgborders" dropEnabled="true">
                          <mx:columns>
                              <mx:DataGridColumn dataField="item" headerText="Item"/>
                              <mx:DataGridColumn dataField="prijs" headerText="Prices"/>
                              <mx:DataGridColumn dataField="aantal" headerText="Quantity"/>
                          </mx:columns>
                             
                         
                      </mx:DataGrid>

               

              Does anyone have an idea ?

              • 4. Re: ArrayCollection and XML
                saisri2k2 Level 4

                for each(var item:Object in myArrayCollection){
                                   
                                    num = num + Number(item.prijs);
                               
                                }

                 

                it is not price which you have to sum it is 'prijs'

                • 5. Re: ArrayCollection and XML
                  nordinkhm Level 1

                  Sorry, but i translated those words to English before posting it on this forum.

                  Apparently i forgot some words in my code to translate, so the code i have is this:

                   

                   

                  [Bindable] public var actotalprice:ArrayCollection = new ArrayCollection();

                   

                  <s:List id="mypizzas" dragEnabled="true" labelField="item" dataProvider="{acpizzas}" />
                          <s:List id="mydesr" dragEnabled="true" labelField="item" dataProvider="{acdessert}"/>
                          <s:List id="mydrink" dragEnabled="true" labelField="item" dataProvider="{acdrinks}" />
                          </s:HGroup>
                          <mx:DataGrid dataProvider="{actotalprice}" dragEnabled="true" id="dgborders" dropEnabled="true">
                              <mx:columns>
                                  <mx:DataGridColumn dataField="item" headerText="Item"/>
                                  <mx:DataGridColumn dataField="price" headerText="Prices"/>
                                  <mx:DataGridColumn dataField="total" headerText="Quantity"/>
                              </mx:columns>
                                 
                             
                          </mx:DataGrid>

                  • 6. Re: ArrayCollection and XML
                    nordinkhm Level 1

                    i found the solution.

                     

                    i had the prices in my xml file with a , instead of using a point

                     

                    If you want to calculate prices and parsing them to a Number value

                    then i have a tip for all of you beginner programmers: Do not use a comma ;-)

                     

                    this topic can be closed.