Is there a good way to get a tri-state checkbox in flex?
Asked Answered
N

4

7

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.

Novitiate answered 4/8, 2009 at 12:18 Comment(0)
M
4

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

Mizzenmast answered 4/8, 2009 at 12:49 Comment(1)
Just a note of caution, this custom widget for 3 state doesn't support Accessibility.Upbraid
C
2

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>
Chapatti answered 17/8, 2011 at 16:47 Comment(0)
B
1

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>
Bannasch answered 8/5, 2013 at 9:35 Comment(0)
B
0

Take a look at http://srinichekuri.wordpress.com/2011/05/20/3-state-checkbox-for-headerrenderer-in-datagrid/.

It has the code for 3 state check box.

Bil answered 24/2, 2014 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.