2 Replies Latest reply on Apr 25, 2009 7:49 PM by ntsiii

    how to load data to repeater dynamically

      Hi

       

      Repeater code:

       

                <mx:Repeater id="rpter" width="600" height="100" >
                  <mx:Canvas width="350" height="30">
                      <mx:HBox id="rpterrow" width="100%" horizontalAlign="center">
                          <mx:Text id="rpttxt" width="150"  />   
                          <mx:Button id="rptbtn1" label="Call" click="onClickRpter(event)" />                                       
                          <mx:Button id="rptbtn2" label="Call" click="onClickRpter(event)" />                                       
                          <mx:Button id="rptbtn3" label="Call" click="onClickRpter(event)" />                                       
                      </mx:HBox>
                  </mx:Canvas>
              </mx:Repeater>

       

      data: this data will be replaced by result from HTTPServer in the future.

      <mx:XML id="rptsource1" xmlns="">
              <root>
              <model>
                  <mn>30-01</mn>
                  <e1>1</e1>
                  <e2>2</e2>
              </model>
              <model>
                  <mn>30-02</mn>
                  <e1>1</e1>
                  <e2>2</e2>
                  <e3>2C</e3>
              </model>
              <model>
                  <mn>30-03</mn>
                  <e1>1</e1>
                  <e2>2</e2>
              </model>
              </root>
          </mx:XML>

       

      here is button click event handler function to assign the data from <mx:XML> to repeater,

                  private function staticLoadXML(e:MouseEvent):void {
                      rpter.dataProvider = "{rptsource1.model}";
                      rpttxt.text = "{rpter.currentItem.mn}"
                      rptbtn1.data =  "{rpter.currentItem.e1}";
                      rptbtn2.data =  "{rpter.currentItem.e2}";
                      rptbtn3.data =  "{rpter.currentItem.e3}";
                      rptbtn1.label =  "{rpter.currentItem.e1}";
                      rptbtn2.label =  "{rpter.currentItem.e2}";
                      rptbtn3.label =  "{rpter.currentItem.e3}";
                  }

       

        rptsource1 would NOT binding to my repeater. What's problem I cannot make it work?

       

       

      Thank you

      Wes

        • 1. Re: how to load data to repeater dynamically
          Gregory Lafrance Level 6

          You need to stuff the xml in an XMLListCollection, then use that as the dataProvider for your Repeater.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                import mx.controls.Alert;
                private function onClickRpter(event:MouseEvent):void{
                  mx.controls.Alert.show(event.currentTarget.label);
                }     
              ]]>
            </mx:Script>
            <mx:XMLListCollection id="rptsource1">
              <mx:XMLList>
                  <model>
                    <mn>30-01</mn>
                    <e1>1</e1>
                    <e2>2</e2>
                  </model>
                  <model>
                    <mn>30-02</mn>
                    <e1>1</e1>
                    <e2>2</e2>
                    <e3>2C</e3>
                  </model>
                  <model>
                    <mn>30-03</mn>
                    <e1>1</e1>
                    <e2>2</e2>
                  </model>
              </mx:XMLList>
            </mx:XMLListCollection>
            <mx:Repeater id="rpter" width="600" height="100" dataProvider="{rptsource1}">
              <mx:Canvas width="350" height="30">
                <mx:HBox id="rpterrow" width="100%" horizontalAlign="center">
                  <mx:Text id="rpttxt" width="150" text="{rpter.currentItem.mn}"/>   
                  <mx:Button id="rptbtn1" data="{rpter.currentItem.e1}" label="{rpter.currentItem.e1}" click="onClickRpter(event)" />                                       
                  <mx:Button id="rptbtn2" data="{rpter.currentItem.e2}" label="{rpter.currentItem.e2}" click="onClickRpter(event)" />                                       
                  <mx:Button id="rptbtn3" data="{rpter.currentItem.e3}" label="{rpter.currentItem.e3}" click="onClickRpter(event)" />                                       
                </mx:HBox>
              </mx:Canvas>
            </mx:Repeater>
          </mx:Application>

          • 2. Re: how to load data to repeater dynamically
            ntsiii Level 3

            Greg's suggestion of using an XMLListCollection is certainly the best long term solution for this case.

             

            But FYI, you can assign the dataProvider directly in a function as you are trying, but you can't set up a binding the way you are.  You could do:

            myRepeater.dataProvider = xmlData.model;

            this assigns the XMLList consisting of model nodes to the repeater and will work.  But it is not a binding.

             

            You cannot set up a binding using braces{} in AS code.