View Full Version : Ext.menu.CheckItem getChecked()?

25 Apr 2011, 5:20 AM
I'm trying to use an Ext.menu.CheckItem, initialized to false. I've attached a 'click' event handler, at which point I need to determine the status of the CheckItem's checkbox. There doesn't seem to exist, however, a getChecked() method, or a checked or value property. How can I get access to the status of the checkbox without using the 'checkchange' listener?


25 Apr 2011, 8:28 AM
The 'checked' property contains a true/false value, e.g.

var checkedState = someMenuCheckItem.checked;

25 Apr 2011, 8:30 AM
When I go to reference it the first time, even though I intialize it to false, it is "undefined". Clicking on the MenuItem then toggles it correctly to either true or false, but why does it not seem to accept the config option as the initial state of the checked attribute?

25 Apr 2011, 8:40 AM
Has the menu item been rendered prior to querying its checked state?

I coded a quick test of an Ext.menu.CheckItem that seems to work as expected:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="ext/resources/css/xtheme-blue.css" id="theme" />

<script type="text/javascript" src="ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext/ext-all.js"></script>

<script type="text/javascript">
Ext.onReady(function() {
Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';

var p = new Ext.Panel({
height: 400,
title: 'Menu button with CheckItem child',
width: 400,
renderTo: 'contentDiv',
items: [],
xtype: 'button',
text: 'Options',
menu: [{
xtype: 'menucheckitem',
id: 'checkable',
text: 'Checkable Item',
checked: false
<div id="contentDiv"></div>


An yes, the use of a hard-coded ID on the menu item is a Bad Thing; I added it just to make the menu item easily accessible for testing (Ext.getCmp('checkable'), another Bad Thing)...

25 Apr 2011, 8:43 AM
Chances are it has not been rendered the first time, but the odd thing is, after switching to the checkchange handler, this event seems to fire when opening the context menu for the first time (with checked set as undefined). This is not as desired, but I've been able to work around it.

25 Apr 2011, 8:47 AM
I'll bet that a default value of false is assigned during the render and since the initial value is undefined, this triggers the checkchange hander (just guessing).

25 Apr 2011, 8:50 AM
That was my thought as well, but it's odd to me that the checked state would be undefined in the listener, instead of false. Rendering it "false" to start should trigger the checkchange from "undefined" to "false" and report "false" in the checked variable.

But all's well. Just have to do a logic check to make sure the status isn't undefined before performing the rest of my logic.