On Wed, 2007-06-06 at 21:51 +0200, Martin Grotzke wrote: > Hi, > > AFAICS doesn't the select component support the attribute > "multiple", only the first selected option is set on the > value. > > Is there some way to get this to work with support for "multiple"?
I just extended the current Select component class and added support for the attribute "multiple": if you specify multiple="true" in your template, then the select component sets a list of converted values on the value property. There's one issue left: how can I have getters/setters in my page class that do not work with Object but with a List? I just tried this, but then tapestry couldn't find the setter for the value any more... So I have a setter for an Object now and cast this to List - not very nice indeed... Btw: would it be intended to have s.th. like this (support for "multiple") in T5, or is there another approach planned? Cheers, Martin ps. this is the diff: -public final class Select extends AbstractField -{ +public class SelectMultiSupport extends AbstractField { + private class Renderer extends SelectModelRenderer { @@ -63,7 +65,19 @@ private boolean isOptionValueSelected(Object value) { - return value == _value || (value != null && value.equals(_value)); + if (_multiple == null || !_multiple) { + return value == _value || (value != null && value.equals(_value)); + } + else { + if (_value != null) { + for(Object item : (Iterable)_value) { + if (value == item || (value != null && value.equals(item))) { + return true; + } + } + } + return false; + } } private ValueEncoder getEncoder() @@ -124,24 +138,55 @@ @Parameter(required = true, principal = true) private Object _value; + /** The value to read or update. */ + @Parameter(required = false, principal = true) + private Boolean _multiple; + @Override protected void processSubmission(FormSupport formSupport, String elementName) { - String primaryKey = _request.getParameter(elementName); + if (_multiple == null || !_multiple) { - Object selectedValue = _encoder.toValue(primaryKey); + String primaryKey = _request.getParameter(elementName); - try - { - _validate.validate(selectedValue); + Object selectedValue = _encoder.toValue(primaryKey); - _value = selectedValue; + try + { + _validate.validate(selectedValue); + + _value = selectedValue; + } + catch (ValidationException ex) + { + _tracker.recordError(this, ex.getMessage()); + return; + } + } - catch (ValidationException ex) - { - _tracker.recordError(this, ex.getMessage()); - return; + else { + + final String[] primaryKeys = _request.getParameters(elementName); + + final List<Object> selectedValues = new ArrayList<Object>(); + for(String primaryKey : primaryKeys) { + final Object selectedValue = _encoder.toValue(primaryKey); + selectedValues.add( selectedValue ); + } + + try + { + _validate.validate(selectedValues); + _value = selectedValues; + } + catch (ValidationException ex) + { + _tracker.recordError(this, ex.getMessage()); + return; + } + } + } void afterRender(MarkupWriter writer) @@ -151,7 +196,10 @@ void beginRender(MarkupWriter writer) { - writer.element("select", "name", getElementName(), "id", getClientId()); + final Object[] attrs = (_multiple == null || !_multiple) + ? new Object[]{"name", getElementName(), "id", getClientId()} + : new Object[]{"name", getElementName(), "id", getClientId(), "multiple", _multiple}; + writer.element("select", attrs); // Disabled, informals via mixins }
signature.asc
Description: This is a digitally signed message part