gpt4 book ai didi

java - DropdownChoice 选择与模型值不同,在 ajax 更新时更改

转载 作者:行者123 更新时间:2023-12-01 15:02:21 25 4
gpt4 key购买 nike

我有一个 ListView ,该 ListView 内部是一个

EnumdropDrop - 文本字段 - DropdownChoice - Ajaxbutton |对于每个项目

第一个下拉列表是货币,第二个下拉列表是帐户对象,帐户下拉列表中的选择之一是创建一个新帐户,然后以货币命名。

创建新帐户时,应为帐户下拉列表中选择的值。

这对于第一个帐户效果很好,但第二个和所有后续新创建的帐户均已正确创建并添加到可能性列表中,但它们不是选定的帐户。每次我进行 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();
}
});
}


}

有人能告诉我问题出在哪里吗?

Created first account Created second account About to create third account Created third account but not selecte Refresh reset all Saved values from image 5

已更新

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);
}
}

Ajax response

最佳答案

如果不知道 OwnedAccountCreateNewDropDownChoice 做了什么,就很难发现问题。

但请检查这一行:

new OwnedAccountCreateNewDropDownChoice("accountForCash",
ownedAccountIModel.getObject().getLegalEntity().getOwnedBankAccounts());

由于您稍后要修改银行帐户,因此此列表可能会过时。

遵循黄金 Wicket 规则,您永远不应该从模型中取出某些内容并将其放入另一个模型中:

new OwnedAccountCreateNewDropDownChoice("accountForCash",
new PropertyModel(ownedAccountIModel, "legalEntity.ownedBankAccounts");

也许这可能有帮助。

关于java - DropdownChoice 选择与模型值不同,在 ajax 更新时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13459993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com