我有一个类似
的对象public class Table {
private String id;
private String name;
private List<Field> fieldsList;
}
public class Field {
private List<Column> columnList;
}
public class Column{
String id;
}
所以,这里我的工作流程是一个由多个Field组成的Table,一个Field将有多个Column。因此,在webUI上,我需要显示一个名称和字段下拉列表作为行。当用户选择一个字段时,我需要动态获取所选字段并渲染属于所选字段的列。这里,如何从Web UI获取所选字段。我尝试使用AjaxFormComponentUpdatingBehavior(“onchange”)。但我得到了那个下拉列表的所有领域。
我的DropDown选择如下:
IChoiceRenderer choiceRenderer = new ChoiceRenderer("Name", "id");
DropDownChoice dropDownChoice = new DropDownChoice("ddc");
dropDownChoice.setChoiceRenderer(choiceRenderer);
dropDownChoice.setChoices(table.getFieldsList());
dropDownChoice.setModel(new CompoundPropertyModel(new Field()));
dropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
//Following is returning all the List of Fields.
Object defaultModelObject = getModelObject();
}
});
如何处理此类情况。请帮忙......
答案 0 :(得分:2)
请参阅下面的完整示例。 主要部分是ViewModel(VM)和ViewModelLoader(VML)。 VM是您要在UI中显示的内容。 VML使用来自数据库的数据填充VM。 使用PropertyModel我绑定下拉项和选定的值。 要更新doropdown,我正在更新VM并将组件添加到AjaxRequestTarget。
public class PlayingWithDropDownPage extends WebPage {
public PlayingWithDropDownPage() {
final ViewModelLoader viewModelLoader = new ViewModelLoader();
final ViewModel viewModel = viewModelLoader.load();
IChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
final DropDownChoice dropDownChoice = new DropDownChoice("dropDown");
dropDownChoice.setChoiceRenderer(choiceRenderer);
dropDownChoice.setChoices(viewModel.getItemsModel());
dropDownChoice.setModel(viewModel.getSelectedModel());
dropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
viewModelLoader.load(viewModel);
target.add(dropDownChoice);
}
});
add(dropDownChoice);
}
public static class ViewModel implements Serializable {
private WhatToShow whatToShow;
private List<Item> items = new ArrayList<>();
private Item selected;
public IModel<List<Item>> getItemsModel() {
return new PropertyModel<>(this, "items");
}
public IModel<Item> getSelectedModel() {
return new PropertyModel<>(this, "selected");
}
}
public static class ViewModelLoader extends LoadableDetachableModel<ViewModel> {
@Override
protected ViewModel load() {
return load(new ViewModel());
}
protected ViewModel load(ViewModel vm) {
vm.items.clear();
if (vm.whatToShow == WhatToShow.City) {
vm.whatToShow = WhatToShow.Person;
vm.items.add(new Person("1", "John", "Smith"));
vm.items.add(new Person("2", "Robert", "Evans"));
vm.items.add(new Person("3", "Jeff", "Jones"));
} else {
vm.whatToShow = WhatToShow.City;
vm.items.add(new City("1", "London"));
vm.items.add(new City("2", "Moscow"));
vm.items.add(new City("3", "Kiev"));
}
return vm;
}
}
public static interface Item {
public String getId();
public String getName();
}
private enum WhatToShow {
Person,
City
}
public static class City implements Item {
public String id;
public String name;
public City(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
}
public static class Person implements Item {
public String id;
public String fname;
public String lname;
public Person(String id, String fname, String lname) {
this.id = id;
this.fname = fname;
this.lname = lname;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return String.format("%s %s", fname, lname);
}
}
}