0

I have encounter a problem on First toggle inputTextarea editable with Ajax, how to submit typed in value after toggle The UI build on JSF 2 and RichFaces, the backing bean is @ManagedBean /@RequestScoped for now, contain all necessary properties(indicator/reason) and corresponding get/set methods. The app is running on IBM websphere. I was first taken example from here and here.

The myPage.xhtml related section is

<h:form id="myPage">
    <td>
       <h:selectOneMenu value="#{myPage.indicator}">
         <f:selectItem itemValue="Y" itemLabel="YES" />
         <f:selectItem itemValue="N" itemLabel="NO" />
         <f:ajax render="reason" />
       </h:selectOneMenu>
    </td>
    <td><h:inputTextarea id="reason" value="#{myPage.reason}" disabled="#{fn:toLowerCase(myPage.indicator) != 'y'}">
        </h:inputTextarea>
    </td>
    <td>
        <h:commandButton value="Update" id="update"  
           action="#{myPage.update}">
        </h:commandButton>
    </td>
</h:form>

The function here is using selectItem "indicator" to toggle whether inputTextarea "reason" is editable or not. I use <f:ajax> to render, the effect can be seen directly when I toggle on GUI.

When I set "indicator" as Yes, "reason" becomes editable, then I type in some words and click "upload" button(which is a normal post method contain logic to commit form value to database), but the words in "reason" not commit to database.

What I suppose is <f:ajax> set render attribute point to inputTextarea "reason", which means a request already generated when I toggle the selectItem "indicator", but this request hold nothing, because the sequence here is first toggle then available to type in words, the "reason" contain no words at toggle time. And as this inputTextarea "reason" set as ajax render, the normal submit form way trigger by "upload" button also cannot commit its value even I click after type in words. Is this a right guess ?

If this is the right guess, how to solve this issue(e.g how to collect this inputTextarea "reason" value after its become available by ajax toggle) ? Create backing bean listener method with <f:ajax render="reason" listener="...">(and if yes, please show some detail actions I should add for this listener) ? OR any other way(e.g should I go with JavaScript or JQuery to solve) ? If what I did wrong way, please help me to figure out(initial goal is to toggle inputTextarea to editable like ajax effect and then submit the value later typed in).

Community
  • 1
  • 1
Lampard
  • 394
  • 7
  • 23
  • The link you found http://stackoverflow.com/questions/29698008/how-to-disable-enable-jsf-input-field-in-javascript tells to use a `@ViewScoped`, yet you're using a `@RequestScoped`. Why? – BalusC Sep 06 '16 at 19:02
  • Hi, BalusC, actually I try to use ViewScoped replace original RequestScoped, but app doesn't work against that in DEV env, this is not caused by Ajax, since require many other changes and not specific in my workspace(may override other team files),we decide to use javascript, but as you said is right, if can change to ViewScoped, this may work with JSF + Ajax – Lampard Sep 06 '16 at 19:23

1 Answers1

0

Maybe this question is a little fuzzy, I want to re-declare it here as: There is an indicator control a related inputTextarea editable or not, but if I just use Ajax render attribute to control this, I cannot use Ajax to retrieve the later typed in words in this inputTextarea, as ajax is acutally a request send from client to server side, when i toggle the indicator to make inputTextarea editable, the request already send out, but no words contained in request at this time, so I stuck here, can not find way to use Ajax to toggle and send later typed in words.

Still welcome if anyone come to an idea with JSF Ajax way.

I fix the issue with using javascript instead of Ajax, the fixed way is a little interesting, as to simulate Ajax effect, need to trigger javascript function in different cases(a combination usage of same javascript function):

1. First, the javascript to toggle "reason" inputTextarea based on indicator, retrieve these DOM elements based on 'formname:element_id', in my question, already show "indicator" as h:selectOneMenu, and "reason" as h:inputTextarea.

<script type="text/javascript">
   function handleDisabledOrNot() {
      var currVal = document.getElementById('myPage:indicator').value;
      if(currVal == "N") {
         document.getElementById('myPage:reason').disabled = true;
    } else {
         document.getElementById('myPage:reason').disabled = false;
    }
}</script>

2. Then call this function on indicator with onclick event

<td align="left"><!-- Use onclick event to control whether reason disabled or not -->
<h:selectOneMenu id="indicator" value="#{myPage.indicator}" onclick="handleDisabledOrNot()"> 
    <f:selectItem itemValue="Y" itemLabel="YES" />
    <f:selectItem itemValue="N" itemLabel="NO" />
</h:selectOneMenu></td>

3. Finally check the status when load page or work in the page on reason with onblur / onfocus event, to make sure full-scale detect indicator status in the view, otherwise, it will miss status checking in either way. E.g, when first time load the page, if missing check status with onfocus event, if you move mouse into reason inputTextarea, no matter what indicator is Y or N, it is editable initially, to avoid such case, should use onfocus event to check, and vice versa to use onblur event.

<!-- Use both onblur and onfocus event to detect indicator status full-scale on view -->
<h:inputTextarea id="reason" value="#{myPage.reason}" onblur="handleDisabledOrNot()" onfocus="handleDisabledOrNot()">
</h:inputTextarea></td>

Finally, it shows the same effect as Ajax render way. As the link i quote in my question, I know in JSF, the best solution is using Ajax to work with backing bean, but as I don't know how to implement after Ajax toggle, how to collect data in inputTextArea later typed in, I can not come with solution on Ajax way now. So, still welcome some idea based on Ajax, if not, just show purely javascript way here.

Lampard
  • 394
  • 7
  • 23