如何在GWTP中创建动态表

时间:2014-10-03 15:14:40

标签: gwt smartgwt gwtp gwt-platform gwt-places

我是新的GWTP用户,我不确定如何在GWTP中创建表。我知道如何在GWT中制作一个。

// Create a CellTable.
CellTable<Contact> table = new CellTable<Contact>();
// Create name column.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
  @Override
  public String getValue(Contact contact) {
    return contact.name;
  }
};

但这似乎并不适用于GWTP。有人可以帮我在GWTP程序中按下按钮获取值。

1 个答案:

答案 0 :(得分:0)

我知道你在一个星期前问了这个问题,但是你可能仍然坚持下去,所以这里就是这样。您只需确保分别在PresenterView中放置正确的逻辑位。

与原则上没有GWTP的MVP(Model-View-Presenter)没有区别:

您的Presenter的工作是让数据填满CellTable,并将其传递给View

public class TablePresenter extends Presenter<TablePresenter.MyView, TablePresenter.MyProxy>
{
    public interface MyView extends View
    {
        void addData(List<Contact> accounts); // pass data to view
    }

    // proxy and constructor omitted for brevity...

    @Override
    protected void onReveal()
    {
        super.onReveal();

        // server action to get contacts
        dispatchAsync.execute(new GetContacts(), new AsyncCallback<GetContactsResult>()
        {
            @Override
            public void onSuccess(GetContactsResult result)
            {                   
                getView().addData(result.getContacts());
            }
        });
    }
}

您的View的工作是初步设置CellTable及其Column,以及从Presenter接收数据。在这里,我使用TextColumn

显示ColumnButtonCell
public class TableView extends View implements TablePresenter.MyView
{
    @UiField
    CellTable<Contact> table;

    // use a dataprovider to hold the data
    private ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

    // COLUMNS
    TextColumn<Contact> nameColumn;
    Column<Contact, String> buttonColumn;

    @Inject
    public AccountsView(Binder uiBinder)
    {
        initWidget(uiBinder.createAndBindUi(this));

        initTable();
    }

    private void initTable()
    {
        nameColumn = new TextColumn<Contact>()
        {
            @Override
            public String getValue(Contact object)
            {
                return object.name;
            }
        };
        // now add the column to the table
        table.addColumn(nameColumn, "Name");

        buttonColumn = new Column<Contact, String>(new ButtonCell())
        {
            // the text of the button
            @Override
            public String getValue(Contact object)
            {
                return "Delete " + object.name;
            }
        };

        // set the button action
        deleteColumn.setFieldUpdater(new FieldUpdater<Contact, String>()
        {
            @Override
            public void update(int index, Contact object, String value)
            {
                // whatever you want to do when you click the button
                Window.alert("You pressed " + object.name);
            }
        });
        fileTable.addColumn(deleteColumn);

        // link dataprovider to the table
        dataProvider.addDataDisplay(table);
    }

    @Override
    public void addData(List<Contact> contacts)
    {
        // clear the dataProvider's list
        dataProvider.getList().clear();

        // pass the data into the list
        dataProvider.setList(contacts);

    }

}

然后在你的UiBinder中:

<g:HTMLPanel>
    <b:CellTable ui:field="table" />
</g:HTMLPanel>