我从细胞表中示例 Google开发人员指南 并进行以下更改:

  • 使用覆盖层代替Java Pojos
  • 使用EdittextCell编辑一列

令我惊讶的是,在运行代码时,单元表正在向推入其中的覆盖对象添加额外的属性。他们应该看起来像:

{“名称”:“ John”,“地址”:“ 123 Fourth Road”} {“名称”:“ MARY”,“地址”:“ 222 Lancer Lane”}

但是他们看起来像:

{“名称”:“ John”,“地址”:“第四道路”,“ $ h”:1} {“名称”:“玛丽”,“地址”:“ 222 Lancer Lane”,“ $ h”: 2}

这是证明问题的修改代码:

import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Overlay implements EntryPoint {

    private static class Contact extends JavaScriptObject {

        protected Contact() {}

        public static native Contact create(String name, String address) /*-{
            return {"name" : name , "address" : address};
        }-*/;
        public final native String getName() /*-{
            return this["name"];
        }-*/;   
        public final native void setName(String name) /*-{
            this["name"] = name;
        }-*/;
        public final native String getAddress() /*-{
            return this["address"];
        }-*/;   
        public final native void setAddress(String address) /*-{
            this["address"] = address;
        }-*/;
    }

    private static List<Contact> CONTACTS = Arrays.asList(
            Contact.create("John", "123 Fourth Road"),
            Contact.create("Mary", "222 Lancer Lane"));

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {

        CellTable<Contact> table = new CellTable<Contact>();
        // Create name column.
        Column<Contact, String> nameColumn = new Column<Contact, String>(new EditTextCell()) {
            public String getValue(Contact object) {
                return object.getName();
            }
        };
        // Create address column.
        TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
            public String getValue(Contact contact) {
                return contact.getAddress();
            }
        };
        // Add the columns.
        table.addColumn(nameColumn, "Name");
        table.addColumn(addressColumn, "Address");      
        table.setRowCount(CONTACTS.size(), true);
        // Push the data into the widget.
        printList();
        table.setRowData(0, CONTACTS);      
        printList();        
        RootPanel.get().add(table);
    }

    private void printList() {
        for(Contact contact : CONTACTS) {
            GWT.log(new JSONObject(contact).toString());
        }
    }

}

我已经检查了导致问题的可编辑列。如果我删除它,则表不会修改我的覆盖层。

无论如何,这是一个非常奇怪的行为。如果您的小部件可以将它们添加出意外的属性,我认为使用叠加层的工作不安全。

有人以前遇到过这个问题吗?还是在任何地方记录了此行为?有任何提示可以解决它吗?

非常感谢

有帮助吗?

解决方案

该问题的正确答案已发布在GWT开发论坛(关联):

$ h属性来自JavascriptObject#Hashcode()的实现(在com.google.gwt.cire.client.impl.impl.impl#gethashcode(object))。

在您的情况下,这是由于AbstractedItableCell维护其“查看数据”的值键,以及您对默认值的使用(我猜)提供了键实现(Simple Provideskey),该键直接返回该项目。

因此,当渲染时,EdittextCell调用getViewData,它查找地图中的键(因此需要键的哈希码,因此需要调用hashCode),而键是您的JSO(因此是新的$ h属性)。

我相信,提供提供的kepplykey实施(在您的情况下,将名称属性归还给CellTable可以解决您的问题。

其他提示

我怀疑Celltable能否偷偷编辑您的JSON。检查firebug/chrome_developer_tools/...的叠加层,如果它们还可以,那么很可能是在此行中的错误:

GWT.log(new JSONObject(contact).toString());

至少有两个问题 JSONObject.toString: 在Web模式下访问/来自JSNI的列表工作,但在托管模式下失败 & tostring()和jsni.
在第二期中有 评论 据此,您可以尝试以下方法:

GWT.log( (String) new JSONObject(contact) );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top