1

In a repeat (var=row, indexVar=rownumber) ,I can't get the the defaultselected to work . The return value becomes correctly "true" but doesn't work.

The idea is that 1 radio from this radiogroup ("selection") is preselected, based on the value in the viewScope selectie.

<xp:radio id="radio1" value="#{viewScope.test}" groupName="selection">
            <xp:this.text><![CDATA[#{javascript:row[3]}]]>
            </xp:this.text>                                                     
       <xp:this.defaultSelected><![CDATA[#{javascript:var check = "no";
         try{
           for (i=0;i<25;i++){
             if( @IsMember(row[4],viewScope.selectie[0][i])==1){var 
             check="true";break}//only 1 can be checked
           }//end for i
          if (check=="true"){return true}
          else {return false}
        }//end try
        catch (e){
          sessionScope.error = "error radio1 :"+e.toString();}}]]>
    </xp:this.defaultSelected>
    <xp:eventHandler event="onchange" submit="true" refreshMode="partial" 
     refreshId="resultPanel">
         <xp:this.action><![CDATA[#{javascript:var item 
             =viewScope.lijnknr.intValue();
             viewScope.selectie[0].set(item,row[4]);}]]>
         </xp:this.action>
    </xp:eventHandler>
</xp:radio>

EDIT I added following line : <xp:this.selectedValue><![CDATA[#{javascript:row[4]}]]></xp:this.selectedValue> since I read following post : Xpages radio btn group checked

but it still doesn't work .....

SECOND EDIT added another property : skipContainers="1" as found in "Mastering Xpages",

but still doesn't work ....

Marc Jonkers
  • 496
  • 1
  • 7
  • 17
  • Isn't there a typo in the SSJS for defaultSelected? Shouldn't `@IsMember` search for `row[4]` in `viewScope.selectie[0]` instead of `viewScope.selectie[0][i]`? Also, IMO the whole code for this property could be simplified to `return (@IsMember(row[4],viewScope.selectie[0]) ? true : false)` . – xpages-noob Dec 13 '17 at 08:29
  • No, it's not a typo. viewScope.selectie is an array, and row[4] can be at one of the 25 positions in this array. – Marc Jonkers Dec 13 '17 at 08:56
  • Then why do you iterate with `@IsMember`? if `viewScope.selectie` already is the array that does or does not contain the value `row[4]`, the code for defaultSelected should just be `#{javascript:return (@IsMember(row[4],viewScope.selectie)==1 ? true : false);}` . – xpages-noob Dec 13 '17 at 09:18
  • You're right , I changed it to (@IsMember(row[4],viewScope.selectie[0])==1 ? true : false); , since the array selectie has two "levels" . This way the code is better, but the result is the same. The problem remains : the defaultSelected property becomes true for the correct radio , but the radio isn't checked. Is their another way to select the radio , for example change the style of the "preselected" radio ? – Marc Jonkers Dec 13 '17 at 14:08
  • For example ; when I put (@IsMember(row[4],viewScope.selectie[0])==1 ? "color:red" : ""); in the computed style property of the radio, the text of defaultchecked radio becomes red . But how do I change the style in order to not change its color to red but select it ...... ? – Marc Jonkers Dec 13 '17 at 14:20
  • 1
    I never use radio or radioGroup elements, but I think the problem might be that you set the value of the radio element to `viewScope.test` and thus defaultSelected might not apply anymore. Also, instead of using a repeat control with single radio elements, I recommend using the approach with the radioGroup element that has been suggested by @shillem in his answer below. – xpages-noob Dec 13 '17 at 14:26
  • I removed viewScope;.test as value of the radioGroup an , it's working now! Thank you so much !! – Marc Jonkers Dec 14 '17 at 08:24

2 Answers2

1

An alternative approach, if possibile, is to to use the radioGroup component:

<xp:radioGroup value="#{viewScope.fruit}" defaultValue="orange">
    <xp:selectItems value="#{javascript:['apple', 'orange', 'banana']}" />
    <xp:eventHandler event="onclick" submit="true"
        execMode="partial" refreshMode="partial" refreshId="result" />
</xp:radioGroup>

<xp:div id="result">
    <xp:text value="The selected fruit is: #{empty viewScope.fruit ? 'none' : viewScope.fruit}" />
</xp:div>
shillem
  • 1,260
  • 7
  • 12
0

IMPORTANT EDIT!

What I wrote below stands if the radios were declared one by one - therefore outside the repeat component. When placed inside the repeat component there's a bug that prevents the radio group from working as one. In fact, the output name param results different for every looped radio component...


I don't see your whole code there so I will go with an example. Consider the following (and for brevity's sake I will commit this sin of using SSJS sometimes):

<xp:repeat value="#{javascript: ['apple', 'orange', 'banana']}"
    var="fruit">
    <xp:radio groupName="fruits" selectedValue="#{fruit}"
        value="#{viewScope.fruit}" defaultSelected="#{fruit eq 'orange'}" text="#{fruit}" />
</xp:repeat>

I passed an on-the-fly array to the repeat control and I will reference each value as fruit.

At this point I populate the radio component:

  1. I used the same groupName to group the radios (this you know)
  2. I assigned the selectedValue to the current fruit. This is the value that will be saved if the radio gets selected
  3. I bound value to the property where the selected value will actually be saved (in this case, like you did, I went for the viewScope)
  4. I set the defaultSelected value (evaluated from the literal value orange but you can do it differently). In case viewScope.fruit is empty orange will be the default value.
  5. I assigned the text - label - again to the current fruit variable

Again, this will give you the orange choice as default selection in case the variable viewScope.fruit is empty. If, supposedly, you had assigned a value to the viewScope during page load like, say:

<xp:view xmlns:xp="http://www.ibm.com/xsp/core" beforePageLoad="#{javascript: viewScope.fruit = 'banana'}">

Then the selected radio would not have been orange but banana.

Regarding the eventHandler, it's not clear to me what you're trying to achieve there. If you want to further explain maybe I can help there also.

shillem
  • 1,260
  • 7
  • 12
  • The radios are declared in the repeat. So probably the bug will be the problem. They seem to act as one , since when you select an item , the previously selected one is deselected.The way I'm using it now works except the "preselection".Isn't it possible to solve this with css. (change the style to a selected state when the radio should be selected) I'm not really into css , so I don't know what's possible ... – Marc Jonkers Dec 13 '17 at 11:03