3 Replies Latest reply on Feb 26, 2011 8:34 AM by jfb00

    Repeater not working fine when passed a dataprovider (array) having a single element

    uajmal

      I am using a Repeater in an Accordian which does not  appear to see a single element in userArray. If I add another entry to  userArray then the Repeater works fine.

      Thoughts??

          private function currUsersServiceHandler(event:ResultEvent):void{

                         
      if (event.result.currentUsers != null)
                         
      {
                             
      if (event.result.currentUsers.user is ArrayCollection) // if more than one elements are present
                             
      {
                                  usersArray
      = event.result.currentUsers.user;
                             
      }
                             
      else if (event.result.currentUsers is ObjectProxy)
                             
      { //FIXIT usersArray populate by following line has some issue
                                  usersArray
      = new ArrayCollection(ArrayUtil.toArray(event.result.currentUsers));
                             
      }
                         
      }
                     
      }

             
      <mx:HTTPService id="currUsersService" url="currUsers.xml" result="currUsersServiceHandler(event)"/>

      <mx:Accordion includeIn="UserList" x="10" y="10" width="554" height="242" >
             
      <mx:Repeater id="rep" dataProvider="{usersArray}">
                 
      <mx:Canvas width="100%" height="100%" label="{rep.currentItem.firstName}" >
                         
      <mx:HBox>
                             
      <s:Label text="{rep.currentItem.firstName}"/>
                             
      <s:Label text="{rep.currentItem.lastName}"/>
                           
      <mx:/HBox>
                  </
      mx:Canvas>
             
      </mx:Repeater> 
      </
      mx:Accordian>
        • 1. Re: Repeater not working fine when passed a dataprovider (array) having a single element
          jfb00 Level 3

          Hi,

          Try to refresh the arrayCollection after add the item.

          usersArray.refresh();
          Also this should work without calling a extra function
          usersArray = new ArrayCollection(event.result.currentUsers as Array);

          Rgds

          Johnny
          • 2. Re: Repeater not working fine when passed a dataprovider (array) having a single element
            uajmal Level 1

            Thankyou Johnny.

             

            My problem is still not solved.

             

            I tried both of your suggestions i.e.

             

            usersArray = new ArrayCollection(event.result.currentUsers as Array);
            usersArray.refresh();

             

            and

             

            usersArray = new ArrayCollection(ArrayUtil.toArray(event.result.currentUsers));

            usersArray.refresh();

             

             

            Both have not worked. There is another thing that I have just noticed i.e. that the accordian does show a single tab (or element) but it's not labelled with the first name which I am setting. If I enter another user, two tabs (or elements) appear and both are labelled with names I am setting. The first tab (or element) appears labelled too then.

            • 3. Re: Repeater not working fine when passed a dataprovider (array) having a single element
              jfb00 Level 3

              Hi,

              I think your issue is your data. You need to see how the data is coming from the http service, it maybe not an array.

              Check this example:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
                   <mx:Script>
                        <![CDATA[
                             import mx.collections.*;
                             import mx.controls.Alert;
                             
                             [Bindable] public var usersArray:ArrayCollection = new ArrayCollection();
                             
                             private function addItem():void{
                                  var myObj:Object = new Object();
                                  myObj.myLabel = "First";
                                  
                                  if (usersArray.length == 0){
                                       usersArray.addItem(myObj);
                                  }else{
                                       usersArray.addItem("Second"); 
                                       //usersArray.addItem({myLabel:"Second"});
                                  }
                             }
                        ]]>
                   </mx:Script>
                   <mx:Button label="addItem" click="addItem()" />
                   <mx:Accordion x="10" y="41" width="554" height="242" >
                        <mx:Repeater id="rep" dataProvider="{usersArray}">
                             <mx:Canvas width="100%" height="100%" label="{rep.currentItem.myLabel}" >
                                  <mx:HBox>
                                       <mx:Label text="firstName"/>
                                       <mx:Label text="lastName"/>
                                  </mx:HBox>
                             </mx:Canvas>
                        </mx:Repeater>  
                   </mx:Accordion>
              </mx:Application>
              
              

               

              I add an item to an array with an add button. When you try the second time, it adds the item but not as object because it doesn't display the label.

              Now try to switch the comments like:

              }else{
                   //usersArray.addItem("Second"); 
                   usersArray.addItem({myLabel:"Second"});
              }
              

               

              Now the second label is add it.

              I hope this help you to figure out your issue.

              Rgds

               

              Johnny