【问题标题】:GWT-Editors and sub-editorsGWT 编辑器和副编辑器
【发布时间】:2013-02-09 03:07:25
【问题描述】:

我正在尝试运行带有子编辑器的编辑器示例。 刷新父级时,子编辑器的值为空。 类是人员和地址。 主编是:

    // 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;

副主编:

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);
}

这是创建驱动程序的地方:

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() 始终为空。 我错过了什么?

【问题讨论】:

    标签: gwt gxt gwt-editors


    【解决方案1】:

    AddressEditor 需要映射到 Address 模型 - 目前似乎不需要,除非 Address 只有一个 getter/setter,称为 getAddress()setAddress(Address),这不会真的很有意义。

    如果您只想要一个ComboBox&lt;Address&gt;(它已经实现了Editor&lt;Address&gt;),请考虑将该组合直接放在PersonEditor 中。否则,您需要将@Path("") 添加到AddressEditor.address 字段,以表明它应该直接编辑值本身,而不是子属性(即person.getAddress().getAddress())。

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

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

    【讨论】:

    • 感谢您的回答。我有点困惑如何设置子对象。我试图添加 @Path("") ComboBox
      地址;但它没有用。我把所有东西都放在一个班级里,效果很好。再次感谢!
    • @Berna,如果您认为这是一个有效的答案,您应该点击空白复选标记接受它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多