1

我正在尝试开发 2 个listmultiplechoice框来将值从一个 list{source} 加载到另一个 list{destination} 并使用Ajaxbuttons将值相互加载。当我单击 ajax 按钮时,我能够看到 java 类中的值,但是这些值没有正确加载到 UI 上的目标 listmultiplechoice 框中。我已经在下面发布了我的代码,请。检查并让我知道这里出了什么问题?

private AjaxButton add;
private AjaxButton remove;
private List selectedOriginals;
private List selectedDestinations;
private ListMultipleChoice originals;
private ListMultipleChoice destinations;
private List<String> currentDestinations = new ArrayList<String>();

originals = new ListMultipleChoice("originals", new PropertyModel(this, "selectedOriginals"),APPLICATIONS);
originals.setOutputMarkupId(true);
add(originals);

destinations = new ListMultipleChoice("destinations", new PropertyModel(this, "selectedDestinations"), new LoadableDetachableModel() 
{
    @Override
    protected Object load() 
    {
        return currentDestinations;
    }
});
destinations.setOutputMarkupId(true);
add(destinations);

add = new AjaxButton("add")
{
    @Override 
    protected void onError(AjaxRequestTarget target, Form form) 
    { 
        System.out.println("Some error in AJX submission....");
        target.addComponent(feedbackPanel); 
    } 

    @Override
    protected void onSubmit(AjaxRequestTarget target, Form form) 
    {
        update(target,selectedOriginals, originals, destinations);
    }
};
add.setOutputMarkupId(true);
add(add);

remove = new AjaxButton("remove") 
{
    @Override 
    protected void onError(AjaxRequestTarget target, Form form) 
    { 
        System.out.println("Some error in AJX submission....**********************");
        target.addComponent(feedbackPanel); 
    }

    @Override
    protected void onSubmit(AjaxRequestTarget target, Form form) 
    {
        update(target, selectedDestinations, destinations, originals);
    }
};
remove.setOutputMarkupId(true);
add(remove);

更新方法::

private void update(AjaxRequestTarget target, List<String> selections, ListMultipleChoice from, ListMultipleChoice to) 
{
    if(selections != null)
    {
        for (String destination : selections) 
        {
            List<String> choices = getChoices(from);
            if (!to.getChoices().contains(destination)) 
            {
                to.getChoices().add(destination);
                choices.remove(destination);
                from.setChoices(choices);
            }
        }

        target.addComponent(to);
        target.addComponent(from);
    }
}
4

1 回答 1

0

这是我为类似任务所做的(我尝试使用您的变量名称),ListMultipleChoice 对象可能必须包含在 WebMarkupContainers 中以进行 ajax 刷新,因此我将容器称为 originalsContainer 和 destinationsContainer。

在添加按钮的 onSubmit 中:

final List choices = originals.getChoices();
for (int index = 0; index < choices.size(); index++) {
    final Location choice = (Location) choices.get(index);
    final String originalsChoiceIndex = originals.getChoiceRenderer().getIdValue(choice, index);
    if (isSelected(originalsChoiceIndex, originals.getValue()) & !selectedDestinations.contains(choice)) {
        selectedDestinations.add(choice);
    }
}
choices.removeAll(selectedDestinations);
originals.setChoices(choices);
SortUtils.sortDestinations(selectedDestinations);
destinations.setChoices(selectedDestinations);
target.add(originalsContainer);
target.add(destinationsContainer);

我的代码中的另一个区别是我对 ListMultipleChoice(s) 的初始化略有不同,这对您来说可能是必要的,也可能不是:originals = new ListMultipleChoice("originals", choices, locationRenderer); 其中 locationRenderer 是一个自定义的 IChoiceRenderer,如下所示:

final IChoiceRenderer locationRenderer = new IChoiceRenderer() {
    @Override
    public Object getDisplayValue(Object o) {
        return ( (Location) o ).getName();
    }

    @Override
    public String getIdValue(Object o, int i) {
        return ( (Location) o ).getId();
    }
};

希望这可以帮助!

于 2013-07-08T15:15:26.453 回答