2 Replies Latest reply on Jan 19, 2010 5:10 AM by mw00

    Changing data providers

    mw00

      Hi I have a custom DataGrid component as seen below, I want to reuse this component but only change the url of the XML file in the HTTPService, how would I go about creating one of these components and feeding this string into it from within another mxml file, currently I add it like so <customgrids:CardGrid/> can I add the url string to this tag?

      TIA.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml"
                width="100%"
                dataProvider="{cardItems.lastResult.CardItem}"
                color="#000000"
                verticalScrollBarStyleName="cardBasketVerticalScrollBar"
                height="60%"
                editable="false"
                creationComplete="cardItems.send()">
         
             <mx:HTTPService id="cardItems"
                          url="carditems.xml"
                          resultFormat="e4x" />
               
          <mx:columns>
              <mx:DataGridColumn id="productColumn"
                                   dataField="ProductName"
                                   headerText="Product"
                                   editable="false" />
                                  
              <mx:DataGridColumn id="valueColumn"
                                   dataField="Value"
                                   headerText="Value"
                                   editable="false" />
          </mx:columns>
      </mx:DataGrid>

        • 1. Re: Changing data providers
          archemedia Level 4

          It's not good practice to hardcode your HTTP Service into your component. You should handle data management in your application or, preferably, in a singleton class. (Read articles on mvc design patterns).


          Then, change your component so that you can bind a property to it in the parent component, like this:

           

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml"
                    width="100%"
                    dataProvider="{compDataProvider}"
                    color="#000000"
                    verticalScrollBarStyleName="cardBasketVerticalScrollBar"
                    editable="false">

           

               <mx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      [Bindable]
                      public var compDataProvider:ArrayCollection;
                     
                  ]]>
              </mx:Script>


              <mx:columns>
                  <mx:DataGridColumn id="productColumn"
                                       dataField="ProductName"
                                       headerText="Product"
                                       editable="false" />
                                      
                  <mx:DataGridColumn id="valueColumn"
                                       dataField="Value"
                                       headerText="Value"
                                       editable="false" />
              </mx:columns>
          </mx:DataGrid>

           

           

          Now, you can implement your component in mxml:

           

          <components:MyCustomDataGrid id="myCDG" compDataProvider="{cardItems.lastResult.CardItem}" height="60%"/>

           

          Or, you can dynamically change it through actionScript:

           

          myCDG.compDataProvider = new_result_from_your_service as ArrayCollection;

           

           

          Hope this helps;

           

          Dany

          • 2. Re: Changing data providers
            mw00 Level 1

            Thanks for the advice, I'm not too bothered about correct design at the moment, I am more interested in how you pass data around in flex, this article helped out in the end http://www.adobe.com/devnet/flex/quickstart/building_components_in_mxml/

            Cheers

            Mike.