加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex DropDownList with CheckBox 多选

发布时间:2020-12-15 03:41:03 所属栏目:百科 来源:网络整理
导读:效果图: 两个主要类: CheckBoxItemRenderer.mxml ?xml?version="1.0"?encoding="utf-8"?s:ItemRenderer?xmlns:fx="http://ns.adobe.com/mxml/2009"????????????????xmlns:s="library://ns.adobe.com/flex/spark"????????????????xmlns:mx="library://ns.adob

效果图:

wKioL1PXWY6jjHlCAABovyn4oa4357.jpg

两个主要类:

CheckBoxItemRenderer.mxml

<?xml?version="1.0"?encoding="utf-8"?>
<s:ItemRenderer?xmlns:fx="http://ns.adobe.com/mxml/2009"
????????????????xmlns:s="library://ns.adobe.com/flex/spark"
????????????????xmlns:mx="library://ns.adobe.com/flex/mx"
????????????????width="100%"?autoDrawBackground="true">
????<s:layout>
????????<s:VerticalLayout/>
????</s:layout>
????
????<fx:Script>
????????<![CDATA[

????????????protected?function?checkbox_changeHandler(event:Event):void
????????????{
????????????????var?chk:CheckBox?=?event.currentTarget?as?CheckBox;
????????????????var?ddl:DropDownListEx?=?this['outerDocument']?as?DropDownListEx;
????????????????
????????????????if(chk.selected){
????????????????????ddl.selectedItems.push(data);
????????????????}else{
????????????????????ddl.selectedItems.splice(ddl.selectedItems.indexOf(data),?1);
????????????????}
????????????????
????????????????ddl.dispatchEvent(new?Event("selectedItemsChange"));
????????????}
????????????
????????????override?public?function?set?data(value:Object):void{
????????????????super.data?=?value;
????????????????
????????????????var?ddl:DropDownListEx?=?this['outerDocument']?as?DropDownListEx;
????????????????chkbox.selected?=?ddl.selectedItems.indexOf(value)?!=?-1;
????????????}
????????]]>
????</fx:Script>
????
????<s:HGroup?width="100%">
????????<s:Spacer?width="5"/>
????????<s:CheckBox?id="chkbox"?width="100%"?label="{data.name}"?change="checkbox_changeHandler(event)"/>
????</s:HGroup>
????
</s:ItemRenderer>

DropDownListEx.mxml

<?xml?version="1.0"?encoding="utf-8"?>
<s:DropDownList?xmlns:fx="http://ns.adobe.com/mxml/2009"
????????????????xmlns:s="library://ns.adobe.com/flex/spark"
????????????????xmlns:mx="library://ns.adobe.com/flex/mx"
????????????????xmlns:comp="comp.*"
????????????????creationComplete="dropdownlist_creationCompleteHandler(event)">
????<fx:Declarations>
????????
????</fx:Declarations>
????
????<fx:Script>
????????<![CDATA[
????????????
????????????import?mx.events.FlexEvent;
????????????
????????????private?var?_selectedItems:Vector.<Object>?=?new?Vector.<Object>();
????????????
????????????override?public?function?set?selectedItems(value:Vector.<Object>):void{
????????????????this._selectedItems?=?value;
????????????}
????????????
????????????override?public?function?get?selectedItems():Vector.<Object>{
????????????????return?_selectedItems;
????????????}
????????????
????????????override?public?function?get?prompt():String{
????????????????
????????????????var?s:String?=?"";
????????????????
????????????????selectedItems.sort(function?compare(x:Object,?y:Object):Number?{
????????????????????return?-(Number(y['index'])?-?Number(x['index']));
????????????????});
????????????????
????????????????for?each(var?item:Object?in?selectedItems){
????????????????????s?+=?(item['name']+",");
????????????????}
????????????????
????????????????if(s.length?>?0){
????????????????????return?s.substr(0,?s.length?-?1);
????????????????}else{
????????????????????return?super.prompt;
????????????????}
????????????}
????????????
????????????protected?function?dropdownlist_creationCompleteHandler(event:FlexEvent):void?{
????????????????this.addEventListener("selectedItemsChange",?selectedItemsChangeHandler);
????????????}
????????????
????????????private?function?selectedItemsChangeHandler(evt:Event):void{
????????????????this.labelDisplay.text?=?prompt;
????????????}
????????????
????????????override?protected?function?item_mouseDownHandler(event:MouseEvent):void{
????????????????
????????????}
????????????
????????]]>
????</fx:Script>
????
????<s:itemRenderer>
????????<fx:Component>
????????????<comp:CheckBoxItemRenderer/>
????????</fx:Component>
????</s:itemRenderer>
</s:DropDownList>

使用方法:

????????????????<comp:DropDownListEx?width="230">
????????????????????<comp:dataProvider>
????????????????????????<s:ArrayCollection>
????????????????????????????<fx:Object?index="0"?name="Sun"/>
????????????????????????????<fx:Object?index="1"?name="Mon"/>
????????????????????????????<fx:Object?index="2"?name="Tues"/>
????????????????????????????<fx:Object?index="3"?name="Wed"/>
????????????????????????????<fx:Object?index="4"?name="Thur"/>
????????????????????????????<fx:Object?index="5"?name="Fri"/>
????????????????????????????<fx:Object?index="6"?name="Sat"/>
????????????????????????</s:ArrayCollection>
????????????????????</comp:dataProvider>
????????????????</comp:DropDownListEx>

特别说明:

index:多选项目排序索引

name:选项显示的内容

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读