How do I set a CheckBox to indeterminate state?

221 views
Skip to first unread message

Greg Dougherty

unread,
Jul 22, 2015, 6:04:19 PM7/22/15
to GWT-Bootstrap

Have a CheckBox in a Tree with other CheckBoxes below it.  I'd like to set the CheckBox to the "indeterminate" state when some, but not all, of the CheckBoxes below it are checked.

In JavaScript you'd do this:

var checkall = document.getElementById('checkall');

        checkall.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;

Short of inlining some JAvaScript, is there any way to do this with GWT Bootstrap?

Thank you,
Greg

Greg Dougherty

unread,
Jul 23, 2015, 12:38:32 PM7/23/15
to GWT-Bootstrap, gre...@gmail.com
I wrote the following code, which "works", in that checkbox.indeterminate is set to true.  However, the checkbox still displays as plain unselected. (Tested under Chrome's debugger, so I know the status was set).

Any idea why it's not displaying correctly?
   
    /**
     * JavaScript to give an ID'd element in a form the focus
     *
     * @param checkbox        Element to manipulate
     * @param indeterminate    True if indeterminate, false if not
     */
    public static native void setIndeterminate (Element checkbox, boolean indeterminate) /*-{
        checkbox.indeterminate = indeterminate;
    }-*/;

Greg Dougherty

unread,
Jul 27, 2015, 12:32:41 PM7/27/15
to GWT-Bootstrap, gre...@gmail.com
It turns out the fix is simple:

1: Make sure to import the correct "Element"

import com.google.gwt.dom.client.Element;

2: For both GWT and GWT Bootstrap, a Checkbox is a span, whose first element is the input checkbox. 

            Element    element = checkbox.getElement ();
            Element    child = element.getFirstChildElement ();
            if (child != null)
                element = child;
        setIndeterminate (element, indeterminate);

Works.
Reply all
Reply to author
Forward
0 new messages