views:

57

answers:

2

Hi there,

I have posted two problems with a custom itemRenderer see this post. My primary Problem is, that it always selects a wrong line (one under the one I select). I now realized that this happens, when I save back the content of the itemRenderer to the dataProvider. If I code this part out, the dataGrid works as expected...

Does this make sense to you?

thanks Markus

A: 

I extracted the Problem to some lines of code:

CustomRenderer.mxml:

<mx:Script>
    <![CDATA[
        import components.ChoiceRenderer;
        import mx.rpc.events.ResultEvent;
        import mx.events.DataGridEvent;

        private function resultHandler(event:ResultEvent):void {            
            var doc:XML = event.result as XML;
            testGrid.dataProvider = doc.Records.BackSide;           
        }

        private function saveBackDataGridContent(event:DataGridEvent):void{                 
            testGrid.dataProvider[event.rowIndex].TextElement = event.currentTarget.itemEditorInstance.text;                        
        }               

        ]]>
</mx:Script>
<mx:HTTPService id="dataService" result="resultHandler(event)" url = "data/example.xml" resultFormat="e4x"/>

<mx:DataGrid id="testGrid" editable="true" itemEditEnd="saveBackDataGridContent(event)">
      <mx:columns>
        <mx:DataGridColumn itemRenderer="components.ChoiceRenderer" width="230"/>
  </mx:columns>
</mx:DataGrid>

ChoiceRenderer.as

package components
{
    import mx.containers.HBox;
    import mx.controls.CheckBox;
    import mx.controls.Label;

    public class ChoiceRenderer extends HBox
    {

        private var correctAnswer:CheckBox;
        private var choiceLabel:Label;

        public function ChoiceRenderer()
        {
            super();                                
            setStyle("horizontalScrollPolicy", "off");
            correctAnswer = new CheckBox;
            addChild(correctAnswer);            
            choiceLabel = new Label;
            addChild(choiceLabel);      
        }

        override public function set data(xmldata:Object):void{
            if(xmldata.name() == "BackSide"){
                super.data = xmldata.TextElement[0];
                choiceLabel.text = xmldata.TextElement[0].toString();
            }
        }
    }
}

example.xml

<TopContainer>
<Records>
    <BackSide>
        <TextElement>first</TextElement>                            
    </BackSide>
    <BackSide>
        <TextElement>second</TextElement>                       
    </BackSide>
    <BackSide>
        <TextElement>third</TextElement>                            
    </BackSide>
    <BackSide>
        <TextElement>fourth</TextElement>                       
    </BackSide>
    <BackSide>
        <TextElement>fifth</TextElement>                        
    </BackSide>
    <BackSide>
        <TextElement>sixth</TextElement>                        
    </BackSide>
</Records>

Thanks Markus

Markus
A: 

This is probably not the problem, but I would recommend adding children in the createChildren method, like this:

override protected function createChildren() : void {
            correctAnswer = new CheckBox();
            addChild(correctAnswer);            
            choiceLabel = new Label();
            addChild(choiceLabel);      

}
Robusto