JSF- passing a parameter to valuechangelistener
Asked Answered
F

2

11

I have a litte radiobutton like this :

<h:selectOneRadio value="#{test.answer}" valueChangeListener="#{TestService.changeanswer}" immediate="true" id="answer">
 <f:selectItem  itemValue="A" itemLabel="Absolutely True"/>
 <f:selectItem  itemValue="B" itemLabel="True"/>
 <f:selectItem  itemValue="C" itemLabel="Partially True"/>
 <f:selectItem  itemValue="D" itemLabel="Not True"/>
 <f:selectItem  itemValue="E" itemLabel="Definitely Not True"/>
 <f:ajax event="change" process="answer"></f:ajax></h:selectOneRadio>

And my listener is like that :

public void changeanswer(ValueChangeEvent vcEvent) { 
System.out.println("comeon= " + vcEvent.getOldValue()); 
System.out.println("comeon= " + vcEvent.getNewValue());}

I would like to pass a parameter to the changeanswer method.For example I want to pass the questionid to the changeanswer function. I need to make some arrangements in it.

How can I do that?

Many many many thanks in advance.

Brad - the Rookie..

Fideliafidelio answered 17/10, 2010 at 18:32 Comment(0)
C
13

Seeing how the component values are bound, I bet that it's inside a datatable. If that is indeed the case, you can use DataModel#getRowData() to obtain the current row. Add a DataModel property to the TestService bean like follows:

private List<Question> questions;
private DataModel<Question> questionModel;

@PostConstruct
public void init() {
    questions = getItSomehow();
    questionModel = new ListDataModel<Question>(questions);
}

public void change(ValueChangeEvent event) {
    Question currentQuestion = questionModel.getRowData();
    // ...
}

and change the view as follows:

<h:dataTable value="#{TestService.questionModel}" var="test">

That said, I'd suggest to use more sensible variable names than TestService, test and change(), like Questionaire, question and changeAnswer() respectively. This makes the code more self-documenting.

Cartridge answered 17/10, 2010 at 20:7 Comment(4)
You are absolutely right. It was a datatable. And you saved my week. Thanks hero!Fideliafidelio
That was VERY VERY helpful BaluC! Thanks a lot! :) I didn't know there exists something like DataModel class!!! This is SO useful! +1 for Brad's question as well.Bills
@Nikhil: you're welcome. You may find this article useful as well: balusc.blogspot.com/2010/06/… It contains a CRUD example utilizing <h:dataTable>, DataModel<E> and @ViewScoped.Cartridge
@BalusC: Today I found your name at about 100 answers to important jsf issues - Thanks for itMapping
G
20

You can use the f:attribute tag to send any data to the ValueChangeListener:

<h:selectOneRadio value="#{test.answer}"
                  valueChangeListener="#{TestService.changeanswer}"
                  immediate="true" id="answer">
    <f:attribute name="myattribute" value="#{test.questionid}" />
    <f:selectItem  itemValue="A" itemLabel="Absolutely True"/>
    ...
</h:selectOneRadio>

If we suppose questionId is an Integer, then you can receive the data the following way:

public void changeanswer(ValueChangeEvent vcEvent) { 
  Integer questionId =
    (Integer) ((UIInput) vcEvent.getSource()).getAttributes().get("myattribute");
Galibi answered 15/12, 2012 at 21:8 Comment(0)
C
13

Seeing how the component values are bound, I bet that it's inside a datatable. If that is indeed the case, you can use DataModel#getRowData() to obtain the current row. Add a DataModel property to the TestService bean like follows:

private List<Question> questions;
private DataModel<Question> questionModel;

@PostConstruct
public void init() {
    questions = getItSomehow();
    questionModel = new ListDataModel<Question>(questions);
}

public void change(ValueChangeEvent event) {
    Question currentQuestion = questionModel.getRowData();
    // ...
}

and change the view as follows:

<h:dataTable value="#{TestService.questionModel}" var="test">

That said, I'd suggest to use more sensible variable names than TestService, test and change(), like Questionaire, question and changeAnswer() respectively. This makes the code more self-documenting.

Cartridge answered 17/10, 2010 at 20:7 Comment(4)
You are absolutely right. It was a datatable. And you saved my week. Thanks hero!Fideliafidelio
That was VERY VERY helpful BaluC! Thanks a lot! :) I didn't know there exists something like DataModel class!!! This is SO useful! +1 for Brad's question as well.Bills
@Nikhil: you're welcome. You may find this article useful as well: balusc.blogspot.com/2010/06/… It contains a CRUD example utilizing <h:dataTable>, DataModel<E> and @ViewScoped.Cartridge
@BalusC: Today I found your name at about 100 answers to important jsf issues - Thanks for itMapping

© 2022 - 2024 — McMap. All rights reserved.