0

I am struggling with this for a really long time. I am changing code and I need to put default value to selectOneRadio element from Trinidad. I have object from database (for example called result). This object has attribute decision. In the select element value parameter was used this way value="#{result.decision}".

Now I need If result.decision is empty set value to True. If value is False let/set value to False. And if value is True let/set value to True. I tried something like this in template:

 <tr:selectOneRadio id="decision" value="#{viewBean.changeValueToOne(result)}" autoSubmit="true" immediate="true">
        <f:selectItem itemValue="true" itemLabel="Yes"/>
        <f:selectItem itemValue="false" itemLabel="No"/>
 </tr:selectOneRadio>

And this function in viewBean:

public Boolean changeValueToOne(ResultDTO result) {
    if (result.getDecision() == null) {
        result.setDecision(Boolean.TRUE);
    }
    return result.getDecision();
}

But when I did this I got only one String value instead of selectOneRadio element. There was only "Yes" or "No" value.

How can I set default value to selectOneRadio element to the value from the object and get a normal selectOneRadio, not just one value.

EDIT

This is code from template. Decision is the object from the database where I need to change value:

<tr:selectOneRadio id="decisionYes" value="#{viewBean.defaultDecision}" valueChangeListener="#{viewBean.valueChangedOneRadioListener(decision)}"
                                           autoSubmit="true" immediate="true">
     <f:selectItems value="#viewBean.decisionStates}"/>
</tr:selectOneRadio>

This is a listener method from viewBean:

public void valueChangedOneRadioListener(DecisionDTO decision, ValueChangeEvent event)
{
    if(event.getNewValue() != null) {
        if((Boolean)event.getNewValue().equals(Boolean.TRUE))
        {
            decision.setDecision(Boolean.TRUE);
        } else {   
            decision.setDecision(Boolean.FALSE);
        }
        // this is variable used in selectRadio
        this.setDefaultVyjadreni((Boolean)event.getNewValue());
    }
}

When I run this code I got error because method wasn't found.

javax.faces.event.AbortProcessingException: Method valueChangedOneRadioListener not found
Bulva
  • 1,208
  • 14
  • 28

1 Answers1

0

I use integer Values for the Trinidads selectOneRadio. You set the default by setting yourValue. You react on a change event with a valueChangeListener.

<tr:selectOneRadio
    id="maschineId"
    value="#{yourBean.yourIntValue}"
    valueChangeListener="#{yourValueChangeListener}"
    autoSubmit="true">
        <f:selectItem itemValue="1"/>
        <f:selectItem itemValue="0"/>
</tr:selectOneRadio

Your backingBean looks somehow like this then:

public void yourValueChangeListener(ValueChangeEvent event)
{
    if(event.getNewValue() == null)
        return;

    if((Integer)event.getNewValue() ==  1)
    {
        //react on a change to 1
    }
    if((Integer)event.getNewValue() ==  0)
    {
        //react on a change to 0
    }
}
public int getYourIntValue() {
    return yourIntValue;
}

public void setYourIntValue(int yourIntValue) {
    this.yourIntValue = yourIntValue;
}
lkdg
  • 1,031
  • 7
  • 18
  • Thanks for answer. I get it but I don't know how to pass value from DTO if the value is from database object. In this example #{yourBean.yourIntValue} is the value from bean but not from a database object from DTO class. – Bulva Apr 19 '18 at 07:00
  • Should I create method which returns myIntValue to value based on value returned from object? – Bulva Apr 19 '18 at 08:20
  • Yes, that is the way. Regarding to your second comment. You retrieve the value you want from database and set the value. Probably within the constructor of your bean, the value will be set by pageload then. If you set the value after the page has loaded und you recognize the valuechange just after you reload with the browser you may have to fiddle around with trinidads partialTriggers. That depends on the scope you use for your backingBean (Request or Session). Don't know if this is much help, but the only resource for trinidad that Iam aware of: http://myfaces.apache.org/trinidad/ – lkdg Apr 19 '18 at 11:12
  • Thanks a lot I will try it and write tomorrow if it works, because I am afraid that this not working and there is only string, not radio button. – Bulva Apr 19 '18 at 19:35