8 Replies Latest reply on Aug 4, 2008 10:16 AM by Newsgroup_User

    Numbering a list

    JohnEField
      I want to be able to number the items in a list control that is populated with dynamic data.
      So for example:

      <List dataProvider="{myArray}">
      <Label text="{data.text}"/>
      </List>

      I want to be able to number the items in the list so they appear as
      1. text1
      2. text2
      3. text3
      etc.

      It seems like it should be very straight forward. I simply add another label and populate it with some property of the data provider. However I have been thinking about it for some time now and I can't think how to go about it.

      I hope I have been clear. If anyone could offer any help it would be much appreciated!
        • 1. Re: Numbering a list
          Gregory Lafrance Level 6
          Get the number from the data provider index + 1.
          • 2. Re: Numbering a list
            Level 7

            "JohnEField" <webforumsuser@macromedia.com> wrote in message
            news:g75bg1$jmg$1@forums.macromedia.com...
            >I want to be able to number the items in a list control that is populated
            >with
            > dynamic data.
            > So for example:
            >
            > <List dataProvider="{myArray}">
            > <Label text="{data.text}"/>
            > </List>
            >
            > I want to be able to number the items in the list so they appear as
            > 1. text1
            > 2. text2
            > 3. text3
            > etc.
            >
            > It seems like it should be very straight forward. I simply add another
            > label
            > and populate it with some property of the data provider. However I have
            > been
            > thinking about it for some time now and I can't think how to go about it.
            >
            > I hope I have been clear. If anyone could offer any help it would be much
            > appreciated!

            If it's XML, you can use childIndex(). I think other collections may have
            similar functions that will allow you to get the child index.

            HTH;

            Amy


            • 3. Re: Numbering a list
              JohnEField Level 1
              Thanks for the replies. I am still a little confused however...

              So a more complete version of the code. This is the List I want to number. I am rendering two labels in an item renderer. The second label calls a function getIndex, with the data from the dataProvider as the argument.

              The getIndex function takes the data arguement and looks to return a value that will allow me to number the list. In the example given bellow I called a no existent property index just as an example.

              As far as I can see my problem is that I am passing the wrong type of thing in as an argument to the getIndex function. Perhaps it should be a property of the list itself?
              Any more suggestions would be great. I am sure the two suggestions are great if only I could figure out how to apply it to my example. I am still finding my way with flex so sorry for an elementary mistakes!
              • 4. Re: Numbering a list
                Gregory Lafrance Level 6
                I think instead of

                <mx:Label text="{outerDocument.getIndex(data)}"/>

                you should be able to something like

                <mx:Label text="{parent.dataProvider.getItemIndex(data.FIELD)}"/>

                where FIELD is the dataProvider field of the data whose index is to be provided.

                Maybe you could boil down to a complete yet very simplified code example with data sample.
                • 5. Re: Numbering a list
                  Level 7

                  "JohnEField" <webforumsuser@macromedia.com> wrote in message
                  news:g76u56$du0$1@forums.macromedia.com...
                  > Thanks for the replies. I am still a little confused however...
                  >
                  > So a more complete version of the code. This is the List I want to number.
                  > I
                  > am rendering two labels in an item renderer. The second label calls a
                  > function
                  > getIndex, with the data from the dataProvider as the argument.
                  >
                  > The getIndex function takes the data arguement and looks to return a value
                  > that will allow me to number the list. In the example given bellow I
                  > called a
                  > no existent property index just as an example.
                  >
                  > As far as I can see my problem is that I am passing the wrong type of
                  > thing in
                  > as an argument to the getIndex function. Perhaps it should be a property
                  > of the
                  > list itself?
                  > Any more suggestions would be great. I am sure the two suggestions are
                  > great
                  > if only I could figure out how to apply it to my example. I am still
                  > finding my
                  > way with flex so sorry for an elementary mistakes!
                  >
                  >
                  > //-- LIST TO NUMBER
                  >
                  > <mx:List id="myList" dataProvider="{artwork.piece}">
                  > <mx:itemRenderer>
                  >
                  > <mx:Label text="{data.name}"/>
                  > <mx:Label text="{outerDocument.getIndex(data)}"/>
                  >
                  > </mx:itemRenderer>
                  > </mx:List>
                  >
                  > //-- SCRIPT CALLED
                  >
                  > public function getIndex(item:*):String
                  > {
                  > return (item.index).toString();
                  > }

                  What type of collection are you using as your dataProvider?


                  • 6. Re: Numbering a list
                    JohnEField Level 1
                    I am using a XMLListCollection as the dataProvider.
                    • 7. Re: Numbering a list
                      JohnEField Level 1
                      Trying the childIndex() method works perfectly when the data provider is XML but not with an XMLListCollection. XMLListCollection does not have this method.

                      Perhaps I can convert the XMLListCollection back to XML and do from there. It is strange though because I thought that when XML (or XMLList for that matter) are used as dataProviders they are automatically converted to XMLList Collections.
                      • 8. Re: Numbering a list
                        Level 7

                        "JohnEField" <webforumsuser@macromedia.com> wrote in message
                        news:g77ckf$mf$1@forums.macromedia.com...
                        > Trying the childIndex() method works perfectly when the data provider is
                        > XML
                        > but not with an XMLListCollection. XMLListCollection does not have this
                        > method.

                        If the item is XML of any type, it will know its position in relation to the
                        original XML. XMLListCollection imposes a view on the XML, but does not
                        change the underlying XML. Look at the e4x docs for more info.