tags:

views:

83

answers:

3

So I have an inputText that has its value hook to myBean.text, I want that if I click enter/return key, the inputText will invoke a method inside myBean to do something. Can any one help me?

EDIT

<p:inplace label="Add Comment">
    <h:inputText id="keyword" value="#{PostComment.comment.comment}" style="width:100%;"
                 onkeypress="if (event.keyCode == 13){onchange(); return false;}">
                 <f:ajax event="change" listener="#{PostComment.postCommentListener}"/>
    </h:inputText>
    <p:watermark for="keyword" value="Comment here"/>
</p:inplace> 

then in PostComment bean

public void postCommentListener(AjaxBehaviorEvent event){
    NewsFeed c = scholar.getModel().getRowData();
    postProfileComment(c.getTargetFeedId());
}
+1  A: 

The easiest way is to put the inputText in a form and hide a commandButton next to it.

For example:

<h:form>
  <h:inputText styleClass="myText" value="#{myBean.text}"/>
  <h:commandButton styleClass="myButton" action="#{myBean.myMethod}" style="display:none;" value="submit"/>
</h:form>

UPDATE:

If you are using Seam you can use the <s:defaultAction/> tag. This makes that commandButton that contains it the one that responds to the ENTER.

<h:commandButton class="myButton" action="#{myBean.myMethod}" style="display:none;" value="submit">
  <s:defaultAction/>
</h:commandButton>

If you aren't using Seam you could try one of the similar defaultAction controls

Or you could roll your own with a bit of Javascript; ideally jQuery. For example:

$('input.myText').keypress(function(e) {
    if (e.which == 13) {
        $('.myButton').click();
    }
});
Damo
On the very outside, I have a `form` already, so I cant have another `form` here. And unlucky enough, I have another commandButton as well. So when I press enter/return, is there a way to tell which commandButton to execute?
Harry Pham
I am gonna try your jQuery suggestion since I am not using Seam. So I just paste the code inside my header, inside the `<script type="text/javascript"></script>` correct? Is `myText` the `id` of my `inputText`? I put an alert inside the if statement, and the alert does not get generated, any idea?
Harry Pham
Remember that JSF changes the IDs to include the form. You're better off sticking with the class. In this case "myText" is the class.
Damo
+1  A: 

Hi, you can probably do something like this:

<h:inputText value="123" onkeyup="if(event.keyCode==13)this.blur();" onchange="document.getElementById('fakebutton').click();" valueChangeListener="#{yourbean.dosomething}"/>
<h:commandButton id="fakebutton"actionListener="#{yourbean.fakeaction}"/>

inside your bean:

public void dosomething(ValueChangeEvent event)
{
    System.out.println("I did something");
}
public void fakeaction(ActionEvent event)
{
    System.out.println("I do no nothing");
}
lkdg
+1  A: 

As per your question history, I know that you're using JSF 2.0, so here's a JSF 2.0 targeted answer: use <f:ajax> which listens on a change event and use keypress event to invoke it when the enter key is pressed (keycode 13).

<h:inputText value="#{bean.text1}" 
    onkeypress="if (event.keyCode == 13) { onchange(); return false; }">
    <f:ajax event="change" listener="#{bean.listener}" />
</h:inputText>

The #{bean.listener} should point to a method like

public void listener(AjaxBehaviorEvent event) {
    // ...
}
BalusC
if I want to pass in a parameter to `#{bean.listener}`, usually I just do `#{bean.listener(param)}`, but since the first parameter is taken by `AjaxBehaviorEvent`, does it mean I have to use `f:param`?
Harry Pham
What would you like to pass? Isn't that parameter just already (in)directly in the bean?
BalusC
You can think of a `comment - reply` system like `facebook`. All the comments are display in a dataTable, with `var="item"`. In addition, each row has their own dataTable (`var="item2"`) representing the **replies** for that specific comment. So the inputText inject `reply` directly into the second dataTable, therefore I need to pass in the `id` of the row, **#{item.id}**, to whatever the method I execute. So that once I display, I know that this comment `X` is the reply to `Y`.
Harry Pham
`DataModel#getRowData()`.
BalusC
ohhh yeah, that will work. Let me try that
Harry Pham
BalusC, can u check the syntax. The inputText, when I press enter, nothing happen. It does not call my listener function. I update my post with some codes, will u take a look for me please?
Harry Pham
Does the generated HTML look right? I haven't tested this in combination with PrimeFaces components.
BalusC
I took out all the primefaces component. Still does not work. Here is the thing, if I put an `alert` inside the if statement of `onkeypress`, then the alert pop up when I press enter. So i try to replace your codes inside the `if` to `$('#myButton').click();`, where `myButton` is an `id` of a `h:commandButton style="display:none"` that invoke an action. Sadly, it still does not work. Any other idea?
Harry Pham
I think I just gonna do myself a big favor and adding in a button and save any more headache. Thank you guys
Harry Pham
You're using jQuery? Try `$(this).change()` instead of `onchange()`. There might be browser specific behaviours on `onchange()` I am not aware of. Which browser are you using?
BalusC