Search code examples
xpages

xpages: how to make the edit box read-only and still have the session scope variable?


I have a xpage that contains 4 edit boxes and a button, they are used to export the data in excel. I use session scope in those four edit boxes because I have store the value in order to generate the excel.

The design look looks like the following:

//1st edit box: get the user name, set it to default so the user does not need to type his/her name

<xp:inputText id="inputText1" value="#{sessionScope.User}">
    <xp:this.defaultValue><![CDATA[#{javascript: @Name("[CN]",@UserName())}]]></xp:this.defaultValue>
</xp:inputText>

//2nd edit box: after get the user name in edit box 1, retrieve the user's department in this edit box

<xp:inputText id="inputText2"value="#{sessionScope.Department}">
    <xp:this.defaultValue><![CDATA[#{javascript:@DbLookup(@DbName(),"UserView", (getComponent("inputText1").getValue()),2  )}]]></xp:this.defaultValue>
</xp:inputText>

//3rd edit box: get the start search date in YYYYMM format, user only can type numbers

<xp:inputText id="inputText3" value="#{sessionScope.startYYYYMM}">
<xp:eventHandler event="onkeypress" submit="false">
    <xp:this.script><![CDATA[if  (event.keyCode >= 48 && event.keyCode <= 57)
    {
       event.returnValue = true;
    }
    else
    {
      event.returnValue = false;
    }]]></xp:this.script>
        <xp:this.parameters>
            <xp:parameter name="message1" value="value">
            /xp:parameter>
        </xp:this.parameters>
</xp:eventHandler>
</xp:inputText>

//4th editbox: get the endsearch date in YYYYMM format, user only can type numbers

<xp:inputText id="inputText4" value="#{sessionScope.startYYYYMM}">
<xp:eventHandler event="onkeypress" submit="false">
    <xp:this.script><![CDATA[if  (event.keyCode >= 48 && event.keyCode <= 57)
    {
       event.returnValue = true;
    }
    else
    {
      event.returnValue = false;
    }]]></xp:this.script>
        <xp:this.parameters>
            <xp:parameter name="message1" value="value">
            /xp:parameter>
        </xp:this.parameters>
</xp:eventHandler>
</xp:inputText>

//a button to call another page to export the excel

<xp:button value="Export" id="button11">
<xp:eventHandler event="onclick" submit="true" immediate="false" save="false" refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:context.redirectToPage("ExportExcel.xsp");}]]></xp:this.action>
</xp:eventHandler>
</xp:button>

I run the code, it works fine. However here is what I would like to ask. In the first and the second edit box, if I set those edit boxes to Read-only. It seems lose the session scope variable and I run the code, the excel just contains the default header.

I find this post ReadOnly field in Xpage not submitted is very similar to my case, so I set the edit box to editable and try to apply this code from the post:

//for 1st editbox
<xp:scriptBlock id="scriptBlock1">
    <xp:this.value><![CDATA[function makeFieldReadOnly() {
document.getElementById("#{id:inputText1}").readOnly = true;  
}
window.onload = makeFieldReadOnly;]]></xp:this.value>
</xp:scriptBlock>

 //for 2nd edit box
 <xp:scriptBlock id="scriptBlock2">
    <xp:this.value><![CDATA[function makeFieldReadOnly() {
document.getElementById("#{id:inputText2}").readOnly = true;  
}
window.onload = makeFieldReadOnly;]]></xp:this.value>
</xp:scriptBlock>

The edit box still editable and the excel is not work properly (only display the default header).

So how can I make the edit box read-only and still have the session scope variable?

Grateful for your advice please. Thank you.

Reference:

ReadOnly field in Xpage not submitted

export data from panel


Solution

  • You can calculate the values for your two sessionScope variables in e.g. the beforeRenderResponse:

    <xp:this.beforeRenderResponse><![CDATA[#{javascript:
        sessionScope.User = @Name("[CN]",@UserName());
        sessionScope.Department = @DbLookup(@DbName(),"UserView", (@Name("[CN]",@UserName()),2  )
    }]]></xp:this.beforeRenderResponse>
    

    You can then show the values of the sessionScope variables in computed text fields instead of input fields:

    <xp:text escape="true" id="computedField1" value="#{sessionScope.User}" />
    <xp:text escape="true" id="computedField2" value="#{sessionScope.Department}" />