سؤال

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?

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

المحلول

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).

نصائح أخرى

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()?

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>

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>
<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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top