Note that there are some explanatory texts on larger screens.

plurals
  1. POGXT Combobox with unselectable categories
    primarykey
    data
    text
    <p>I am trying to do a ComboBox with <strong>options</strong> and <strong>categories</strong> in GXT 3.</p> <p>I can use a <code>ComboBoxCell</code> to have a different display for options and categories.</p> <p>Problem : I need the categories to be <strong>unselectable</strong> (like with HTML tag <code>&lt;optgroup&gt;</code>).</p> <p>Here is a plain old HTML comboBox (<code>&lt;select&gt; tag</code>)</p> <p><strong>HTML CODE:</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;select&gt; &lt;optgroup label="category 1"&gt; &lt;option name="option1"&gt;Option 1&lt;/option&gt; &lt;option name="option2"&gt;Option 2&lt;/option&gt; &lt;option name="option3"&gt;Option 3&lt;/option&gt; &lt;/optgroup&gt; &lt;optgroup label="category 2"&gt; &lt;option name="option4"&gt;Option 4&lt;/option&gt; &lt;option name="option5"&gt;Option 5&lt;/option&gt; &lt;option name="option6"&gt;Option 6&lt;/option&gt; &lt;/optgroup&gt; &lt;/select&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>As you may see on <a href="http://jsfiddle.net/pgJQZ/" rel="nofollow">this fiddle</a>, it produces a ComboBox with <strong>unselectable</strong> categories. I am trying to do the equivalent with GXT.</p> <p>Any idea?</p> <p>EDIT:</p> <p>I finally did my own implementation : </p> <pre><code>import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.ui.UIObject; import com.google.gwt.user.client.ui.Widget; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; public class Select&lt;T&gt; extends Widget { private static final String TAG_OPTGROUP = "optgroup"; private static final String ATTR_NAME = "name"; private static final String PROPERTY_SELECTED = "selected"; private Element select; private Map&lt;Element, T&gt; mapElement; public Select(boolean multiple){ this.mapElement=new HashMap&lt;Element, T&gt;(); this.select = DOM.createSelect(multiple); this.setElement(this.select); } public Select(){ this(false); } public OptGroup addGroup(String displayName){ OptGroup optGroup=new OptGroup(displayName); this.select.appendChild(optGroup.getElement()); return optGroup; } public void addOption(T t,String displayName){ Element option=DOM.createOption(); option.setAttribute(ATTR_NAME,t.toString()); option.setInnerText(displayName); this.select.appendChild(option); this.mapElement.put(option,t); } public T getSelectedValue(){ for(Map.Entry&lt;Element,T&gt; entry:mapElement.entrySet()){ if(entry.getKey().getPropertyBoolean(PROPERTY_SELECTED)){ return entry.getValue(); } } return null; } public Collection&lt;T&gt; getSelectedValues(){ Collection&lt;T&gt; result=new HashSet&lt;T&gt;(); for(Map.Entry&lt;Element,T&gt; entry: mapElement.entrySet()){ if(entry.getKey().getPropertyBoolean(PROPERTY_SELECTED)){ result.add(entry.getValue()); } } return result; } public class OptGroup extends UIObject { private static final String ATTR_LABEL="label"; private String name; private Element element; private OptGroup(String name) { this.name = name; this.element = DOM.createElement(TAG_OPTGROUP); this.element.setAttribute(ATTR_LABEL,name); this.setElement(this.element); } public String getName() { return name; } public void addOption(T t,String displayName){ Element option=DOM.createOption(); option.setAttribute(ATTR_NAME,t.toString()); option.setInnerText(displayName); this.element.appendChild(option); mapElement.put(option,t); } } } </code></pre> <p>This is perfectible as I use <code>toString()</code> for the attribute <code>name</code> of the <code>&lt;option&gt;</code> tag but it meets my need ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload