Ok, here is a solution. Took me 5 minutes to write, 20 mins to test. ;-)
> Ulrich Stark wrote:
>> Nick,
>>
>> there is no difference whether your event method gets triggered upon
>> clicking a link or a button. Except that you can pass parameters like
>> an index when using actionlink. And that's exactly what you want.
>>
As I said, not if you want your form input fields sent/preserved.
Francois Armand wrote:
> My use case was : I have a form with a list of text field, for each
> field a delete (link|button), and a submit button.
> I want that if an user modify some fields and then click to delete
> (without submitting the form), the matching value is deleted _and_ other
> modification are kept.
I've just tested that this works with my SubmitContext component below.
> To achieve that, the field values have to be transmitted to the server,
> what is not done with an action link.
Exactly, we need a submit ...
> So, Nick Westgate point out the good solution (I think), I just need to
> be done ;)
>
> Just to be sure : there is no other solution than to use javascipt to do
> that ? I was thinking that as Tapestry handle the form action link, it
> would be possible to add a context to submit button, as example :
The Javascript was used to assign a "tag" (like a context) to a shared
hidden input and then submit the form. But I found another way - just use
a hidden input for each submit.
> <:tform>
> ....
> <t: submit t:id="context" context="value">
> <t: submit t:id="submit">
> </t:form>
>
> With submit from "submit" button mapped to action link
> myformcomponent.form and "context" button mapped to
> myformcomponent.form/value, and in Java code, context available as now
> for action link : onSelectedFromContext(String value) {...}
Yeah, this is exactly what I have implemented. Eventually a shared hidden
input should probably be used, but I can't see (or perhaps grok) the form
support for this yet. Also a context that is a list of objects was my plan,
but the context "passivation" is all internal and relies on the TypeCoercer
stuff which is a bit involved. This should do for a first cut.
Diff it to Submit and you'll see it's only a few extra lines. Hope it helps.
Cheers,
Nick.
// Copyright 2007 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package your.package.tapestry.components;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.Environmental;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.corelib.base.AbstractField;
import org.apache.tapestry.services.FormSupport;
import org.apache.tapestry.services.Heartbeat;
import org.apache.tapestry.services.Request;
/**
* Corresponds to <input type="submit">, a client-side element that can
force the
* enclosing form to submit. The submit responsible for the form submission
will post a
* notification that allows the application to know that it was the responsible
entity. The
* notification is named "selected" and has a String context.
*/
public final class SubmitContext extends AbstractField
{
static final String SELECTED_EVENT = "selected";
/**
* If true, then any notification sent by the component will be deferred
until the end of
* the form submission (this is usually desirable).
*/
@Parameter
private boolean _defer = true;
@Parameter
private String _context;
@Environmental
private FormSupport _formSupport;
@Environmental
private Heartbeat _heartbeat;
@Inject
private ComponentResources _resources;
@Inject
private Request _request;
public SubmitContext()
{
}
SubmitContext(Request request)
{
_request = request;
}
void beginRender(MarkupWriter writer)
{
// write a hidden input for the context
String elementName = getElementName();
writer.element("input", "type", "hidden", "name", elementName + "X",
"value", _context);
writer.end();
// now the submit
writer.element("input", "type", "submit", "name", elementName, "id",
getClientId());
}
void afterRender(MarkupWriter writer)
{
writer.end();
}
@Override
protected void processSubmission(FormSupport formSupport, String
elementName)
{
String value = _request.getParameter(elementName);
final String context = _request.getParameter(elementName + "X");
if (value == null)
return;
Runnable sendNotification = new Runnable()
{
public void run()
{
_resources.triggerEvent(SELECTED_EVENT, new Object[] {context},
null);
}
};
// When not deferred, don't wait, fire the event now (actually, at the
end of the current
// heartbeat). This is most likely because the Submit is inside a Loop
and some contextual
// information will change if we defer. Another option might be to wait
until the next
// heartbeak?
if (_defer)
_formSupport.defer(sendNotification);
else
_heartbeat.defer(sendNotification);
}
// For testing:
void setDefer(boolean defer)
{
_defer = defer;
}
void setup(ComponentResources resources, FormSupport support, Heartbeat
heartbeat)
{
_resources = resources;
_formSupport = support;
_heartbeat = heartbeat;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]