10 Replies Latest reply on Aug 18, 2006 2:38 PM by upshotvideo

    giving uniuqe ids in REPEATER

    upshotvideo
      Im getting an error when i try to give uniuque ids in my repeater. I have a repater that makes several checkboxes, but when i try to give each checkbox an ID, it wont let me put variables in there. How can I give each CB a unique name so I can validate the form?
      <mx:Repeater id="featRepeat" dataProvider="{getFeatures.lastResult.features.feature}" >
      <mx:GridRow width="100%">
      <mx:GridItem width="35%">
      <mx:CheckBox label="{featRepeat.currentItem.name}" id="feat_{featRepeat.index}" />
      </mx:GridItem>
      mx:GridItem width="30%">
      <mx:ComboBox width="50" />
      </mx:GridItem>
      <mx:GridItem width="35%">
      mx:TextInput width="50" />
      </mx:GridItem>
      </mx:GridRow>
      </mx:Repeater>
      Thanks
        • 1. Re: giving uniuqe ids in REPEATER
          ntsiii Level 3
          You can't work with repeater controls themselves. You need to update the dataProvider when the checkbox is clicked, using getRepeaterItem(),

          Then work with the dataProvider.

          This is true with all Flex controls like list and DataGrid. Changes to a control are lost if you scroll away. The dataProvider is king.

          Tracy
          • 2. Re: giving uniuqe ids in REPEATER
            upshotvideo Level 1
            i need to send the info with a httpService when the form is done being fill out. The php script that it gets sended to needs to know what CBs were checked. How can I check which ones were checked when I run the validation function?
            • 3. Re: giving uniuqe ids in REPEATER
              Renaun_Erickson
              One approach is to create an LookUp object (hash table) that holds the references to the ComboBox. And then use some key to relate the too. The biggest issue it that Repeater reuses the actual cells so you have to do it independant of the Repeater.

              Basically:
              on the Repeater event where the item is created save a reference in to LookUp object by using something unique from the "currentItem"
              Then when you want to access that item you just use the "currentItem" with the LookUp object and get the reference to the checkbox.
              • 4. Re: giving uniuqe ids in REPEATER
                ntsiii Level 3
                But be aware that if you don't save the checkbox state in the dataProvider, when you scroll away and then back to it, the checkbox state will have been lost.

                This is probably not what you want.

                When you click the checkbox, update the dataProvider item.

                Tracy
                • 5. Re: giving uniuqe ids in REPEATER
                  upshotvideo Level 1
                  I'm sorry, i just don't get it. At any given time, how will any function or anything else know what checkbox is being clicked? So after the repeater, im storing all the info in an object, which works great, but how do i update that object, or ever dataprovider when the checkbox gets clicked? Wouldnt i need to pass something unique for anything to know what item im working with? If I hear what you are saying about repeaters correctly, then you cannot use the click in:

                  <mx:Repeater id="fpFeatRepeat" dataProvider="{getFpFeatures.lastResult.features.feature}">
                  <mx:CheckBox label="{fpFeatRepeat.currentItem.name}" selected="{fpFeatRepeat.currentItem.checked}" click="doSomethingWith(thisCheckBox)" />
                  </mx:Repeater>

                  Where and how do i use the getRepeaterItem()?
                  • 6. Re: giving uniuqe ids in REPEATER
                    ntsiii Level 3
                    Call a handler function on the click event of the checkbox, passing in "event"
                    IN that handler, event.target will give you the checkbox, so you can determine the state.
                    In the handler use event.target.getRepeaterItem() to get a reference to the item that generated that particular repeated control.

                    Use the dataProvider methods to update the checkbox state property in the item.

                    I'll see if I can come up with a 2.0 example.

                    Tracy
                    • 7. Re: giving uniuqe ids in REPEATER
                      upshotvideo Level 1
                      Thank you for all of your help. It all makes sense now. Im so used to php looping to add form elements with unique ids for form valiation. This is whole new concept , but it's starting to make sense now. I'm also used to looping in AS 2.0 to dynamically attach any given number of movie clips to the stage with unique ids for applyig clicks, rollovers, etc. within the loop itself. Will this approach be extinct with AS 3.0?
                      Thanks again
                      • 8. Re: giving uniuqe ids in REPEATER
                        ntsiii Level 3
                        Repeaters as just a little unusual.

                        Here is a full example. Sorry that the forum will probably kill the formatting.

                        Tracy

                        <?xml version="1.0" encoding="utf-8"?>
                        <!--
                        RepeaterUpdateDP.mxml
                        This sample loads the salesdata.xml file using HTTPService, with e4x resultFormat
                        It gets the XMLList of month nodes, and converts it to an XMLListCollection,
                        and assigns it to the repeater dataProvider.
                        The repeater dislpays the month from the @name attribute,displays a checkbox
                        whose selected value is determined from the @showdetail attribute.
                        It also displays a TextInput whose visibility is also determined by the @showdetail attribute
                        Clicking the checkbox updates the dataprovider, as can be varified by clicking the show DP button.
                        That function also re-assign the dataProvider to make it redraw with the changed data.
                        There should be a better way to do this.
                        -->
                        <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="left"
                        creationComplete="initApp()">
                        <mx:Script><![CDATA[
                        import mx.collections.XMLListCollection;
                        import mx.rpc.events.ResultEvent;
                        import mx.controls.TextInput
                        import mx.controls.Alert
                        import mx.rpc.events.ResultEvent;
                        private var _xmlSalesData:XMLList;
                        [Bindable]
                        private var _xlSalesData:XMLListCollection;

                        private function initApp():void
                        {
                        dsSalesData.send(); //send for the data
                        }//initApp

                        private function onResult(oEvent:ResultEvent):void
                        {
                        _xmlSalesData = XMLList(oEvent.result.month); //gets the XMLList of month nodes
                        _xlSalesData = new XMLListCollection(_xmlSalesData) //wraps that in an XMLListCollection
                        rpSalesData.dataProvider = _xlSalesData; //assign the collection to the repeater dataProvider
                        }//onResult

                        //sets the dataProvider item property based on the checkbox selected value
                        private function updateDP(oEvent:Object):void
                        {
                        var oItem:Object = oEvent.target.getRepeaterItem(); //get the dataProvider item
                        oItem.@showdetail = oEvent.target.selected; //set the attribute value
                        //_xlSalesData.itemUpdated(oItem) //does not work
                        //rpSalesData.invalidateDisplayList(); //does not work
                        rpSalesData.dataProvider = _xlSalesData; //required to make the display refresh
                        }

                        ]]></mx:Script>
                        <mx:HTTPService id="dsSalesData" url="assets/salesdata.xml" result="onResult(event)" resultFormat="e4x"/>
                        <mx:VBox id="vbox1" height="200" width="400">
                        <mx:Repeater id="rpSalesData" recycleChildren="true">
                        <mx:HBox>
                        <mx:Label id="lblMonth" text="{rpSalesData.currentItem.@name}" width="50"/>
                        <mx:CheckBox label="Show Detail" click="updateDP(event)" selected="{rpSalesData.currentItem.@showdetail==true}"/>
                        <mx:Label text="Detail:" />
                        <mx:TextInput id="tiDetail" text="{rpSalesData.currentItem.@revenue}"
                        visible="{rpSalesData.currentItem.@showdetail==true}"
                        enabled="false"
                        width="50"/>
                        </mx:HBox>
                        </mx:Repeater>
                        </mx:VBox>
                        <mx:Button label="Show dp" click="{Alert.show(_xmlSalesData.toXMLString())}" />
                        </mx:Application>


                        • 9. Re: giving uniuqe ids in REPEATER
                          ntsiii Level 3
                          salesdata.xml looks like this:

                          <list>
                          <month name="Jan-04" revenue="400263" average="80052" showdetail="true"></month>
                          <month name="Feb-04" revenue="379145" average="75829" showdetail="false"></month>
                          <month name="Mar-04" revenue="389687" average="77937" showdetail="true"></month>
                          <month name="Apr-04" revenue="460329" average="92065" showdetail="false"></month>
                          <month name="May-04" revenue="351014" average="70202" showdetail="true"></month>
                          <month name="Jun-04" revenue="384855" average="76971" showdetail="true"></month>
                          <month name="Jul-04" revenue="335192" average="67038" showdetail="true"></month>
                          <month name="Aug-04" revenue="393654" average="78730" showdetail="false"></month>
                          <month name="Sep-04" revenue="472554" average="94510" showdetail="true"></month>
                          <month name="Oct-04" revenue="324299" average="64859" showdetail="true"></month>
                          <month name="Nov-04" revenue="415403" average="83080" showdetail="true"></month>
                          <month name="Dec-04" revenue="386089" average="77217" showdetail="true"></month>
                          </list>
                          • 10. Re: giving uniuqe ids in REPEATER
                            upshotvideo Level 1
                            Wow, that's even cooler than the way i started doing it Thanks again. I figured it out based on what you were telling me before, but what i was doing is put all the xml data into arrays, then updating the arrays when the checkboxes where clicked. It makes way more sense to just udpate the dataprovider itself like what you are showing me here.
                            Thanks again