首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

Flex应用Repeater创建checkbox组

2012-10-30 
Flex使用Repeater创建checkbox组使用Repeater创建checkbox组并默认选中给出选中的结果mx:Application xml

Flex使用Repeater创建checkbox组
使用Repeater创建checkbox组并默认选中给出选中的结果


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white" creationComplete="init()">

    <mx:Style>
        Alert {
            backgroundAlpha: 0.8;
            backgroundColor: black;
            borderAlpha: 0.8;
           borderColor: black;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.controls.RadioButton;
            import mx.collections.ArrayCollection;
      //定义  Repeater要绑定的数据即用来添加checkbox  模拟所有的操作
      private var roleFields:ArrayCollection = new ArrayCollection([{name:"编号",field:"id",link:"false"}
                                       ,{name:"角色名",field:"roleName",link:"true"}
                                      ,{name:"操作",field:"删除角色",link:"false"}]);
      //默认选择的checkbox的集合  模拟用户有的操作
      private var role:ArrayCollection = new ArrayCollection([{name:"编号",field:"id",link:"false"}
                                          ,{name:"操作",field:"删除角色",link:"false"}]);
    //全选操作
     private function selectAll(event:Event):void {
    var target:CheckBox = event.currentTarget as CheckBox;
    var i:uint;
    if(target.selected){
    
     for(i=0;i<roleFields.length;i++) {
     checkBox[i].selected=true;
     }
    }else {
     for(i=0;i<roleFields.length;i++) {
      checkBox[i].selected=false;
     }
    }
   }
   private function init():void {
         if(role != null && roleFields != null) {
          //当前角色所拥有的操作的循环
       for(var i:uint=0;i<role.length;i++){
        //所有的操作的循环
        for(var j:uint=0;j<roleFields.length;j++){
         //如果checkbox的data是当前角色拥有的操作的id则check选中
         if(role[i].field == checkBox[j].data){
          checkBox[j].selected=true;
         }
        }
       }
         }
        }
        //选择的内容
        private function selectedContext():void {
         var context:String = "";
         if(roleFields != null ) {
          for(var i:uint=0;i<roleFields.length;i++) {
           //如果checkbox被选中把选中的内容记录下来
           if(checkBox[i].selected) {
            context = context+","+checkBox[i].data;
           }
          }
         }
         Alert.show(context);
        }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="Red" data="red" />
        <mx:Object label="Orange" data="haloOrange" />
        <mx:Object label="Yellow" data="yellow" />
        <mx:Object label="Green" data="haloGreen" />
        <mx:Object label="Blue" data="haloBlue" />
    </mx:Array>

  <mx:Panel id="panel" width="50%" paddingLeft="10" verticalGap="10">
       <mx:Repeater id="checkBoxRepeater" dataProvider="{roleFields}">
            <mx:CheckBox id="checkBox"
                    label="{checkBoxRepeater.currentItem.name}"
                   data="{checkBoxRepeater.currentItem.field}"
                   />
        </mx:Repeater>
        <mx:ControlBar horizontalAlign="right">
            <mx:CheckBox label="全选" click="selectAll(event)" selected="false"/>
            <mx:Button id="button" label="Click me"  click="selectedContext()"/>
        </mx:ControlBar>
    </mx:Panel>

</mx:Application>

热点排行