Search code examples
xpages

Are xp:dataContext variables read-only?


From examples I've seen in the Mastering XPages book and around the web, an xp:dataContext variable should be writable but I can't get it to work.

Below is a simplified version of my code. A button calls an xe:dialog containing a panel with a dataContext variable attended and initial value of "", expecting to be able to write to it from the radioGroup data binding. When the radio is set to a certain value (in this case, != "Yes") I want to unhide a div but it doesn't work. The div does not appear and the txt1 computed text field never changes to reflect the value of the radio.

I have tried both an onclick and onchange event handler but neither seems effective. Are dataContext variables read-only?

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xe="http://www.ibm.com/xsp/coreex">

  <xp:button id="btn" value="Button">
    <xp:eventHandler event="onclick" submit="true" refreshMode="partial">
      <xp:this.action><![CDATA[#{javascript:getComponent('dlg').show()}]]></xp:this.action>
    </xp:eventHandler>
  </xp:button>

  <xe:dialog id="dlg" title="Dialog">
    <xp:panel id="panel1" readonly="false">
      <xp:this.dataContexts>
        <xp:dataContext var="attended" value="" />
      </xp:this.dataContexts>

      <xp:radioGroup id="activityAttended" value="#{attended}">
        <xp:selectItem itemLabel="Yes" />
        <xp:selectItem itemLabel="No" />
        <xp:selectItem itemLabel="Maybe" />
        <xp:eventHandler id="event1" event="onclick" submit="true"
          refreshMode="partial" refreshId="target" />
      </xp:radioGroup>

      <xp:div id="target">
        radio: <xp:text id="txt1" value="#{attended}" />

        <xp:div id="div1" rendered="#{attended != 'Yes'}">
          <xp:text id="txt2" value="#{attended}" />
        </xp:div>
      </xp:div>
    </xp:panel>
  </xe:dialog>
</xp:view>

Solution

  • To my knowledge, you can't assign to the variable name for a dataContext. What you could do would be to make the value bound to something that contains another value, like a Map or custom object, and then alter properties on that.

    For example, if you made it like var="contextProps" value="${javascript:new java.util.HashMap()}", you could then do things like #{contextProps.attended != 'Yes'} and value="#{contextProps.attended}". Usually, you'll want a more-fleshed-out object than a generic HashMap for this, but it can serve as an example or a useful container in a pinch.