0

我有一个列表视图,里面的列表视图是

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

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

创建新帐户时应该是帐户下拉列表中的选定值。

这适用于第一个帐户,但第二个和所有后续新创建的帐户被正确创建并添加到可能性列表中,但它们不是选定的帐户。每次我进行 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

4

2 回答 2

1

如果不知道 OwnedAccountCreateNewDropDownChoice 做了什么,就很难找到问题所在。

但请查看这一行:

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

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

遵循黄金 Wicket 规则,您永远不应该从模型中拉出某些东西来将其放入另一个模型中:

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

也许这可能会有所帮助。

于 2012-11-20T09:19:07.520 回答
0

我发现了问题:)

Listviews are not good in forms because the can't handle dynamic changes because the list items dont keep track of their index.

I've implemented a listview based on a guide found at this website

http://wicketinaction.com/2008/10/building-a-listeditor-form-component/

I works like a charm.

于 2012-11-26T07:57:05.070 回答