سؤال

I have a trinidad table with one column contains drop downs (select one choice) for each row and another column contains an input text for each row.

When a particular option is selected, the input becomes editable (else it remains read only).

The read only behaviour and the partial event are working perfectly as expected. The problem is whenever the input text is supposed to move from editable to read only, I still see the value previously entered when the input text was editable.

From here if I select another option (for which the input text would also be read only) the value goes away.

The JSP Code is as follows:

<tr:table emptyText="No Data" var="vo" autoSubmit="true" partialTriggers="accountType"     value="#{testingBean.voList}" binding="#{testingBean.table}" width="50%">
<tr:column>
    <f:facet name="header">
        <tr:outputText value="Sl No" />
    </f:facet>
    <tr:outputText value="#{vo.slNo}" />
</tr:column>
<tr:column>
    <f:facet name="header">
        <tr:outputText value="Account Type" />
    </f:facet>
    <tr:selectOneChoice unselectedLabel="Select" id="accountType" value="#{vo.accountType}" 
        autoSubmit="true" immediate="true" valuePassThru="true"
        valueChangeListener="#{testingAction.optionsChanged}">
        <f:selectItems value="#{testingBean.options}" />
    </tr:selectOneChoice>
</tr:column>
<tr:column>
    <f:facet name="header">
        <tr:outputText value="Other" />
    </f:facet>
    <tr:inputText value="#{vo.otherType}"
        partialTriggers="accountType"
        readOnly="#{vo.readOnlyFlag}"></tr:inputText>
</tr:column>
</tr:table>

The code for the valueChangeListener is as follows:

public void optionsChanged(ValueChangeEvent vce) {
    try {
        System.out.println("Inside Method");
        testingBean = (TestingBean) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("testingBean");
        ArrayList<TableRowVO> list = testingBean.getVoList();
        UIXTable table = testingBean.getTable();
        TableRowVO vo = list.get(table.getRowIndex());
        vo.setReadOnlyFlag(true);
        vo.setAccountType(vce.getNewValue().toString());
        vo.setOtherType("");
        if (vce.getNewValue().equals("3")) {
            System.out.println("Setting false");
            vo.setReadOnlyFlag(false);
        }
        list.set(table.getRowIndex(), vo);
        table.setValue(list);
        testingBean.setVoList(list);
        FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().put("testingBean", testingBean);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Is this behaviour expected? Or is there a workaround?

Interestingly, the "View Page Source" option in Firefox does not contain the text, even though I can see it on the page. I had to use FireBug to analyze the element which revealed the following:

<div class="af_inputText_content" 
id="j_id_jsp_545172797_17:0:j_id_jsp_545172797_27">
tttt
</div>

Version used: Trinidad 2.x JSF 2.x Tomcat 7.0

هل كانت مفيدة؟

المحلول

Ok, so I got it to work a while back but forgot to post the answer....

I used FacesContext to find the CoreComponent for the input text and then reset the components value.

Basically:

  1. Find the CoreTable object
  2. Find the CoreColumn object
  3. Find the CoreInputText object
  4. Reset the value for CoreInputText

The Modified JSP Code is as follows:

<tr:table emptyText="No Data" var="vo" autoSubmit="true" partialTriggers="accountType" id="voTable" value="#{testingBean.voList}" binding="#{testingBean.table}" width="50%">
    <tr:column id="slNoCol">
        <f:facet name="header">
            <tr:outputText id="slNoHdr" value="Sl No" />
        </f:facet>
        <tr:outputText id="slNo" value="#{vo.slNo}" />
    </tr:column>
    <tr:column id="accountTypeCol">
        <f:facet name="header">
            <tr:outputText id="accountTypeHdr" value="Account Type" />
        </f:facet>
        <tr:selectOneChoice unselectedLabel="Select" id="accountType" value="#{vo.accountType}" autoSubmit="true" immediate="true" valuePassThru="true"  valueChangeListener="#{testingAction.optionsChanged}">
            <f:selectItems value="#{testingBean.options}" />
        </tr:selectOneChoice>
    </tr:column>
    <tr:column id="otherTypeCol">
        <f:facet name="header">
            <tr:outputText id="otherTypeHdr" value="Other" />
        </f:facet>
        <tr:inputText value="#{vo.otherType}" columns="15" partialTriggers="accountType" id="otherType" readOnly="#{vo.readOnlyFlag}" immediate="true">
        </tr:inputText>
    </tr:column>
</tr:table>

The code for the modified valueChangeListener is as follows:

public void optionsChanged(ValueChangeEvent vce) {
    try {
        testingBean = (TestingBean) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("testingBean");
        ArrayList<TableRowVO> list = testingBean.getVoList();

        CoreTable table = (CoreTable) FacesContext.getCurrentInstance()
                .getViewRoot().findComponent("voTable");
        CoreColumn column = (CoreColumn) table
                .findComponent("otherTypeCol");
        CoreInputText text = (CoreInputText) column
                .findComponent("otherType");
        text.setValue("");//This is the trick....
        TableRowVO vo = list.get(table.getRowIndex());
        vo.setReadOnlyFlag(true);
        vo.setAccountType(vce.getNewValue().toString());
        vo.setOtherType("");
        if (vce.getNewValue().equals("3")) {
            vo.setReadOnlyFlag(false);
        }
        list.set(table.getRowIndex(), vo);
        table.setValue(list);
        testingBean.setVoList(list);
        FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().put("testingBean", testingBean);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top