2 Replies Latest reply on Nov 29, 2007 8:27 PM by rkoe123

    nested repeater problem with custom component

    rkoe123
      Hi there,
      I am really stuck with this issue and any help from you guys will be greatly appreciated.
      To start I have a simple external nested xml file for the data : "book.xml"
      <?xml version="1.0" encoding="UTF-8"?>
      <book>
      <section>
      <sectionnumber>s1</sectionnumber>
      <chapter>
      <chapternumber>c1</chapternumber>
      </chapter>
      <chapter>
      <chapternumber>c2</chapternumber>
      </chapter>
      </section>
      <section>
      <sectionnumber>s2</sectionnumber>
      <chapter>
      <chapternumber>c3</chapternumber>
      </chapter>
      </section>
      </book>

      I also have a main app(NestedRepeater.mxml) with a repeater control which contains a custom mxml component(Section.mxml):
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:comps="component.*" layout="absolute">
      <mx:XML id="data" source="data/book.xml" />
      <mx:VBox>
      <mx:Repeater id="Repeater1" dataProvider="{data.section}">
      <comps:Section sectionNumber="{Repeater1.currentItem.sectionnumber}" />
      </mx:Repeater>
      </mx:VBox>
      </mx:Application>

      And in my custom component(Section.mxml), I have another repeater which I want it for the chapter of each section as a <mx:state> for the user to be able to show/hide the chapters of each section.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml">
      <mx:Script>
      <![CDATA[
      [Bindable]
      public var sectionNumber:String;
      ]]>
      </mx:Script>
      <mx:XML id="data" source="../data/book.xml" />
      <mx:VBox>
      <mx:Panel id="panel1" layout="absolute" title="Section">
      <mx:VBox>
      <mx:Label text="{this.sectionNumber}"/>
      <mx:Button id="btnArticles" label="Show Chapters" click="this.currentState='Chapter'"/>
      </mx:VBox>
      </mx:Panel>
      </mx:VBox>
      <mx:states>
      <mx:State name="Chapter">
      <mx:AddChild relativeTo="{panel1}" position="after">
      <mx:VBox>
      <mx:Repeater id="Repeater2" dataProvider="{data.section.chapter}">
      <mx:Panel layout="absolute" title="Chapter">
      <mx:Label text="{Repeater2.currentItem.chapternumber}" />
      </mx:Panel>
      </mx:Repeater>
      </mx:VBox>
      </mx:AddChild>
      </mx:State>
      </mx:states>
      </mx:Canvas>

      So the problem I am having is the relationship of each chapter to its parent section, when I run the application, the output is:
      s1
      c1
      c2
      c3
      s2
      c1
      c2
      c3

      The correct output I want based on the xml data provider should be and not displaying all chapters for each section:
      s1
      c1
      c2
      s2
      c3

      If anyone can have any suggestion, would be greatl appreciated.
      Thanks
        • 1. Re: nested repeater problem with custom component
          ntsiii Level 3
          Yes, do not attempt to get the data again in the component, simply pass the entire section node into the custom component:
          <comps:Section sectionNumber="{Repeater1.currentItem}" />

          In that component have either a public property or a setter function:
          [Bindable]
          public var xmlSection:XML;

          that variable will now have this in it:
          <section>
          <sectionnumber>s1</sectionnumber>
          <chapter>
          <chapternumber>c1</chapternumber>
          </chapter>
          <chapter>
          <chapternumber>c2</chapternumber>
          </chapter>
          </section>

          So repeater2 is:

          <mx:Repeater id="Repeater2" dataProvider="{xmlSection.chapter}">
          <mx:Panel layout="absolute" title="Chapter">
          <mx:Label text="{Repeater2.currentItem.chapternumber}" />
          </mx:Panel>
          </mx:Repeater>

          Very simple, do you follow? I do this very often. Repeater, XML and custom components are an awesome combination.

          Tracy
          • 2. Re: nested repeater problem with custom component
            rkoe123 Level 1
            Yes! Thank you so much for your help Tracy.
            I am please with the application.
            Cheers!