`
DavyJones2010
  • 浏览: 153763 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Flex4: How to Create Checked List ComboBox?

    博客分类:
  • Flex
阅读更多

1.  ComboCheck.as

/*
* ComboCheck
* v1.2.6
* Arcadio Carballares Martín, 2009
* http://www.carballares.es/arcadio
*/
package com.arcadiocarballares {
        import flash.events.Event;
        import flash.events.MouseEvent;
        
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.controls.ComboBox;
        import mx.controls.listClasses.ListBase;
        import mx.core.ClassFactory;
        import mx.events.CollectionEvent;
        import mx.events.FlexEvent;
        
        [Event(name="addItem", type="flash.events.Event")]
        [Event(name="itemsCreated", type="flash.events.Event")]
        [Event(name="selectAll", type="flash.events.Event")]
        [Event(name="deSelectAll", type="flash.events.Event")]
        
        public class ComboCheck extends ComboBox {
                public var itemAllValue:int=-1;
                private const ITEM_ALL_POSITION_DEFAULT:int=-1000;
                private var itemAllPosition:int=ITEM_ALL_POSITION_DEFAULT;
                
                private var _selectedItems:ArrayCollection;
                
                [Bindable("change")]
                [Bindable("valueCommit")]
                [Bindable("collectionChange")]
                public function set selectedItems(value:ArrayCollection):void {
                        _selectedItems=value;
                }
                
                public function get selectedItems():ArrayCollection {
                        return _selectedItems;
                }
                
                public function ComboCheck() {
                        super();
                        addEventListener("comboChecked", onComboChecked);
                        addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
                        addEventListener(Event.CLOSE, onDropDownInit);
                }
                
                private function onCreationComplete(event:Event):void {
                        dropdown.addEventListener(FlexEvent.CREATION_COMPLETE, onDropDownComplete);
                }
                
                override public function set dataProvider(value:Object):void {
                        super.dataProvider = value;
                        // Set selecAll position
                        for (var i:int;i<dataProvider.length;i++) {
                                if (dataProvider[i].value == itemAllValue) {
                                        itemAllPosition=i;
                                        break;
                                }
                        }
                }
                
                override protected function commitProperties():void {
                        super.commitProperties();
                        
                        var render:ClassFactory = new ClassFactory(ComboCheckItemRenderer);
                        super.itemRenderer=render;
                        var myDropDownFactory:ClassFactory = new ClassFactory(ComboCheckDropDownFactory);
                        super.dropdownFactory=myDropDownFactory;
                        
                        selectedItems=new ArrayCollection();
                        for each (var item:Object in dataProvider) {
                                var index:int=selectedItems.getItemIndex(item);
                                if (item.assigned==true) {
                                        if (index==-1) {
                                                selectedItems.addItem(item);
                                        }
                                } else {
                                        if (index!=-1) {
                                                selectedItems.removeItemAt(index);
                                        }
                                }
                        }
                        
                        setText()
                        
                        dispatchEvent(new Event("itemsCreated"));
                        trace ("commit properties!");
                }
                
                private function onDropDownInit(event:Event):void {
                        invalidateProperties();
                } 
                
                private function onDropDownComplete(event:Event):void {
                        trace ("dropdown complete!");
                } 
                
                private function onComboChecked(event:ComboCheckEvent):void {
                        var obj:Object=event.obj;
                        var index:int=selectedItems.getItemIndex(obj);
                        if (index==-1) {
                                selectedItems.addItem(obj);
                                if (obj.value == itemAllValue) {
                                        dispatchEvent(new Event("selectAll"));    
                                } else {
                                        if (selectedItems.length == dataProvider.length - 1) {
                                                selecCheckboxAll(true);
                                        }
                                }
                        } else {
                                selectedItems.removeItemAt(index);
                                if (obj.value == itemAllValue) {
                                        dispatchEvent(new Event("deSelectAll"));    
                                } else {
                                        selecCheckboxAll(false);
                                }
                        }
                        
                        setText();
                        
                        
                        dispatchEvent(new Event("valueCommit"));
                        dispatchEvent(new Event("addItem"));
                }
                
                private function setText():void {
                        if (selectedItems.length>1) {
                                textInput.text='multiple'
                        }
                        if (selectedItems.length==1) {
                                textInput.text=selectedItems.getItemAt(0)[labelField];
                        }
                        if (selectedItems.length<1) {
                                textInput.text='';
                        }
                }
                
                private function selecCheckboxAll(value:Boolean):void {
                        var item:ComboCheckItemRenderer;
                        if (itemAllPosition!=ITEM_ALL_POSITION_DEFAULT) {
                                item=ComboCheckItemRenderer(ListBase(dropdown).indexToItemRenderer(itemAllPosition));
                        
                                if (value) {
                                        item.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
                                } else {
                                        var index:int=selectedItems.getItemIndex(item.data);
                                        if (index!=-1) {
                                                selectedItems.removeItemAt(index);
                                        }
                                        item.selected = false;
                                        item.data.assigned = false;
                                }
                        }
                }
        }
}

 

2. ComboCheckDropDownFactory.as

package com.arcadiocarballares {
        import flash.events.Event;
        import flash.events.MouseEvent;
        
        import mx.controls.ComboBox;
        import mx.controls.List;
        import mx.controls.listClasses.*;
        
        public class ComboCheckDropDownFactory extends List {
                private var index:int=0;
                public function ComboCheckDropDownFactory(): void {
                        addEventListener("comboChecked", onComboChecked);
                }
                
                override protected function mouseOverHandler(event:MouseEvent):void {
                        event.preventDefault();
                        event.stopImmediatePropagation();
                }
                
                override protected function mouseEventToItemRenderer(event:MouseEvent):IListItemRenderer {
                        var row:IListItemRenderer = super.mouseEventToItemRenderer(event);
                        if (row!=null) {
                                index=itemRendererToIndex(row);
                        }
                        return null;
                }
                
                override protected function scrollHandler(event:Event):void {
                        super.scrollHandler(event);
                        refreshComboCheck();
                }
                
                override protected function scrollVertically(pos:int, deltaPos:int, scrollUp:Boolean):void {
                        super.scrollVertically(pos,deltaPos,scrollUp);
                        refreshComboCheck();
                }
                
                private function refreshComboCheck():void {
                        ComboBox(owner).invalidateProperties();
                }
                
                private function onComboChecked (event:ComboCheckEvent):void {
                        var myComboCheckEvent:ComboCheckEvent=new ComboCheckEvent(ComboCheckEvent.COMBO_CHECKED);
                        myComboCheckEvent.obj=ComboCheckEvent(event).obj;
                        owner.dispatchEvent(myComboCheckEvent);
                }
                
        }
}

 

3. ComboCheckEvent.as

package com.arcadiocarballares {
    
    import flash.events.Event;

    public class ComboCheckEvent extends Event {
        
                public static const COMBO_CHECKED:String = "comboChecked";
        public var obj:Object=new Object();
        
        public function ComboCheckEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) {
            super(type, bubbles, cancelable);
        }
        
    }

}

 

4. ComboCheckItemRenderer

package com.arcadiocarballares {
        import flash.events.Event;
        import flash.events.MouseEvent;
        
        import mx.controls.Alert;
        import mx.controls.CheckBox;
        import mx.events.FlexEvent;
        
        public class ComboCheckItemRenderer extends CheckBox {
                
                public function ComboCheckItemRenderer() {
                        super();
                        addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
                        addEventListener(MouseEvent.CLICK,onClick);
                }

                override public function set data(value:Object):void {
                        if (value != null) {
                                super.data = value;
                                if (value.tooltip!=null) {
                                        toolTip = value.tooltip;
                                }
                        }
                }
                
                private function onCreationComplete(event:Event=null):void {
                        if (data!= null && data.assigned==true) {
                                selected=true;
                        }
                }
                
                private function onClick(event:MouseEvent):void {
                        data.assigned=selected;
                        var myComboCheckEvent:ComboCheckEvent=new ComboCheckEvent(ComboCheckEvent.COMBO_CHECKED);
                        myComboCheckEvent.obj=data;
                        owner.dispatchEvent(myComboCheckEvent);
                }
                
        }
}

 

5. Test.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
			   initialize="application1_initializeHandler(event)" xmlns:view="view.*">
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			[Bindable]
			private var cobList:ArrayCollection = new ArrayCollection();
			
			
			protected function application1_initializeHandler(event:FlexEvent):void
			{
				cobList.addItem({label:"AAA", data:"AAA", assigned:true});
				cobList.addItem({label:"BBB", data:"BBB", assigned:true});
				cobList.addItem({label:"CCC", data:"CCC", assigned:true});
			}
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				var selectedItemList:ArrayCollection = comboCheck.selectedItems;
				for each (var o:Object in selectedItemList){
					Alert.show(o.label + " = " + o.data + ", assigned: " + o.assigned);
				}
			}
			
		]]>
	</fx:Script>
	<mx:VBox>
		<view:ComboCheck id="comboCheck" dataProvider="{cobList}" />
		<s:Button click="button1_clickHandler(event)" label="check"/>
	</mx:VBox>
</s:Application>

 

Declaration:

All rights reserved arcadiocarballares.com from Google

http://code.google.com/p/combocheck/source/browse/tags/combocheck-1.2/src/com/arcadiocarballares/?r=17

 

分享到:
评论

相关推荐

    flex4 list全选,反选

    在Flex4中,List组件是用于展示数据列表的常用组件,它可以显示一组可滚动的数据项。在实际应用中,我们经常需要实现对列表中的所有项进行全选或反选的操作,以便用户能快速选择或取消选择全部内容。本文将详细探讨...

    HSHintComboBox_delphi_COMPONENT_

    Now hint is removed.Added the HorizontalExtent property to sethorizontal Scrollbar in the ComboBox list. By default propertyhas a value of -1 which prohibits setting the horizontalScrollBar.

    新视野大学英语视听说教程第二册听力练习录音文本和答案

    M: Have you checked in the car?” 问:What does the man imply? - **答案解析**: 男士建议女士检查汽车内是否有钱包,意味着他认为钱包可能遗落在车里。 - **例子3**: “M: Are you going to buy that pirated...

    软件工程的形式化方法考查试题

    借阅事务(Checked_out)和归还事务(Return)会根据图书是否可借和已借出进行状态更新。 7. **Add和Remove模式**:书库事务的模式Stock_trans中,Add模式用于添加图书,Remove模式用于移除。这需要更新库存stock和...

    java面试题库(自己找的,希望能帮助到大家).rar

    2. **集合框架**:Java集合框架是程序员必须掌握的重要部分,包括List、Set、Map等接口及其实现类。面试中可能会问:ArrayList和LinkedList的区别是什么?HashMap的工作原理是怎样的?如何避免并发修改异常...

    combobox实现下拉框多选

    本方法和用checkbox, listbox等控件和事件拼凑出来的不同,本方法是一个集成的独立控件,基本实现了控件的顺滑度,下拉框可悬浮等效果,可以认为是comboBox的升级版,使用方便,仅需引用编译好的DLL,直接在toolBox...

    C# combobox里面显示checkbox

    在C#编程中,ComboBox控件通常用于提供一个下拉列表供用户选择。然而,有时候我们可能需要在ComboBox中不仅显示文本,还需要显示复选框,以便用户可以选择多个选项。这种功能可以增强用户界面的交互性,使得多选操作...

    医护人员常用英语口语500句3.docx

    4. **病史调查**:"Have you been with anyone who has a cold?"询问患者的接触史,而"Have you had any fever?"和"Do you sweat a lot?"则关注发热和出汗等常见症状。 5. **症状持续时间**:"When did it begin?...

    Java各种必备面试题目程序代码例子与应战技巧.zip

    - 面试题:解释checked和unchecked异常的区别,如何进行异常处理? - 实战代码:编写包含try-catch-finally语句块的代码,演示异常捕获和自定义异常。 5. **多线程**: - 面试题:什么是守护线程?如何创建和...

    jQuery中:checked选择器用法实例

    `:checked`选择器的基本语法非常简单,就像这样: ```javascript $(":checked") ``` 这行代码会返回页面上所有当前被选中的复选框和单选按钮的jQuery对象集合。你可以进一步对这些元素进行各种操作,例如改变它们的...

    新视野大学英语视听说2答案

    - **问题4:**worry too much (现代社会女性过度关注外貌) - **问题5:**more important (健康与外貌哪个更重要) #### Task3: Mozart 这部分介绍了莫扎特的生平及其成就。 - **内容概述:** - 莫扎特是一位...

    旅游英语常见词汇归纳.doc

    - **How many minutes does it take to get to the Capital Airport?**: 到首都机场要几分钟? - **Would you hurry, please?**: 能不能快一点? - **Can you make it in five minutes?**: 能否在五分钟内赶到? ...

    Asp.net CheckBoxList操作集合

    在ASP.NET Web Forms开发中,CheckBoxList控件是一种常用的选择组件,它允许用户从一系列选项中选择一个或多个项目。本篇文章将详细讲解如何进行CheckBoxList的操作,包括获取选中项、实现全选、取消选中以及反选...

    350句精选商务英语TXT

    - **句子**:How long are you going to stay here? - **中文翻译**:你打算在这里停留多久? - **应用场景**:了解访客的停留时间有助于更好地安排接待计划。 ### 42. 有需要申报的东西吗? - **句子**:Do you ...

    带CheckBox功能的ComboBox

    这通常涉及到数据绑定的概念,我们将需要确保控件的DataSource属性与数据集(可能是数组、列表或其他集合类型)关联,并且每个CheckBox的Checked状态对应于数据集中的一项属性。 在“MultiSelectComboBox Demo ...

    VclZip pro v3.10.1

    Modified the SFX code (the code used to create the SFX stub distributed with VCLZip) so that it handles filenames that have been run through an OEM Conversion. The SFX was losing accented characters. ...

    howto_save_checkedlistbox_values_C#_

    "howto_save_checkedlistbox_values"的标题表明这是一个关于如何保存`CheckedListBox`中选定值的教程。在Windows应用程序开发中,这通常涉及到数据持久化,即在用户关闭程序后仍能保留用户的选择。以下是关于这个...

    checked build acpi.sys

    "Checked Build ACPI.sys" 是一个专为Windows 7 RTM(Release to Manufacturing)x86架构设计的特殊版本的ACPI(Advanced Configuration and Power Interface)系统驱动程序。"Checked Build"通常指的是经过严格调试...

    C# ListView中添加多种自定义控件源码

    然而,ListView控件的标准功能有时不能满足所有需求,这时我们可能需要对其进行自定义,添加额外的交互元素,如CheckBox或ComboBox。本文将深入探讨如何在C#的ListView中添加多种自定义控件,以增强用户界面的功能和...

Global site tag (gtag.js) - Google Analytics