2

Google 開発者ガイドから Cell Table の例を取り上げ、次の変更を加えました。

  • Java POJO の代わりにオーバーレイを使用する
  • EditTextCell を使用して 1 つの列を編集する

驚いたことに、コードを実行すると、Cell Table はプッシュされたオーバーレイ オブジェクトに追加のプロパティを追加しています。それらは次のようになります。

{"name":"John", "address":"123 Fourth Road"} {"name":"Mary", "address":"222 Lancer Lane"}

しかし、代わりに次のようになります。

{"name":"John", "address":"123 Fourth Road", "$H":1} {"name":"Mary", "address":"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());
        }
    }

}

問題の原因となっているのは編集可能な列であることを確認しました。削除しても、テーブルはオーバーレイを変更しません。

とにかく、これは非常に奇妙な動作です。ウィジェットが予期しないプロパティを追加できる場合、オーバーレイを使用するのは安全ではないと思います。

以前にこの問題に遭遇した人はいますか、またはこの動作はどこかに文書化されていますか? それを解決するためのヒントはありますか?

どうもありがとう

4

2 に答える 2

1

この問題に対する正しい回答が GWT 開発フォーラム (リンク)に投稿されました。

$H プロパティは JavaScriptObject#hashCode() (com.google.gwt.cire.client.impl.Impl#getHashCode(Object) 内) の実装に由来します。

あなたの場合、これは AbstractEditableCell が値キーの「ビューデータ」へのマップを維持していることと、アイテムを直接返すデフォルトの ProvidesKey 実装 (SimpleProvidesKey) の使用 (推測) によるものです。

そのため、レンダリング時に、EditTextCell は getViewData を呼び出します。これは、マップ内のキーを検索し (したがって、キーのハッシュコードが必要になるため、hashCode を呼び出す必要があります)、キーは JSO です (したがって、新しい $H プロパティ)。

私は、Celltable に ProvidesKey 実装 (たとえば、name プロパティを返す) を与えることで問題が解決すると信じています。

于 2011-01-21T22:39:10.790 に答える
0

CellTableがJSONをこっそり編集できるとは思えません。Firebug / Chrome_Developer_Tools / ...でこれらのオーバーレイを確認してください。問題がない場合は、次の行にバグがある可能性があります。

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

少なくとも2つの問題がありますJSONObject.toString。JSNIとの間でリストを渡すことはWebモードでは機能しますが、ホストモードtoString()およびJSNIでは失敗します。
2番目の問題では、これを試すことができるコメントがあります。

GWT.log( (String) new JSONObject(contact) );
于 2011-01-21T22:24:16.060 に答える