Hi,

I had an issue with triggering a form fragment with a radio button. I
came up with the following solution, and thought I'd share and ask for
comments - perhaps it is possible to come up with something that can
be included in Tapestry.

The .tml looks as follows:

        <t:form t:id="create">
                <t:radiogroup t:id="createAction" value="action"
                        encoder="actionEncoder">
                        <t:radio t:id="copy" t:mixins="radiotriggerfragment"
                                fragment="copyfragment" />
                        <t:label for="copy" />
                        <t:radio t:id="new" />
                        <t:label for="new" />
                </t:radiogroup>

                <t:formfragment t:id="copyfragment" visible="true">
                        This is the copy form fragment
                </t:formfragment>
        </t:form>

This required the following mixin (mostly a copy of TriggerFragment.java):

/**
 * Deal with using a radio button to make a form fragment visible/invisible
 */
public class RadioTriggerFragment {

        /** The container for this mixin */
        @InjectContainer
        private Field container_;

        /**
         * The [EMAIL PROTECTED] 
org.apache.tapestry.corelib.components.FormFragment} instance
         * to make dynamically visible or hidden.
         */
        @Parameter(name = "fragment", required = true, defaultPrefix = 
"component")
        private ClientElement fragment_;

        /** Page render support to get unique client side ids and generate 
links */
        @Environmental
        private PageRenderSupport renderSupport_;

        /** The heartbeat */
        @Environmental
        private Heartbeat heartbeat_;

        /**
         * Render the javascript call to deal with the radio button 
check/uncheck
         */
        void beginRender() {
                Runnable r = new Runnable() {
                        @SuppressWarnings("synthetic-access")
                        public void run() {
                                renderSupport_.addScript(
                                                
"aplLinkRadioButtonToFormFragment('%s', '%s');",
                                                container_.getClientId(), 
fragment_.getClientId());
                        }
                };

                // Defer generating the script to ensure that the FormFragment 
has
                // rendered
                // and generated its client id.

                heartbeat_.defer(r);
        }
}

and the following bit of javascript:

function aplLinkRadioButtonToFormFragment(radio, form) {
        radio = $(radio);
        // Get the name on this radio button
        var allRadios = radio.form[radio.name];
        for (var i=0; i<allRadios.length; i++)
                allRadios[i].observe("click", function() {
                        $(form).formFragment.setVisible(radio.checked);
                });
}

There does not seem to be a different way but to listen for events on
all radio buttons in a radio group.

Cheers,

Adriaan

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to