带有choicerenderer的dropdownchoice,如何获取值
dropdownchoice with choicerenderer, how to get values
我在尝试获取某些值时遇到问题。这是我的情况(检票口)
我有一个下拉选项 study_template,我在填充 DDC 时没有问题,问题是当我尝试获取一些值(id 或名称)时。这是代码
ChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
study_template = new DropDownChoice( "study_template",new PropertyModel( this, "selectedTemplate" ),template_names , choiceRenderer);
template_names 是一个列表< SelectOption >,其中包含从数据库获取的值。这工作正常。
这是我用来填充 DDC
的 class
public class SelectOption implements java.io.Serializable {
private long id;
private String name;
public SelectOption(long id, String name ) {
this.id = id; this.name=name;
}
public long getId()
{return id; }
public String getName()
{return name; }
}
通常我可以用study_template.getModelObject()获取值,但在这种情况下它不起作用,我对获取id和name没有任何想法,我知道我需要 GETID() 和 GETNAME(),但我不知道如何使用它,任何帮助将不胜感激
您可以使用如下内容:
public class SpikePage extends WebPage {
class Person {
String id;
String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public SpikePage() {
Person employee = new Person("E001", "ABC");
Person manager = new Person("E002", "XYZ");
List personList = new ArrayList(2);
personList.add(employee);
personList.add(manager);
Form form = new Form("form");
final DropDownChoice personDropDownChoice = new DropDownChoice("person", new ArrayList(personList), new IChoiceRenderer() {
@Override
public Object getDisplayValue(Person object) {
return object.getId().concat("-").concat(object.getName());
}
@Override
public String getIdValue(Person object, int index) {
return object.getId();
}
});
personDropDownChoice.setOutputMarkupId(true);
personDropDownChoice.setNullValid(false);
form.add(personDropDownChoice);
final Label label = new Label("name");
label.setOutputMarkupId(true);
form.add(label);
personDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
Person selectedPerson = personDropDownChoice.getModelObject();
label.setDefaultModelObject("Hi ".concat(selectedPerson.getName()));
target.add(label);
}
});
add(form);
}
}
谢谢你的回答,我已经搞定了
我用这个
private SelectOption selectedTemplate;
然后
selectedTemplate.getId();
我在尝试获取某些值时遇到问题。这是我的情况(检票口)
我有一个下拉选项 study_template,我在填充 DDC 时没有问题,问题是当我尝试获取一些值(id 或名称)时。这是代码
ChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
study_template = new DropDownChoice( "study_template",new PropertyModel( this, "selectedTemplate" ),template_names , choiceRenderer);
template_names 是一个列表< SelectOption >,其中包含从数据库获取的值。这工作正常。
这是我用来填充 DDC
的 classpublic class SelectOption implements java.io.Serializable {
private long id;
private String name;
public SelectOption(long id, String name ) {
this.id = id; this.name=name;
}
public long getId()
{return id; }
public String getName()
{return name; }
}
通常我可以用study_template.getModelObject()获取值,但在这种情况下它不起作用,我对获取id和name没有任何想法,我知道我需要 GETID() 和 GETNAME(),但我不知道如何使用它,任何帮助将不胜感激
您可以使用如下内容:
public class SpikePage extends WebPage { class Person { String id; String name; public Person(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public SpikePage() { Person employee = new Person("E001", "ABC"); Person manager = new Person("E002", "XYZ"); List personList = new ArrayList(2); personList.add(employee); personList.add(manager); Form form = new Form("form"); final DropDownChoice personDropDownChoice = new DropDownChoice("person", new ArrayList(personList), new IChoiceRenderer() { @Override public Object getDisplayValue(Person object) { return object.getId().concat("-").concat(object.getName()); } @Override public String getIdValue(Person object, int index) { return object.getId(); } }); personDropDownChoice.setOutputMarkupId(true); personDropDownChoice.setNullValid(false); form.add(personDropDownChoice); final Label label = new Label("name"); label.setOutputMarkupId(true); form.add(label); personDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") { @Override protected void onUpdate(AjaxRequestTarget target) { Person selectedPerson = personDropDownChoice.getModelObject(); label.setDefaultModelObject("Hi ".concat(selectedPerson.getName())); target.add(label); } }); add(form); } }
谢谢你的回答,我已经搞定了
我用这个
private SelectOption selectedTemplate;
然后
selectedTemplate.getId();