首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GWT-编辑器和子编辑器

GWT-编辑器和子编辑器
EN

Stack Overflow用户
提问于 2013-02-09 00:37:47
回答 1查看 816关注 0票数 0

我正在尝试运行一个带有子编辑器的编辑器示例。刷新父级时,子编辑器的值为空。类是Person和Address。主编辑器是:

代码语言:javascript
复制
    // editor fields
public TextField firstname;
public TextField lastname;
public NumberField<Integer> id;

public AddressEditor address = new AddressEditor();

public PersonEditor(Person p){
    asWidget();
}

@Override
public Widget asWidget() {
    setWidth(400);
    setBodyStyle("padding: 5px;");
    setHeaderVisible(false);

    VerticalLayoutContainer c = new VerticalLayoutContainer();

    id = new NumberField<Integer>(new IntegerPropertyEditor());
    // id.setName("id");
    id.setFormat(NumberFormat.getFormat("0.00"));
    id.setAllowNegative(false);
    c.add(new FieldLabel(id, "id"), new VerticalLayoutData(1, -1));

    firstname = new TextField();
    // firstname.setName("firstname");
    c.add(new FieldLabel(firstname, "firstname"), new VerticalLayoutData(1, -1));

    lastname = new TextField();
    lastname.setName("lastname");
    c.add(new FieldLabel(lastname, "lastname"), new VerticalLayoutData(1, -1));

    c.add(address);
    add(c);
    return this;

子编辑器:

代码语言:javascript
复制
public class AddressEditor extends Composite implements Editor<Address> {

private AddressProperties props = GWT.create(AddressProperties.class);
private ListStore<Address> store = new ListStore<Address>(props.key());
ComboBox<Address> address;

public AddressEditor() {
    for(int i = 0; i < 5; i ++)
        store.add(new Address("city" + i));
    address = new ComboBox<Address>(store, props.nameLabel());

    address.setAllowBlank(false);
    address.setForceSelection(true);
    address.setTriggerAction(TriggerAction.ALL);
    initWidget(address);
}

驱动程序就是在这里创建的:

代码语言:javascript
复制
private HorizontalPanel hp;

private Person googleContact;
PersonDriver driver = GWT.create(PersonDriver.class);

public void onModuleLoad() {

    hp = new HorizontalPanel();
    hp.setSpacing(10);

    googleContact = new Person();
    PersonEditor pe = new PersonEditor(googleContact);

    driver.initialize(pe);
    driver.edit(googleContact);

    TextButton save = new TextButton("Save");
    save.addSelectHandler(new SelectHandler() {

        @Override
        public void onSelect(SelectEvent event) {
            googleContact = driver.flush();
            System.out.println(googleContact.getFirstname() + ", " + googleContact.getAddress().getCity());
            if (driver.hasErrors()) {
                new MessageBox("Please correct the errors before saving.").show();
                return;
            }
        }
    });

googleContact.getFirstname()的值已填充,但googleContact.getAddress()始终为空。我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-09 03:07:26

AddressEditor需要映射到Address模型--目前,似乎还没有,除非Address只有一个getter/setter,称为getAddress()setAddress(Address),这并没有多大意义。

如果您只需要一个ComboBox<Address> (它已经实现了Editor<Address> ),那么可以考虑将该组合直接放入PersonEditor中。否则,您将需要将@Path("")添加到AddressEditor.address字段,以指示它应该直接编辑值本身,而不是子属性(即person.getAddress().getAddress())。

构建地址编辑器的另一种方法是在AddressEditor中列出Address类型的每个属性。这是驱动程序默认情况下所期望的,所以当它看到名为“address”的字段时,它会感到困惑。

关于代码本身的两个快速想法:不需要将一个人传递到PersonEditor中-这是驱动程序本身的工作。其次,您的编辑器字段不需要为public,它们不能为private

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14777225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档