Bruce Stemplewski
Bruce Stemplewski

Reputation: 1343

How do you make a computed field both computed and databound to a field?

I know how to bind a computed field to a document field. And I know how to use javascript to compute a computed field. But how do I do both?

Say my javascript in the computed field is:

 @Name("[ABBREVIATE]" ,@UserName());

How do I bind that to say document1.ReqName field?

Upvotes: 1

Views: 4417

Answers (5)

user3947424
user3947424

Reputation: 1

<xp:text escape="true" id="computedFieldTest1">
[CDATA[#{javascript:
  var userName = "Username: "+("Reader",@Name("[ABBREVIATE]",@UserName()));
  currentDocument.replaceItemValue("computedFieldTest1",userName);
  return userName;
  }]]></xp:this.value>
</xp:text>

Upvotes: 0

Michael
Michael

Reputation: 51

Maybe I miss the Point but why don't You simply do the following?

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

This is a Textfield bound per EL to the DocumentField. If the field is empty it is calculated by the default value.

Great! - I missed the point. But You can try this one:

<xp:text escape="true" id="computedField3">
    <xp:this.value>
        <![CDATA[#{javascript: 
                    if(@IsNewDoc()){
                          document1.replaceItemValue("Reader",@Name("[ABBREVIATE]",@UserName()));
                    }
                    return document1.getItemValue("Reader");}]]>
    </xp:this.value>
</xp:text>

Upvotes: 1

Simon McLoughlin
Simon McLoughlin

Reputation: 8465

I wouldn't bind in this case. I would bind every other control and in this case manually add that into the document field directly with script before the save action.

so something like: (forgive the code not at my work comp)

<eventhandler>
   <actionGroup>
      <script><![CDATA[#{javascript:
         doc.replaceItemValue("ReqName", @Name("[ABBREVIATE]" ,@UserName()));
      }]]><script>

      <saveDocument></saveDocument>
   </actionGroup>
</eventhandler>

Upvotes: 0

Per Henrik Lausten
Per Henrik Lausten

Reputation: 21709

I use two fields:

  1. a hidden input field that computes the required value (using the xp:inputHidden control). I use "default value" to emulate Computed when composed - and converters to emulate Computed (inspired by Tommy Valand).
  2. a visible computed field or a visible input field set to read only that does the same calculation. Perhaps this can be changed to display the value of the hidden input field using getComponent("").getValue()?

Upvotes: 3

Bill Hanson
Bill Hanson

Reputation: 91

For the computed field's Value, use Advanced data binding and select Expression Language (EL). The expression to use is simply "document1.ReqName" (no quotes).

Upvotes: 0

Related Questions