我正在制作一个教师提出问题的表格。一种类型的问题是多项选择题。该表单有一个 textArea,您可以在其中编写问题公式,以及一个带有 textFields 的 listView 供您选择。
有一个按钮可以添加一个新的选项(添加一个新的文本字段),按下它会重新绘制所有的选项并添加一个新的选项。现在问题来了:我希望 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);
    }
}
任何帮助表示赞赏。