有没有好的方法可以在flex中获得三态复选框?

发布于 2024-07-29 13:11:52 字数 161 浏览 6 评论 0原文

我在互联网上进行了很好的搜索,但似乎找不到任何三态复选框的示例。 SDK 似乎不支持它,而且我在网上找不到任何示例。

我想这是一个常见问题,在我开始编写自己的之前,有人知道我可以在某个地方使用一个好的弹性三态复选框组件吗:)

干杯,

Jawache。

I've had a good rummage around the interweb and can't seem to find any examples of a tri-state checkbox. It doesn't look to be supported in the SDK and I can't find any examples online.

I would imagine this is a common problem, before I embark on writing my own does anyone know of a good flex tri-state checkbox component somewhere I can use :)

Cheers,

Jawache.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别闹i 2024-08-05 13:11:52

There is an example posted here on Flex Cookbook. You can easily create your own component from this code.

只为守护你 2024-08-05 13:11:52

对于后来遇到此问题的其他人,我制作了自己的自定义组件,该组件仅隐藏第三状态的复选框,并将其替换为看起来像第三状态下的复选框的图像。 这真的很容易实现:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Script>
    <![CDATA[
        public static const BOX_CHECKED:int = 1;
        public static const BOX_REJECTED:int = -1;
        public static const BOX_UNCHECKED:int = 0;
        [Bindable] public var boxVisible:Boolean = true;
        [Bindable] public var enum_id:int = -1;
        protected function checkbox_clickHandler(event:MouseEvent):void
        {
            if(!checkbox.selected)
                boxVisible = false;             
        }

        protected function image_clickHandler(event:MouseEvent):void
        {
            boxVisible = true;              
        }

        public function set label(str:String):void {
            checkbox.label = str;
            xLabel.text = str;
        }

        public function get boxState():int {
            if(checkbox.selected)
                return BOX_CHECKED;
            else if(redX.visible)
                return BOX_REJECTED;

            return BOX_UNCHECKED;
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:CheckBox id="checkbox" click="checkbox_clickHandler(event)" visible="{boxVisible}" includeInLayout="{boxVisible}"/>
<s:HGroup>
    <s:Image id="redX" source="@Embed('assets/icons/redX.png')"
         width="16" height="16"
         click="image_clickHandler(event)" 
         visible="{!boxVisible}" includeInLayout="{!boxVisible}"/>
    <s:Label id="xLabel" visible="{!boxVisible}" includeInLayout="{!boxVisible}" paddingTop="4"/>
</s:HGroup>

</s:Group>

For others who come across this later, I made my own custom component that just hides the checkbox for the third state and replaces it with an image that looks like a checkbox in the third state. This is really easy to implement:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Script>
    <![CDATA[
        public static const BOX_CHECKED:int = 1;
        public static const BOX_REJECTED:int = -1;
        public static const BOX_UNCHECKED:int = 0;
        [Bindable] public var boxVisible:Boolean = true;
        [Bindable] public var enum_id:int = -1;
        protected function checkbox_clickHandler(event:MouseEvent):void
        {
            if(!checkbox.selected)
                boxVisible = false;             
        }

        protected function image_clickHandler(event:MouseEvent):void
        {
            boxVisible = true;              
        }

        public function set label(str:String):void {
            checkbox.label = str;
            xLabel.text = str;
        }

        public function get boxState():int {
            if(checkbox.selected)
                return BOX_CHECKED;
            else if(redX.visible)
                return BOX_REJECTED;

            return BOX_UNCHECKED;
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:CheckBox id="checkbox" click="checkbox_clickHandler(event)" visible="{boxVisible}" includeInLayout="{boxVisible}"/>
<s:HGroup>
    <s:Image id="redX" source="@Embed('assets/icons/redX.png')"
         width="16" height="16"
         click="image_clickHandler(event)" 
         visible="{!boxVisible}" includeInLayout="{!boxVisible}"/>
    <s:Label id="xLabel" visible="{!boxVisible}" includeInLayout="{!boxVisible}" paddingTop="4"/>
</s:HGroup>

</s:Group>
总攻大人 2024-08-05 13:11:52

这是我自己的版本,灵感来自詹姆斯的版本。 您可以启用/禁用三态以获得具有相同组件的经典复选框,并且使用的机制有点不同,样板较少。 嵌入图片位于此处
使用风险自负! :)

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         creationComplete="init()">

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

            // STATES :
            /** The intermediate state value. */
            public static const INTERMEDIATE:int = -1;
            /** The unchecked state value. */
            public static const UNCHECKED:int = 0;
            /** The checked state value. */
            public static const CHECKED:int = 1;


            // TRISTATE :
            private var _isTristate:Boolean;
            /** Whether this checkbox is tristate or a normal checkbox. */
            [Bindable] public function get isTristate():Boolean { return _isTristate; }
            public function set isTristate(value:Boolean):void {
                _isTristate = value;
                if(!_isTristate && state == INTERMEDIATE) 
                    state = UNCHECKED;
            }


            // LABEL :
            /** The checkbox label. */
            [Bindable] public var label:String;


            // STATE CHANGE :

            private var _state:int;
            /** The current state of the box. */
            public function get state():int { return _state; }
            public function set state(value:int):void {
                _state = value;
                switch(_state) {
                    case INTERMEDIATE:
                        if(!_isTristate)
                            throw new Error("Setting state to INTERMEDIATE on a non tristate checkbox !");
                        tristateImg.visible = true;
                        checkbox.selected = false;
                        break;

                    case UNCHECKED:
                        tristateImg.visible = false;
                        checkbox.selected = false;
                        break;

                    case CHECKED:
                        tristateImg.visible = false;
                        checkbox.selected = true;
                        break;
                }
            }

            /**
             * Simply sets the state according to the current state.
             */
            protected function changeState(ev:Event):void {
                if(isTristate) ev.preventDefault();
                switch(state) {
                    case INTERMEDIATE: state = CHECKED; break;
                    case UNCHECKED: state = isTristate ? INTERMEDIATE : CHECKED; break;
                    case CHECKED: state = UNCHECKED; break;
                }
            }

            /**
             * Initial state.
             */
            protected function init():void {
                state = UNCHECKED;
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:layout>
        <s:BasicLayout />
    </s:layout>

    <s:CheckBox id="checkbox" label="{label}"
                change="changeState(event)"/>
    <s:Image id="tristateImg"
             mouseEnabled="false" mouseChildren="false"
             source="@Embed('assets/icons/tristate_checkbox.png')"
             x="2" y="5" />

</s:Group>

Here is my own version inspired by James' one. You can enable / disable tristate to get a classic checkbox with the same component, and the mechanism used is a bit different, with less boilerplate. Embedded picture is here.
Use at your own risks ! :)

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         creationComplete="init()">

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

            // STATES :
            /** The intermediate state value. */
            public static const INTERMEDIATE:int = -1;
            /** The unchecked state value. */
            public static const UNCHECKED:int = 0;
            /** The checked state value. */
            public static const CHECKED:int = 1;


            // TRISTATE :
            private var _isTristate:Boolean;
            /** Whether this checkbox is tristate or a normal checkbox. */
            [Bindable] public function get isTristate():Boolean { return _isTristate; }
            public function set isTristate(value:Boolean):void {
                _isTristate = value;
                if(!_isTristate && state == INTERMEDIATE) 
                    state = UNCHECKED;
            }


            // LABEL :
            /** The checkbox label. */
            [Bindable] public var label:String;


            // STATE CHANGE :

            private var _state:int;
            /** The current state of the box. */
            public function get state():int { return _state; }
            public function set state(value:int):void {
                _state = value;
                switch(_state) {
                    case INTERMEDIATE:
                        if(!_isTristate)
                            throw new Error("Setting state to INTERMEDIATE on a non tristate checkbox !");
                        tristateImg.visible = true;
                        checkbox.selected = false;
                        break;

                    case UNCHECKED:
                        tristateImg.visible = false;
                        checkbox.selected = false;
                        break;

                    case CHECKED:
                        tristateImg.visible = false;
                        checkbox.selected = true;
                        break;
                }
            }

            /**
             * Simply sets the state according to the current state.
             */
            protected function changeState(ev:Event):void {
                if(isTristate) ev.preventDefault();
                switch(state) {
                    case INTERMEDIATE: state = CHECKED; break;
                    case UNCHECKED: state = isTristate ? INTERMEDIATE : CHECKED; break;
                    case CHECKED: state = UNCHECKED; break;
                }
            }

            /**
             * Initial state.
             */
            protected function init():void {
                state = UNCHECKED;
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:layout>
        <s:BasicLayout />
    </s:layout>

    <s:CheckBox id="checkbox" label="{label}"
                change="changeState(event)"/>
    <s:Image id="tristateImg"
             mouseEnabled="false" mouseChildren="false"
             source="@Embed('assets/icons/tristate_checkbox.png')"
             x="2" y="5" />

</s:Group>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文