You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Martin Grotzke <ma...@javakaffee.de> on 2007/06/06 21:51:59 UTC

T5: select component with support for attribute "multiple"

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"?

Thx && cheers,
Martin



Re: T5: select component with support for attribute "multiple"

Posted by Martin Grotzke <ma...@javakaffee.de>.
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
     }

Re: T5: select component with support for attribute "multiple"

Posted by Martin Grotzke <ma...@javakaffee.de>.
On Thu, 2007-06-07 at 10:39 +0200, Michael Maier wrote:
> Am 06.06.2007 um 23:00 schrieb Martin Grotzke:
> > I didn't change the type of _value to be able to use the component
> > with or without multiple="true", but perhaps it's better to have a
> > dedicated multiselect component, so the _value could also be a
> > collection as in your implementation...
> 
> yes that was the goal...I need a multiple selection based on  
> sets...so I can now bind the value to a set and I get a modified set  
> back...
on the other hand, with an additional attribute "multiple" but the
_value still as an object, it should be possible to integrate that
in the current Select component, which IMHO would be valuable.

cheers,
Martin


> 
> cheers
> 
> Michael
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
-- 
Martin Grotzke
http://www.javakaffee.de/blog/

Re: T5: select component with support for attribute "multiple"

Posted by Michael Maier <mi...@vivai.de>.
Am 06.06.2007 um 23:00 schrieb Martin Grotzke:

> Do you have getters/setters with a Set then? Does the method signature
> depend on the value?
>
no, I use the value-encoder to map the objects in the set to strings  
and vice versa...
> I didn't change the type of _value to be able to use the component
> with or without multiple="true", but perhaps it's better to have a
> dedicated multiselect component, so the _value could also be a
> collection as in your implementation...

yes that was the goal...I need a multiple selection based on  
sets...so I can now bind the value to a set and I get a modified set  
back...

cheers

Michael



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5: select component with support for attribute "multiple"

Posted by Martin Grotzke <ma...@javakaffee.de>.
On Wed, 2007-06-06 at 22:47 +0200, Michael Maier wrote:
> Yes...it seems that multiple selects are not yet supported, but it is  
> very easy to write a component...
You're right, it's really easy to do this - it took about 30 minutes
and some testing :)

> just take the select component from  
> tapestry, look for "value" and the isOptionValueSelected method. I  
> changed the value to Set type
Do you have getters/setters with a Set then? Does the method signature
depend on the value?

I didn't change the type of _value to be able to use the component
with or without multiple="true", but perhaps it's better to have a
dedicated multiselect component, so the _value could also be a
collection as in your implementation...

Cheers,
Martin


>  and changed the
> 
>      protected void processSubmission(FormSupport formSupport, String  
> elementName) {
> 
>          String[] primaryKey= _request.getParameters(elementName);
>          if( primaryKey != null ) {
>              Set selectedValues= new HashSet();
>              for( String key : primaryKey ) {
> 
>                  selectedValues.add( _encoder.toValue( key ) );
>              } // for
> 
>              try {
>                  _validate.validate(selectedValues);
>                  _value= selectedValues;
> 
>              } catch (ValidationException ex) {
> 
>                  _tracker.recordError(this, ex.getMessage());
>                  return;
>              }
>          } // if
>      }
> 
> method. Now I have a "multiple-select" component which takes a value  
> parameter as a set...and it works fine...
> 
> cheers
> 
> Michael
> 
> Am 06.06.2007 um 21:51 schrieb Martin Grotzke:
> 
> > 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"?
> >
> > Thx && cheers,
> > Martin
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
-- 
Martin Grotzke
http://www.javakaffee.de/blog/

Re: T5: select component with support for attribute "multiple"

Posted by Michael Maier <mi...@vivai.de>.
Yes...it seems that multiple selects are not yet supported, but it is  
very easy to write a component...just take the select component from  
tapestry, look for "value" and the isOptionValueSelected method. I  
changed the value to Set type and changed the

     protected void processSubmission(FormSupport formSupport, String  
elementName) {

         String[] primaryKey= _request.getParameters(elementName);
         if( primaryKey != null ) {
             Set selectedValues= new HashSet();
             for( String key : primaryKey ) {

                 selectedValues.add( _encoder.toValue( key ) );
             } // for

             try {
                 _validate.validate(selectedValues);
                 _value= selectedValues;

             } catch (ValidationException ex) {

                 _tracker.recordError(this, ex.getMessage());
                 return;
             }
         } // if
     }

method. Now I have a "multiple-select" component which takes a value  
parameter as a set...and it works fine...

cheers

Michael

Am 06.06.2007 um 21:51 schrieb Martin Grotzke:

> 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"?
>
> Thx && cheers,
> Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5: select component with support for attribute "multiple"

Posted by Martin Grotzke <ma...@javakaffee.de>.
On Wed, 2007-06-06 at 16:42 -0400, Daniel Jue wrote:
> I'm not sure how that would work with a combobox/dropdown rendering
> (since one click selects an item)
> 
> It would have to render it like a listbox, the way it's done in Palette.
> 
> BTW- I think the Palette super component is ready for use in T5.0.5
> SNAPSHOT.  That should at least give you the same functionality for
> now.
Whooo, also very nice :)

Although, after I added an option to the selected items, it didn't
show it in the right box - and T5 didn't tell me anything in the logs
that s.th. isn't right...
However, it's very nice, but I think for now I stick to the
simple MultiSelect version...

Cheers,
Martin


> 
> On 6/6/07, Martin Grotzke <ma...@javakaffee.de> 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"?
> >
> > Thx && cheers,
> > Martin
> >
> >
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
-- 
Martin Grotzke
http://www.javakaffee.de/blog/

Re: T5: select component with support for attribute "multiple"

Posted by Daniel Jue <te...@gmail.com>.
I'm not sure how that would work with a combobox/dropdown rendering
(since one click selects an item)

It would have to render it like a listbox, the way it's done in Palette.

BTW- I think the Palette super component is ready for use in T5.0.5
SNAPSHOT.  That should at least give you the same functionality for
now.

On 6/6/07, Martin Grotzke <ma...@javakaffee.de> 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"?
>
> Thx && cheers,
> Martin
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org