2011-09-21 71 views
0
This is my application: 


<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark"> 

    <s:layout> 
     <s:VerticalLayout/> 
    </s:layout> 

    <fx:Script> 
     <![CDATA[ 
      import mx.collections.XMLListCollection; 
      import mx.controls.Alert; 
      import mx.events.FlexEvent; 

      import spark.components.gridClasses.IGridItemRenderer; 

      public var modulesXMLList:XMLListCollection; 

      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) 
        //item.data.access = !item.data.access; 
        Alert.show(item.data.sub_module);  
      } 

      protected function controlPanel_Datagrid_creationCompleteHandler(event:FlexEvent):void 
      { 
       modulesXMLList = new XMLListCollection(tempXML.module as XMLList); 

       modulesXMLList.refresh(); 
       myDatagrid.dataProvider = modulesXMLList; 

      } 

     ]]> 
    </fx:Script> 

    <fx:Declarations> 

     <s:RadioButtonGroup id="rbg1"/> 
     <fx:XML id="tempXML" source="assets/myXMl1.xml"/> 

     <!--<s:ArrayCollection id="moduleControlPanel_DP"> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub1" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub2" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub3" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub4" default="true" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub5" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub6" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub7" default="true" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub8" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub9" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub11" default="true" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub11" default="true" access="true"/> 
     </s:ArrayCollection> 
     --> 
    </fx:Declarations> 


    <s:DataGrid id="myDatagrid" creationComplete="controlPanel_Datagrid_creationCompleteHandler(event)" rowHeight="35" fontSize="9" 
       x="20" y="20" width="184" height="176"> 
     <s:columns> 
      <s:ArrayList>     
       <s:GridColumn headerText="Default"> 
        <s:itemRenderer> 
         <fx:Component> 
          <s:GridItemRenderer> 
           <s:RadioButton name="yoyo" group="{outerDocument.rbg1}" selected="@{data.default}" label="" horizontalCenter="0"/> 
          </s:GridItemRenderer> 
         </fx:Component> 
        </s:itemRenderer> 
       </s:GridColumn> 
       <s:GridColumn headerText="Access" dataField="@access"> 
        <s:itemRenderer> 
         <fx:Component> 
          <s:GridItemRenderer> 

           <fx:Script> 
            <![CDATA[ 

             override public function set data(value:Object):void 
             { 
              // not sure of the details on when or why, but sometimes this method is passed a null value 
              if(value != null) 
              { 
               //super.data = value; 
               // set itemRenderer's state based on data value 
               //chk.selected = value.access; 
               super.data = value; 
               validateNow(); 
              } 
             } 

             protected function Check_clickHandler(even:MouseEvent):void 
             { 

              dispatchEvent(new Event('moduleItemChange', true)); 
             } 
            ]]> 
           </fx:Script> 

           <s:CheckBox id="chk" click="Check_clickHandler(event)" label="" selected="@{data.access}" horizontalCenter="0"/> 
          </s:GridItemRenderer> 
         </fx:Component> 
        </s:itemRenderer> 
       </s:GridColumn> 
       <s:GridColumn headerText="Sub-Module" dataField="@sub_module" /> 
      </s:ArrayList> 
     </s:columns> 
    </s:DataGrid> 

</s:Application> 



And this my C<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark"> 

    <s:layout> 
     <s:VerticalLayout/> 
    </s:layout> 

    <fx:Script> 
     <![CDATA[ 
      import mx.collections.XMLListCollection; 
      import mx.controls.Alert; 
      import mx.events.FlexEvent; 

      import spark.components.gridClasses.IGridItemRenderer; 

      public var modulesXMLList:XMLListCollection; 

      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) 
        //item.data.access = !item.data.access; 
        Alert.show(item.data.sub_module);  
      } 

      protected function controlPanel_Datagrid_creationCompleteHandler(event:FlexEvent):void 
      { 
       modulesXMLList = new XMLListCollection(tempXML.module as XMLList); 

       modulesXMLList.refresh(); 
       myDatagrid.dataProvider = modulesXMLList; 

      } 

     ]]> 
    </fx:Script> 

    <fx:Declarations> 

     <s:RadioButtonGroup id="rbg1"/> 
     <fx:XML id="tempXML" source="assets/myXMl1.xml"/> 

     <!--<s:ArrayCollection id="moduleControlPanel_DP"> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub1" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub2" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub3" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub4" default="true" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub5" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub6" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub7" default="true" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub8" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub9" default="false" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub11" default="true" access="true"/> 
      <fx:Object habilitations_id ="1" module_id="4" sub_module="Sub11" default="true" access="true"/> 
     </s:ArrayCollection> 
     --> 
    </fx:Declarations> 


    <s:DataGrid id="myDatagrid" creationComplete="controlPanel_Datagrid_creationCompleteHandler(event)" rowHeight="35" fontSize="9" 
       x="20" y="20" width="184" height="176"> 
     <s:columns> 
      <s:ArrayList>     
       <s:GridColumn headerText="Default"> 
        <s:itemRenderer> 
         <fx:Component> 
          <s:GridItemRenderer> 
           <s:RadioButton name="yoyo" group="{outerDocument.rbg1}" selected="@{data.default}" label="" horizontalCenter="0"/> 
          </s:GridItemRenderer> 
         </fx:Component> 
        </s:itemRenderer> 
       </s:GridColumn> 
       <s:GridColumn headerText="Access" dataField="@access"> 
        <s:itemRenderer> 
         <fx:Component> 
          <s:GridItemRenderer> 

           <fx:Script> 
            <![CDATA[ 

             override public function set data(value:Object):void 
             { 
              // not sure of the details on when or why, but sometimes this method is passed a null value 
              if(value != null) 
              { 
               //super.data = value; 
               // set itemRenderer's state based on data value 
               //chk.selected = value.access; 
               super.data = value; 
               validateNow(); 
              } 
             } 

             protected function Check_clickHandler(even:MouseEvent):void 
             { 

              dispatchEvent(new Event('moduleItemChange', true)); 
             } 
            ]]> 
           </fx:Script> 

           <s:CheckBox id="chk" click="Check_clickHandler(event)" label="" selected="@{data.access}" horizontalCenter="0"/> 
          </s:GridItemRenderer> 
         </fx:Component> 
        </s:itemRenderer> 
       </s:GridColumn> 
       <s:GridColumn headerText="Sub-Module" dataField="@sub_module" /> 
      </s:ArrayList> 
     </s:columns> 
    </s:DataGrid> 

</s:Application> 


And this is my XML file: 

<?xml version="1.0" encoding="UTF-8"?> 

<modules> 
    <module habilitation_id="1" module_id="1" sub_module="Sub1" default="true" access="true" /> 
    <module habilitation_id="1" module_id="2" sub_module="Sub2" default="true" access="true" /> 
    <module habilitation_id="1" module_id="2" sub_module="Sub3" default="false" access="false" /> 
    <module habilitation_id="1" module_id="2" sub_module="Sub4" default="false" access="true" /> 
    <module habilitation_id="1" module_id="3" sub_module="Sub5" default="true" access="true" /> 
    <module habilitation_id="1" module_id="3" sub_module="Sub6" default="false" access="true" /> 
    <module habilitation_id="1" module_id="4" sub_module="Sub7" default="true" access="true" /> 
    <module habilitation_id="1" module_id="4" sub_module="Sub8" default="false" access="true" /> 
</modules> 




Anyone please throw some lights. 

回答

0

這是一個有趣的。我遇到了一陣子。您的項目渲染器會在屏幕上滾動時被銷燬並重新使用。因此它看起來沒有被選中。如果我沒有記錯,試着將你的控件綁定到渲染器中的一個方法,該方法將它們的值更新爲數據對象中的值。例如<s:checkbox selected={myMethod(data.selected)} />或在呈現器上查找更新事件並設置您的值。

+0

有趣我知道...這就是我從昨天開始用這個拉頭髮 我嘗試在itemrenderer中使用set data方法,但它忽略它。我也嘗試使用validateNow()作爲一些例子,但徒勞無功。 – FlexyBoz

+0

其他人請幫助這個..對我的項目非常緊迫..很多謝謝 – FlexyBoz

0

剛看到這個帖子,似乎你還沒有接受還沒有任何答案:

你的項目渲染:

override public function set data(value:Object):void 
{ 
    if(value != null) 
    { 
     super.data = value; 
     //chk.selected = value.access; 
     validateNow(); 
    } 
} 

取消對上述行://chk.selected = value.access;

Flex DataGrid回收其itemRenderer以獲得更好的內存性能。當您檢查itemRenderer上的複選框並開始滾動時,帶有複選框的itemRenderer將被重用以顯示仍然設置了selected =「true」值的其他記錄。

所以你必須明確地設置複選框的值。對於這個例子,如果value.access是布爾值,並且它用於複選框,它肯定會工作。

謝謝。

相關問題