リストビューがあり、そのリストビューの中に
EnumdropDrop - テキストフィールド - DropdownChoice - Ajaxbutton | すべてのアイテムに
最初のドロップダウンは通貨で、2 番目はアカウント オブジェクトです。アカウント ドロップダウンの選択肢の 1 つは、通貨にちなんで名付けられた新しいアカウントを作成することです。
新しいアカウントが作成されると、アカウントのドロップダウンで選択された値になります。
これは最初のアカウントでは正常に機能しますが、2 つ目以降の新しく作成されたすべてのアカウントは正しく作成され、可能性のリストに追加されますが、それらは選択されたアカウントではありません。ajaxを更新するたびに、新しく作成されたすべてのアイテムで選択されたアカウントが最初に作成されたアカウントとして設定されます。面白いのは、フォームを送信すると、アカウントの選択が正しいことです。
package com.trifork.pengeplan.web.components.lists;
import com.trifork.pengeplan.domain.*;
import com.trifork.pengeplan.web.components.choice.EnumDropDownChoice;
import com.trifork.pengeplan.web.components.choice.OwnedAccountCreateNewDropDownChoice;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: tommysadiqhinrichsen
* Date: 21/09/12
* Time: 11.55
*/
public class MultipleCurrenciesListView extends PropertyListView<FinanceAccountCurrencyMapping> {
IModel<OwnedAccount> ownedAccountIModel;
public MultipleCurrenciesListView(String id, IModel model) {
super(id, new PropertyModel<List<FinanceAccountCurrencyMapping>>(model, "financeAccountCurrencyMappings"));
ownedAccountIModel = model;
}
@Override
protected void populateItem(final ListItem<FinanceAccountCurrencyMapping> item) {
EnumDropDownChoice<MoneyCurrency> moneyCurrency = new EnumDropDownChoice<MoneyCurrency>("moneyCurrency");
moneyCurrency.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
MoneyCurrency moneyCurrency = (MoneyCurrency) getFormComponent().getModelObject();
}
});
item.add(moneyCurrency);
item.add(new TextField("financeAccount.accountNumber"));
final OwnedAccountCreateNewDropDownChoice accountForCash = new OwnedAccountCreateNewDropDownChoice("accountForCash", ownedAccountIModel.getObject().getLegalEntity().getOwnedBankAccounts());
accountForCash.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
OwnedAccount ownedAccount = (OwnedAccount) getFormComponent().getModelObject();
if (ownedAccount != null && ownedAccount.getId() == -1) {
OwnedAccount newOwnedAccount = new OwnedAccount();
FinanceAccountCurrencyMapping financeAccountCurrencyMapping = item.getModelObject();
financeAccountCurrencyMapping.setAccountForCash(ownedAccount);
newOwnedAccount.setName(financeAccountCurrencyMapping.getOwnedAccount().getName() + " - " + financeAccountCurrencyMapping.getMoneyCurrency().name());
newOwnedAccount.setLegalEntity(financeAccountCurrencyMapping.getOwnedAccount().getLegalEntity());
newOwnedAccount.setAssetOrLiability(AssetOrLiability.ASSET);
newOwnedAccount.setAssetType(AssetType.BANK_ACCOUNT);
newOwnedAccount.setCurrency(financeAccountCurrencyMapping.getMoneyCurrency());
newOwnedAccount.getFinanceAccountCurrencyMappings().add(new FinanceAccountCurrencyMapping(newOwnedAccount));
ownedAccountIModel.getObject().getLegalEntity().getOwnedAccounts().add(ownedAccountIModel.getObject().getLegalEntity().getOwnedAccounts().size() - 1, newOwnedAccount);
accountForCash.setDefaultModelObject(newOwnedAccount);
target.add(getFormComponent().getForm().getPage());
}
}
});
item.add(accountForCash);
item.add(new AjaxButton("deleteFinanceAccount", new ResourceModel("button.delete")) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
ownedAccountIModel.getObject().getFinanceAccountCurrencyMappings().remove(item.getModelObject());
target.add(this.getForm());
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
super.onError();
}
});
}
}
誰かが私に何が問題なのか教えてもらえますか?
更新しました
package com.trifork.pengeplan.web.components.choice;
import com.trifork.pengeplan.domain.OwnedAccount;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: tommysadiqhinrichsen
* Date: 18/09/12
* Time: 16.01
* To change this template use File | Settings | File Templates.
*/
public class OwnedAccountCreateNewDropDownChoice extends DropDownChoice<OwnedAccount> {
public OwnedAccountCreateNewDropDownChoice(String id) {
super(id);
List<OwnedAccount> choices = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, IModel model) {
super(id, model);
List<OwnedAccount> choices = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, List<OwnedAccount> choices) {
super(id, choices);
List<OwnedAccount> choices1 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, List<OwnedAccount> choices, ChoiceRenderer<OwnedAccount> renderer) {
super(id, choices, renderer);
List<OwnedAccount> choices2 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, Model<OwnedAccount> model, List<OwnedAccount> choices, ChoiceRenderer<OwnedAccount> renderer) {
super(id, model, choices, renderer);
List<OwnedAccount> choices3 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public OwnedAccountCreateNewDropDownChoice(String id, PropertyModel<OwnedAccount> model, List<OwnedAccount> choices, ChoiceRenderer<OwnedAccount> renderer) {
super(id, model, choices, renderer);
List<OwnedAccount> choices4 = (List<OwnedAccount>) getChoices();
choices.add(new OwnedAccount(-1, new ResourceModel("create.new").getObject()));
setChoices(choices);
init();
}
public void init() {
}
public void addChoice(OwnedAccount ownedAccount) {
List<OwnedAccount> choices = (List<OwnedAccount>) getChoices();
choices.add(choices.size() - 1, ownedAccount);
setChoices(choices);
}
}