OPTGROUP

513 views
Skip to first unread message

Karl

unread,
Aug 28, 2008, 3:15:55 AM8/28/08
to Google Web Toolkit
How can i create a optgroup inside a combobox?

thanks,
Karl

James Scott

unread,
Oct 21, 2008, 9:15:07 PM10/21/08
to Google Web Toolkit
On Aug 28, 3:15 am, Karl <ben...@gmail.com> wrote:
> How can i create aoptgroupinside a combobox?

ListBox does not have an optgroup API, so you need to get the select
element from your ListBox and use the DOM API to add the optgroup.

Something like this:
List<String> options = new ArrayList<String>();
//omitted: fill options with the strings you want to see
in the select list
ListBox lb = new ListBox();
SelectElement select = lb.getElement().cast();
OptGroupElement groupElement =
Document.get().createOptGroupElement();
groupElement.setLabel("My group");

for (String option : options) {
OptionElement optElement =
Document.get().createOptionElement();
//setText results in blank options in IE6/7, use
setInnerText
optElement.setInnerText(option.getName());
groupElement.appendChild(optElement);
}

select.appendChild(groupElement);

Hope that helps,

James
Reply all
Reply to author
Forward
0 new messages