저는 교사가 질문을하는 양식을 만들고 있습니다. 질문의 한 유형은 선다형 문제입니다. 양식에는 질문 공식을 작성하는 textArea와 대안에 대해 textFields를 사용하는 listView가 있습니다.Wicket : 텍스트 필드를 다시 칠하고 사용자 입력을 유지합니다.
새 대안을 추가하는 버튼이 있습니다 (새 textField 추가).이 버튼을 누르면 모든 대안을 다시 페인팅하고 새 대안을 추가합니다. 이제 문제는 다음과 같습니다. 이미 텍스트가있는 listView에 텍스트가있는 텍스트를 칠하기 전에 선생님이 작성한 텍스트를 유지하고 싶습니다. 그러나이 방법을 사용하는 방법을 모릅니다 (모든 다시 칠하기 전에 데이터베이스에 값 저장하는 경우 제외). ,하지만 그것은 나쁜 생각처럼 보입니다).
다음은 MultipleChoiceQuestionPanel의 코드입니다. 충분한 것으로 기대됩니다.
public class MultiChoiceQuestionPanel extends QuestionPanel {
private List<Alternative> alternatives;
@SpringBean
private AlternativeRepository alternativeRepository;
public List<Alternative> getAlternatives(){
return alternatives;
}
public MultiChoiceQuestionPanel(String id, MultipleChoiceQuestion q){
super(id, q);
final WebMarkupContainer parent = new WebMarkupContainer("alternativesContainer");
parent.setOutputMarkupId(true);
add(parent);
parent.add(new Label("AnswerLabel", "Svar"));
q.setAlternatives(alternativeRepository.findByMultipleChoiceQuestion(q));
alternatives = q.getAlternatives();
Form form = new Form("addForm");
form.add(new ListView<Alternative>("alternatives", alternatives) {
@Override
protected void populateItem(final ListItem<Alternative> alternativeListItem) {
alternativeListItem.add((TextField<String>) new TextField<String>("alternative", new AlternativeModel(alternativeListItem.getModelObject())).setRequired(true).setType(String.class));
Form form = new Form("removeForm");
form.add(new AjaxSubmitLink("remove") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
Alternative selected = alternativeListItem.getModelObject();
alternativeRepository.delete(selected);
getAlternatives().remove(selected);
target.addComponent(parent);
}
});
alternativeListItem.add(form);
add(alternativeListItem);
}
});
AjaxSubmitLink a = new AjaxSubmitLink("add") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
Alternative alternative = new Alternative();
MultipleChoiceQuestion mcq = (MultipleChoiceQuestion) getQuestion();
alternative.setSequenceNumber(mcq.getAlternatives().size());
alternative.setMultipleChoiceQuestion((MultipleChoiceQuestion) getQuestion());
alternativeRepository.save(alternative);
getAlternatives().add(alternative);
target.addComponent(parent);
}
};
a.setDefaultFormProcessing(false);
form.add(a);
parent.add(form);
}
}
도움을 주시면 감사하겠습니다. Javadoc of ListView에서
감사합니다. 문제가 해결되었습니다. –
@LeoSundholm 여러분을 환영합니다. – Nicktar
이것은 새로운 문제를 만든 것으로 보입니다. 새 textFields를 추가해도 완벽하게 작동하지만 제거 버튼이 이상하게 작동합니다. 어떤 버튼을 눌러도 삭제되는 버튼은 사라지는 textField의 마지막 텍스트입니다. 제거한 textField의 텍스트가 아닙니다. 그 이유에 대한 설명이나 제안? –