Skip to main content

Get Combo Box Item Id in Glue.js file

13 replies [Last post]
hmistry
Offline
Joined: 2008-08-04

I have a Combo Box (dojo.Combobox) and a Command Button.
1) In combox i have values [{A,1},{B,2},{C,3}]
2) User Select any item from the combo box
3) On Clicking the command button in the onClick event of the command button i need to now the Id of the selected Item in the combo box.

Eg. If B is selected i need to get the value 2 in a variable.
I am able to get the value that is B using

s1 = jmaki.attributes.get('cboa').getvalue();

but not the ID.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
gmurray71
Offline
Joined: 2003-07-31

Hi,

Assuming in your JSP page you have:

value="[
{label : 'Alabama', value : 'AL'},
{label : 'California', value : 'CA'},
{label : 'New York', value : 'NY', selected : true},
{label : 'Texas', value : 'TX'}
]" />

This is really easy with a Yahoo / Dojo Button.

The button by publishes to "/yahoo/button/onClick" so all you need to do is in your glue.js file subscribe:

// map button publish /onClick to the handler
jmaki.subscribe("/yahoo/button/onClick", function(args) {
// get reference to the combobox widget
var _combobox = jmaki.getWidget("cboa");
// log the changes
jmaki.log("**glue.js : onClick request from: " + jmaki.inspect(_combobox.getValue()));
});

In order to see the changes I turned on the logger with:

jmaki.debug = true;
jmaki.debugGlue = true;

Enjoy,

-Greg

gmurray71
Offline
Joined: 2003-07-31

Hi,

Right now the Dojo Combox Doesn't publish the item id that was selected.

I have updated the Dojo Combobox to do what you want but you will need to update your widget to:

https://widgets.dev.java.net/source/browse/*checkout*/widgets/trunk/code...

Just replace the /dojo/combobox/component.js with the version above.

Then in your glue use the following:

jmaki.subscribe("/dojo/dijit/button/onClick", function(args) {
var _combobox = jmaki.getWidget("cboa");
jmaki.log("**glue.js : onClick request from: " + jmaki.inspect(_combobox.getSelectedItem()));
});

Notice I've added a _combobox.getSelectedItem(); which will give you the currently selected item. The property "targetId" is the ID of the item that was selected.

-Greg

hmistry
Offline
Joined: 2008-08-04

Hi
I have downloaded the latest component but
my problem is i am a bit new to Java.net
i tried using
A1=jmaki.getwidget("cbosearchtype").getselecteditem();

but it give it as a object for getting the target id i tried
A1=jmaki.getwidget("cbosearchtype").getselecteditem().targetid;

but was getting a1 as undefined

gmurray71
Offline
Joined: 2003-07-31

In your JSP page do you have the id on the widget?

value="[
{label : 'Alabama', value : 'AL'},
{label : 'California', value : 'CA'},
{label : 'New York', value : 'NY', selected : true},
{label : 'Texas', value : 'TX'}
]" />

Also calls need to be case sensitive:

A1=jmaki.getwidget("cbosearchtype").getselecteditem();

Should be:

A1=jmaki.getWidget("cbosearchtype").getSelectedItem();

-Greg

hmistry
Offline
Joined: 2008-08-04

value="[{'label' : 'D1', 'value' : '-1'},
{'label' : 'D2', 'value' : '-2'},
{'label' : 'D3', 'value' : '0', 'selected' : true}]"/>

In the glue File it is
A1 =jmaki.getWidget("cboSearchType").getSelectedItem().targetid;

This is how it is

gmurray71
Offline
Joined: 2003-07-31

Try adding id properties to the values:

value="[{'label' : 'D1', 'value' : '-1', id : 'd1'},
{'label' : 'D2', 'value' : '-2', id : 'd2'},
{'label' : 'D3', 'value' : '0', 'selected' : true, id : 'd3'}]"/>

Are you seeing it now?

-Greg

hmistry
Offline
Joined: 2008-08-04

yeap i have taht too bit same result
A1 i am getting as undefined

gmurray71
Offline
Joined: 2003-07-31

what does it say when you use the following:

jmaki.inspect(jmaki.getWidget("cboSearchType").getSelectedItem(), 3);

-Greg

hmistry
Offline
Joined: 2008-08-04

"{label : 'Both' , targetId : 'd3' , action : undefined}"

hmistry
Offline
Joined: 2008-08-04

Sorry its
"{label : 'D3' , targetId : 'd3' , action : undefined}"

gmurray71
Offline
Joined: 2003-07-31

Oh it's a case sensitive thing.

targetId should work

jmaki.inspect and firebug are good at finding these types of problems.

So you are good?

-G

hmistry
Offline
Joined: 2008-08-04

A1 =jmaki.getWidget("cboSearchType").getSelectedItem().targetId;

Gr8
Many thanks Mate
I also realised it as soon as i saw it in the results genereted from the inspect command

Its now working

gmurray71
Offline
Joined: 2003-07-31

I had the same problem so many times I created the inspect because I'm not always on Firefox. It's a great API when you are on IE especially.

Glad things are working.

-Greg