Per Newgro wrote:
> Hi,
>
> i have to implement a questionnaire but i'm not sure how to capture
> the selected values in a model.
>
> My questionnaire has some questions. The questions have some reply options.
> The selected reply options will be assigned to an answer. The
> answers are the data i try to capture.
>
> So i put the questions in a listview and a dropdown for the reply options on
> it's item.
> But i have no clue how to implement the "business model" containing the
> answers.
> Every answer is related to one selected reply option.
According to your code, your "business model" is a list of QuestionBO objects.
Each QuestionBO object seems to have a property with the question and a
property for the answer. For the sake of this example, let's assume
public class QuestionBO {
private String question;
private ReplyOptionBO answer;
}
(Add serializability and other methods as needed.) That your answer is of
this class follows from your DropDownChoice declaration below.
You'll pass a Wicket model of the list where you want to store the answer to the
constructor. If that business model object gets passed to your page, just wrap
it.
Otherwise, if it's a property of your page, use something like
form.add(new QuestionListView("wicket_id", PropertyModel.of(this,
"propName"))
If it's a member of a larger business model object that's get used elsewhere
in this form as well, chomp the 2nd argument and establish the larger object
in a CPM at the form or page level.
> Can some1 please give me some pointers. Thanks for your support.
> Per
>
> Here some code
>
> <code>
> public class QuestionListView extends ListView<QuestionBO> {
My tip: change to extends PropertyListView<QuestionBO>
if you start working with ListViews. Explore more elaborate model bindings
when you have more experience.
This establishs a CPM for every list item, you'll be able to access
the object properties directly, and don't have to spell out models
explicitly.
>
> public QuestionListView(String id, IModel<? extends List<? extends
> QuestionBO>> model) {
> super(id, model);
// Add this, see Javadoc of ListView
this.setReuseItems(true);
> }
>
> @Override
> protected void populateItem(ListItem<QuestionBO> item) {
> item.add(new Label("questionText", new
> PropertyModel<String>(item.getModel(), QuestionBO.TEXT)));
This code doesn't make much sense to me.
Is QuestionBO.TEXT a constant that's the name of the property with the question
text?
Anyhow, with the above change, this is just
item.add(new Label ("question"))
The label will get it's object from the CPM that's established by PLV.
> // TODO the null has to be replaced by a smart model assigning
> the selected reply option to an answer in the business model
> item.add(new DropDownChoice<ReplyOptionBO>("replyOptions", null,
> new LoadableDetachableReplyOptionsModel(item.getModel()), new
> ReplyOptionBORenderer()));
This changes to
item.add(new DropDownChoice<ReplyOptionBO>("answer", choices,
renderer)
Again, the DropDownChoice will get it's answer property via the CPM
and will just store the answer object in the respective property
of the respective list element object.
In case your question object list change a lot while the application runs,
establish the DB id as selection id in your choice renderer; instead of
the index number.
> }
> }
>
> public class LoadableDetachableReplyOptionsModel extends
> LoadableDetachableModel<List<? extends ReplyOptionBO>> {
>
> private final IModel<QuestionBO> question;
>
> public LoadableDetachableReplyOptionsModel(IModel<QuestionBO>
> question) {
> this.question = question;
> }
>
> @Override
> protected List<? extends QuestionBO> load() {
> return service.loadReplyOptionsFor(question.getObject());
> }
>
> @Override
> public void detach() {
> question.detach();
> super.detach();
> }
> }
> </code>
If your answer shall not be stored in the question object, or if it's also
not possible to have a class that combines question and answer in one object,
it get's a bit more difficult. Then you have to pass a model of a list of answer
objects to the constructor as well, and store that in the ListView, to be able
to access the answer objects in a model in your DropDownChoice. That can be
done,
e.g., with a subclass of PropertyModel that references this.answerListModel,
knows the list item index, and has appropriate redefinitions for getObject()
and setObject() that do the list access.
HTH,
Joachim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Joachim Schrod, Roedermark, Germany
Email: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]