1
votes

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

2
So the first two edit boxes should only display the value of the sessionScope variables? and the two sessionScope variables should not be changed by the user? Then set the values of the sessionScope variables in e.g. the beforeRenderResponse event insteadPer Henrik Lausten
Hello @PerHenrikLausten, thanks for your response. " the two sessionScope variables should not be changed by the user?", Yes that is true, I review the user requirement of the program, the user request when they login to that xpage, their username and the department should display automatically. " first two edit boxes should only display the value of the sessionScope variables?", it is because if I don't use sessionScope variable, I cannot export the data in excel and I don't know if there is a better approach to achieve the goal.beginner
I am sorry that I am not sure how to use the beforeRenderResponse event properly. In there I put sessionScope.Officer & sessionScope.Department, and the edit box is still editable.beginner
I have added an answer that hopefully can help youPer Henrik Lausten

2 Answers

3
votes

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}" />
0
votes

I had this issue with 9.01 FP6 The best solution was to place the inputs in a xp:panel and then use the readonly attribute on the panel to make the field within it read only. This worked well and was easy to implement