You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Krishna Mohan <k....@gmail.com> on 2012/01/18 11:12:52 UTC

Doing an action on press of key in a form

I have a form, few panels in it.

In one of the panel i have few fields and in other panel a search button.

I have requirement wherein after entering text in the field on press of "Enter" 
button search should be performed with out click of search button.

To achieve this i tried following snippet

 add(new AjaxEventBehavior("onkeypress")
      {
        @Override protected CharSequence getCallbackScript(boolean 
onlyTargetActivePage)
        {
          return generateCallbackScript("wicketAjaxGet('" + 
getCallbackUrl(onlyTargetActivePage) + "&" + KEYPRESS_PARAM + 
"='+wicketKeyCode(event)");
        }


        @Override protected void onEvent(AjaxRequestTarget target)
        {
          String paramValue = 
RequestCycle.get().getRequest().getParameter(KEYPRESS_PARAM);
  int key = Integer.parseInt(paramValue);
            if (key == 13)
            {
               submitSearchForm();
             }
        }    

    });
but using above the text in the field is cleared and get wrong search. but when 
i remove focus from the field and press "Enter" i get desired result.

My question here is how to perform search, remaining in the field(with focus in 
the field)

or their any other approach to get my requirement 

i also tried
 Form form = findParent(Form.class);
    if (form != null)
    {
      form.setDefaultButton(searchButton);
    }

but form is always null for me

Regards
Krishna



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


Re: Doing an action on press of key in a form

Posted by Krishna Mohan <k....@gmail.com>.
> the form should be reachable in #onInitialize(). try again
 

No it is not reachable




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


Re: Doing an action on press of key in a form

Posted by Martin Grigorov <mg...@apache.org>.
On Thu, Jan 19, 2012 at 1:56 PM, Krishna Mohan
<k....@gmail.com> wrote:
>
>> Put this code in PanelWithTheButton#onInitialize()
>
>
>
> i did the same but it did not work

the form should be reachable in #onInitialize(). try again

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



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

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


Re: Doing an action on press of key in a form

Posted by Krishna Mohan <k....@gmail.com>.
> Put this code in PanelWithTheButton#onInitialize()



i did the same but it did not work







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


Re: Doing an action on press of key in a form

Posted by Martin Grigorov <mg...@apache.org>.
On Thu, Jan 19, 2012 at 1:31 PM, Krishna Mohan
<k....@gmail.com> wrote:
> Martin Grigorov <mgrigorov <at> apache.org> writes:
>
>>
>> I'm not sure what Igor meant with this suggestion.
>>
>> I think .setDefaultButton() should do what you need.
>>
>> Here is another suggestion from me:
>>
>> Add a non-Ajax behavior to the text field which adds 'keyup' event
>> handler. When the user types something in the field then check the
>> keyCode from the event and if it is 13 then call
>> 'document.getElementById("submitButtonId").click()'.
>
> I describe my problem agin
>
> I have a form and few panels in it.
>
> In panel 'A' i have search button , panle 'A' consist of set of other panels
> 'B'.
> panel 'B' has few fields for example field 'F'
>
> Each panel is implemented in sepearate java class
>
> I have requirement wherein after entering text in the field 'F' on press of
> "Enter"
> button search should be performed with out click of search button.
>
> To achieve this i tried following snippet
>
>  add(new AjaxEventBehavior("onkeypress")
>      {
>        @Override protected CharSequence getCallbackScript(boolean
> onlyTargetActivePage)
>        {
>          return generateCallbackScript("wicketAjaxGet('" +
> getCallbackUrl(onlyTargetActivePage) + "&" + KEYPRESS_PARAM +
> "='+wicketKeyCode(event)");
>        }
>
>        @Override protected void onEvent(AjaxRequestTarget target)
>        {
>          String paramValue =
> RequestCycle.get().getRequest().getParameter(KEYPRESS_PARAM);
>  int key = Integer.parseInt(paramValue);
>            if (key == 13)
>            {
>               submitSearchForm();
>             }
>        }
>
>    });
> but using above the text in the field is cleared and get wrong search. but when
> i remove focus from the field and press "Enter" i get desired result.
>
> My question here is how to perform search, remaining in the field(with focus in
> the field)
>
> or their any other approach to get my requirement
>
> i also tried
>  Form form = findParent(Form.class);
>    if (form != null)
>    {
>      form.setDefaultButton(searchButton);
>    }

Put this code in PanelWithTheButton#onInitialize()

>
> but form is always null for me
>
> i also tried Igor's idea
>
> i get below error on using AjaxFormComponentUpdatingBehavior
>
> WicketMessage: Behavior
> nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only be added
> to an instance of a FormComponentRoot
> cause:org.apache.wicket.WicketRuntimeException: Behavior
> nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only be added
> to an instance of a FormComponent
>
> Regards
> Krishna
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

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


Re: Doing an action on press of key in a form

Posted by Krishna Mohan <k....@gmail.com>.
Martin Grigorov <mgrigorov <at> apache.org> writes:

> 
> I'm not sure what Igor meant with this suggestion.
> 
> I think .setDefaultButton() should do what you need.
> 
> Here is another suggestion from me:
> 
> Add a non-Ajax behavior to the text field which adds 'keyup' event
> handler. When the user types something in the field then check the
> keyCode from the event and if it is 13 then call
> 'document.getElementById("submitButtonId").click()'.

I describe my problem agin

I have a form and few panels in it.

In panel 'A' i have search button , panle 'A' consist of set of other panels 
'B'.
panel 'B' has few fields for example field 'F' 

Each panel is implemented in sepearate java class

I have requirement wherein after entering text in the field 'F' on press of 
"Enter" 
button search should be performed with out click of search button.

To achieve this i tried following snippet

 add(new AjaxEventBehavior("onkeypress")
      {
        @Override protected CharSequence getCallbackScript(boolean 
onlyTargetActivePage)
        {
          return generateCallbackScript("wicketAjaxGet('" + 
getCallbackUrl(onlyTargetActivePage) + "&" + KEYPRESS_PARAM + 
"='+wicketKeyCode(event)");
        }

        @Override protected void onEvent(AjaxRequestTarget target)
        {
          String paramValue = 
RequestCycle.get().getRequest().getParameter(KEYPRESS_PARAM);
  int key = Integer.parseInt(paramValue);
            if (key == 13)
            {
               submitSearchForm();
             }
        }    

    });
but using above the text in the field is cleared and get wrong search. but when 
i remove focus from the field and press "Enter" i get desired result.

My question here is how to perform search, remaining in the field(with focus in 
the field)

or their any other approach to get my requirement 

i also tried
 Form form = findParent(Form.class);
    if (form != null)
    {
      form.setDefaultButton(searchButton);
    }

but form is always null for me

i also tried Igor's idea

i get below error on using AjaxFormComponentUpdatingBehavior 

WicketMessage: Behavior 
nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only be added 
to an instance of a FormComponentRoot 
cause:org.apache.wicket.WicketRuntimeException: Behavior 
nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only be added 
to an instance of a FormComponent

Regards
Krishna 



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


Re: Doing an action on press of key in a form

Posted by Martin Grigorov <mg...@apache.org>.
I'm not sure what Igor meant with this suggestion.

I think .setDefaultButton() should do what you need.

Here is another suggestion from me:

Add a non-Ajax behavior to the text field which adds 'keyup' event
handler. When the user types something in the field then check the
keyCode from the event and if it is 13 then call
'document.getElementById("submitButtonId").click()'.

On Thu, Jan 19, 2012 at 10:07 AM, Krishna Mohan
<k....@gmail.com> wrote:
>
> Yes i understood that, but how to solve my original problem
>
> Regards
> Krishna
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

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


Re: Doing an action on press of key in a form

Posted by Krishna Mohan <k....@gmail.com>.
Yes i understood that, but how to solve my original problem

Regards
Krishna


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


RE: Doing an action on press of key in a form

Posted by Michal Wegrzyn <mi...@onior.com>.
AjaxFormComponentUpdatingBehavior must be used on a FormComponent (sub)class.

Best Regards,
Michal Wegrzyn

> -----Original Message-----
> From: Krishna Mohan [mailto:k.krishnamohan.r@gmail.com]
> Sent: Thursday, January 19, 2012 9:32
> To: users@wicket.apache.org
> Subject: Re: Doing an action on press of key in a form
> 
> i get below error on using AjaxFormComponentUpdatingBehavior
> 
> WicketMessage: Behavior
> nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only
> be added
> to an instance of a FormComponentRoot
> cause:org.apache.wicket.WicketRuntimeException: Behavior
> nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only
> be added
> to an instance of a FormComponent
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org


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


Re: Doing an action on press of key in a form

Posted by Krishna Mohan <k....@gmail.com>.
i get below error on using AjaxFormComponentUpdatingBehavior 

WicketMessage: Behavior 
nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only be added 
to an instance of a FormComponentRoot 
cause:org.apache.wicket.WicketRuntimeException: Behavior 
nl.planon.hera.filters.quicksearch.PnWebAdvancedSearchPanel$3 can only be added 
to an instance of a FormComponent


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


Re: Doing an action on press of key in a form

Posted by Igor Vaynberg <ig...@gmail.com>.
use FormComponentUpdatingBehavior instead

-igor

On Wed, Jan 18, 2012 at 2:12 AM, Krishna Mohan
<k....@gmail.com> wrote:
> I have a form, few panels in it.
>
> In one of the panel i have few fields and in other panel a search button.
>
> I have requirement wherein after entering text in the field on press of "Enter"
> button search should be performed with out click of search button.
>
> To achieve this i tried following snippet
>
>  add(new AjaxEventBehavior("onkeypress")
>      {
>        @Override protected CharSequence getCallbackScript(boolean
> onlyTargetActivePage)
>        {
>          return generateCallbackScript("wicketAjaxGet('" +
> getCallbackUrl(onlyTargetActivePage) + "&" + KEYPRESS_PARAM +
> "='+wicketKeyCode(event)");
>        }
>
>
>        @Override protected void onEvent(AjaxRequestTarget target)
>        {
>          String paramValue =
> RequestCycle.get().getRequest().getParameter(KEYPRESS_PARAM);
>  int key = Integer.parseInt(paramValue);
>            if (key == 13)
>            {
>               submitSearchForm();
>             }
>        }
>
>    });
> but using above the text in the field is cleared and get wrong search. but when
> i remove focus from the field and press "Enter" i get desired result.
>
> My question here is how to perform search, remaining in the field(with focus in
> the field)
>
> or their any other approach to get my requirement
>
> i also tried
>  Form form = findParent(Form.class);
>    if (form != null)
>    {
>      form.setDefaultButton(searchButton);
>    }
>
> but form is always null for me
>
> Regards
> Krishna
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

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