具有覆盖类型问题的GWT单元小部件
我从中获取了单元格表示例,并进行了以下更改:具有覆盖类型问题的GWT单元小部件,gwt,widget,overlay,data-presentation,Gwt,Widget,Overlay,Data Presentation,我从中获取了单元格表示例,并进行了以下更改: 使用覆盖而不是Java POJO 使用EditTextCell编辑一列 让我惊讶的是,在运行代码时,单元格表正在向推入其中的覆盖对象添加一个额外的属性。它们应该看起来像: {“姓名”:“约翰”,“地址”:“第四路123号”} {“姓名”:“玛丽”,“地址”:“兰瑟巷222号”} 但它们看起来却像: {“姓名”:“约翰”,“地址”:“第四路123号”,“$H”:1} {“姓名”:“玛丽”,“地址”:“兰瑟巷222号”,“$H”:2} 下面是演示该问
- 使用覆盖而不是Java POJO
- 使用EditTextCell编辑一列
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());
}
}
}
我已检查是否是可编辑列导致问题。如果我删除它,表不会修改我的覆盖
无论如何,这是一种非常奇怪的行为。如果您的小部件可以添加意外属性,我觉得使用覆盖是不安全的
以前是否有人遇到过此问题,或者是否在任何地方记录了此行为?有什么解决办法吗
非常感谢我怀疑CellTable能否偷偷编辑您的JSON。在Firebug/Chrome\u Developer\u Tools/中检查这些覆盖。。。如果它们正常,则最有可能出现的错误如下:
GWT.log(new JSONObject(contact).toString());
JSONObject.toString至少有两个问题:&。
在第二期中,您可以尝试以下方法:
GWT.log( (String) new JSONObject(contact) );
这个问题的正确答案发布在GWT开发论坛()上:
$H属性来自
实施
JavaScriptObject#hashCode()(在
com.google.gwt.cire.client.impl.impl#getHashCode(Object))
在你的情况下,这是由于
维护地图的AbstractEditableCell
“查看数据”的值键数目,
我猜你使用了默认值
ProvidesKey实现
(SimpleProvideKey)直接
返回项目
因此,在渲染时,EditTextCell
调用getViewData,它将查找
输入地图(因此需要
密钥的哈希代码,因此调用
hashCode),关键是您的JSO
(因此,新的$H财产)
我相信给一个ProvidesKey
实现(在您的情况下,返回
将名称属性(例如)添加到
Celltable可以解决你的问题
谢谢你的回答。是JavaScriptObject的hashCode()方法将$H属性添加到覆盖中,请参阅我的答案。感谢您提供详细的答案。不幸的是,使用ProvidesKey并不能解决我的问题。我将所有详细信息发布在: