3

I have been learning Xpages programming. We are currently using domino 8.5.2. I am gaining familiarity with the display/input controls and I have had some success using them to display information from backend domino documents, views, scoped variables that are not an array. What I have not been able to discover is how to display the elements of a scoped variable array that is created dynamically.
For instance: I create an array with a number of elements. I can print the elements to the domino log with the following code:

for (var i=0; i<array.length; i++) {
    print(array[i])
}

What do I use to display the individual elements on webpage? I apologize if the answer if obvious. I did find one posting about displaying a 2 dimensional array - but was unable to interpret the answer.
Thanks for any guidance. ---Lisa&

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76
  • Lisa, I removed the JavaScript tag to not get wrong answers that focus on csjs. Also, upgrade Domini to 9.0.1 or at least the latest 8.5.3. Your XPages experience will be much better :-) – Per Henrik Lausten Aug 11 '15 at 21:25
  • 3
    We are planning to go to 9.0.1 as soon as we can. –  Aug 11 '15 at 21:40

2 Answers2

4

Use a repeat control and show elements within repeat in a computed field:

<xp:this.beforePageLoad><![CDATA[#{javascript:
    var myTest = []; 
    for (var i=0; i<9; i++) {
        myTest[i] = i; 
    } 
    viewScope.myTest = myTest;
}]]></xp:this.beforePageLoad>
<xp:repeat
    id="repeat1"
    rows="30"
    var="row"
    value="#{viewScope.myTest}">
    <xp:text
        escape="true"
        id="computedField1"
        value="#{javascript:row.toString()}">
    </xp:text>
    <br />
</xp:repeat>

The array is in viewScope.myTest in this example.

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • I have been working with the repeat control and got it to work when my array was defined as follows: "viewScope.myTest= [["202"], ["203"], ["204"]];" When I create my array dynamically by using a loop: "var myTest = newArray(); for (i=0, i<9, i++) { myTest[i] = i; } viewScope.myTest=myTest" - I get a JVM error. There must be something more fundamental that I am not understanding about arrays and SSJS. –  Aug 11 '15 at 21:48
  • 1
    But wait - The example I was using had "{row[0]}" instead of "#{row}" because it came from a post about multi-dimensional arrays. (http://stackoverflow.com/questions/24661783/xpages-repeat-control-with-scoped-variable-as-data-source). I removed the zero and -think- I may be getting somewhere. –  Aug 11 '15 at 21:55
  • 1
    So, the solution from answer is working? If yes, please accept it, as it will help others as well – Florin M. Aug 12 '15 at 13:34
  • Solution now has the green check. –  Aug 14 '15 at 15:59
1

A very quick way to display the content of an array is using the join function. No repeat required but the display is limited.

<xp:this.beforePageLoad><![CDATA[#{javascript:
    var arrTest= ["a","b","c","d"];
    viewScope.myTest = myTest;
}]]></xp:this.beforePageLoad>

    <xp:text id="testField"
        value="#{javascript:viewScope.arrTest.join('; ')">
    </xp:text>

This shows

a; b; c; d

You could add HTML around it to pretty it up, and set the display type of the text field to be HTML whcih gives you the opton to join with HTML eg

value="#{javascript:viewScope.arrTest.join('<br>')"

Not as complete a solution as a repeat control, but good for quick things.

user2808054
  • 1,376
  • 1
  • 11
  • 19