如何获得Itemrenderer的价值

时间:2011-09-16 17:32:43

标签: flex4 checkbox itemrenderer

我想从itemrenderer获取复选框的值。我有一个带有checkBox的数据网格作为itemrenderer,如下所示:

<s:DataGrid id="myGrid" dataProvider="{module_DP}" rowHeight="35" fontSize="9"  
                    x="20" y="20" width="184" height="306">
            <s:columns>
                <s:ArrayList>                     
                    <s:GridColumn headerText="Access" dataField="access">
                        <s:itemRenderer>
                            <fx:Component>
                                <s:GridItemRenderer>
                                    <s:CheckBox label="" click="Check_Click(event)" selected="@{data.access}" horizontalCenter="0"/>
                                </s:GridItemRenderer>
                            </fx:Component>
                        </s:itemRenderer>
                    </s:GridColumn>
                </s:ArrayList>
            </s:columns>
        </s:DataGrid>

Check_Click()方法:

public function Check_Click():void{
trace(I want to get the value of clicked checkbox..in this case "access")
}

我无法弄清楚我需要在跟踪中添加的代码。    有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试:

<s:CheckBox label="" click="Check_Click(event)" selected="@{data.access}" horizontalCenter="0"/>


public function Check_Click(event:MouseEvent):void{
   var cb:Checkbox = event.target as CheckBox
   trace(cb.selected);
}

帖子标题问了一个略有不同的问题“如何获得Itemrenderer的价值”。访问渲染器中的data属性,就像使用{data.access}。

一样

从外面访问它:

<fx:Script>
   <![CDATA[
      import mx.events.FlexEvent;

      import spark.components.gridClasses.IGridItemRenderer;
      private function onGridInitialize(event:FlexEvent):void
      {
         this.addEventListener('moduleItemChange', onModuleItemChange);
      }
      private function onModuleItemChange(event:Event):void
      {
         var item:IGridItemRenderer = event.target as IGridItemRenderer;
         if(!item || !item.data) { /* deal with this */ } 
         trace(item.data.access);            
      }
   ]]>
</fx:Script>
<s:DataGrid id="myGrid" rowHeight="35" fontSize="9"  
         x="20" y="20" width="184" height="306" 
         initialize="onGridInitialize(event)">
   <s:dataProvider>
      <s:ArrayList>         
         <fx:Object   access="true"/>
         <fx:Object   access="false"/>
         <fx:Object   access="false"/>
         <fx:Object   access="true"/>
         <fx:Object   access="true"/>               
      </s:ArrayList>
   </s:dataProvider>
   <s:columns>
      <s:ArrayList>                     
         <s:GridColumn headerText="Access" dataField="access">
            <s:itemRenderer>
               <fx:Component>
                  <s:GridItemRenderer>
                     <fx:Script>
                        <![CDATA[
                           private function Check_Click(even:MouseEvent):void
                           {
                              dispatchEvent(new Event('moduleItemChange', true));
                           }                           
                        ]]>
                     </fx:Script>
                     <s:CheckBox label="" click="Check_Click(event)" selected="@{data.access}" horizontalCenter="0"/>
                  </s:GridItemRenderer>
               </fx:Component>
            </s:itemRenderer>
         </s:GridColumn>
      </s:ArrayList>
   </s:columns>
</s:DataGrid>