The gwt-incubator has PagingScrollTable widget that provides
pagination. if you want to try you can follow
http://code.google.com/p/google-web-toolkit-incubator/wiki/MakingIncubatorBetter
and get the latest version of the project. There are a example, how
use the widget.

Basically you need

1) Define a table model derived from MutableTableModel that is used to
fetch data. This code is executed at the client. Relevant code:

public class ClienteTableModel extends MutableTableModel<Cliente> {
        @Override
        public void requestRows(
                        final Request request,
                        final 
com.google.gwt.gen2.table.client.TableModel.Callback<Cliente>
callback) {

                  Servico.Util.getInstance().requestRows(request, exemplo,
                                new 
AsyncCallback<SerializableResponse<Cliente>>(){
                                                        public void 
onFailure(Throwable caught) {
                                                                
callback.onFailure(new Exception("falha rpc"));
                                                        }

                                                        public void 
onSuccess(SerializableResponse<Cliente> result) {
                                                                
callback.onRowsReady(request, result);
                                                        }

                });

        }


2) Servico.Util.getInstance().requestRows is a class that is called on
the server using RPC. That's why We need to provide an AsyncCallBack
to that is going to be executed assyncronously when the server
responds, and feed the widget with data on callback.onRowsReady
(request,result).

You need to follow some small rules to let gwt rpc do its magic:

An interface and it's Async countepart which is code shared between
server and client. (Im my case i have just one project) Removed the
import, for brevity

An interface celebrating the contract between the server and the
client:

package unipac.client;
@RemoteServiceRelativePath("servico")
public interface Servico extends RemoteService {

        public SerializableResponse<Cliente> requestRows(Request request,
Cliente qbe);

        public static class Util {
                public static ServicoAsync getInstance() {
                        return GWT.create(Servico.class);
                }
        }

}

An interface counter-part that . Note that its methods returns void.
The SerializableResponse<Cliente> that the above interface returns
will be got by the callback object. Code Shared between the client and
the server too. It's necessary due the asyncronous nature of Ajax
calls.

package unipac.client;
public interface ServicoAsync {
        public void requestRows(Request request, Cliente qbe,
AsyncCallback<SerializableResponse<Cliente>>  callback);
}

Finally the implementation... (server code only)

package unipac.server;
public class ServicoImpl extends RemoteServiceServlet implements
Servico {

        public SerializableResponse<Cliente> requestRows(Request request,
Cliente qbe) {

                SerializableResponse<Cliente> result = null;
                Session sessao = 
HibernateUtil.getSessionFactory().openSession();


    Transaction tran = sessao.beginTransaction();
    Criteria crit = sessao.createCriteria(Cliente.class)
                                  .addOrder(Order.asc("nome"))
                                  .setFirstResult(request.getStartRow())
                                  .setMaxResults(request.getNumRows());

    if (qbe != null) {
                Example exemplo = Example.create(qbe).ignoreCase().enableLike
(MatchMode.ANYWHERE);
      crit.add(exemplo);
    }

                result = new SerializableResponse<Cliente>( crit.list() );
                tran.commit();
                return result;
        }

}

Things to note.  ServicoImpl implements the first interface and
extends from RemoteServiceServlet (Yes this is a servlet). Look at
requestRows parameters: Request is used to tell the server the
starting row and the number of rows, in order to limit the results.
Cliente qbe is an object that im passing from the client to the server
to query by example which instances I want. The code uses Hibernate,
not JPA but you probably can understand.

3) Back to client side. you need:

a) Instantiate the table model.

ClienteTableModel tableModel = new ClienteTableModel();

b) One tableDefinition

            TableDefinition<Cliente> tableDef = createTableDefinition();


          private TableDefinition<Cliente> createTableDefinition() {

                    // Create the table definition
                    DefaultTableDefinition<Cliente>tableDefinition = new
DefaultTableDefinition<Cliente>();

                    // Set the row renderer
                    String[] rowColors = new String[] {"#FFFFDD", "#EEEEEE"};
                    tableDefinition.setRowRenderer(new 
DefaultRowRenderer<Cliente>
(rowColors));

                    // Nome
                    {
                        AbstractColumnDefinition<Cliente, String> columnDef = 
new
AbstractColumnDefinition<Cliente, String>() {
                        public String getCellValue(Cliente rowValue) {
                          return rowValue.getNome();
                        }

                        public void setCellValue(Cliente rowValue, String 
cellValue)
{
                          rowValue.setNome(cellValue);
                        }

                        };
                      tableDefinition.addColumnDefinition(columnDef);
                    }

                    // Endereço
                    {
                        AbstractColumnDefinition<Cliente, String> columnDef = 
new
AbstractColumnDefinition<Cliente, String>() {
                        public String getCellValue(Cliente rowValue) {
                          return rowValue.getEndereco();
                        }

                        public void setCellValue(Cliente rowValue, String 
cellValue)
{
                          rowValue.setEndereco(cellValue);
                        }

                        };
                      tableDefinition.addColumnDefinition(columnDef);
                    }


                    return tableDefinition;
                  }

TableDefinition is a collection of column definition that  is used to
tell which columns you are going to have.


c) PagingScrollTable is an widget that needs 2 or 3 tables to work.
It's simple:

<A HEADER TABLE> FixedWidthFlexTable
<A DATA TABLE> FixedWidthGrid. Where your columns definition are shown
<OPTIONALLY A FOOTER TABLE>

It will orchestrate these objects. Let's create them. First the header
table

            FixedWidthFlexTable headerTable = new FixedWidthFlexTable();
            headerTable.setText(0, 0, "Nome");
            headerTable.setText(0, 1, "Endereço");

This matches the number of columns in TableDefinition (2).

The data table:
             FixedWidthGrid dataTable = new FixedWidthGrid();


Now let's create the PagingScrollTable:

pagingScrollTable = new PagingScrollTable(tableModel, dataTable,
headerTable, tableDef);
To create PST whe feed the table model, the data table, the header
table and the field definition.


And set some properties:

            pagingScrollTable.setPageSize(19);
            pagingScrollTable.setEmptyTableWidget(new HTML(
                "There is no data to display"));

            // Setup the formatting
            pagingScrollTable.setCellPadding(3);
            pagingScrollTable.setCellSpacing(0);
            pagingScrollTable.setResizePolicy
(ScrollTable.ResizePolicy.FILL_WIDTH);
            //pagingScrollTable.setHeight("400px");
            pagingScrollTable.setSize("500px", "450px");
            pagingScrollTable.setColumnWidth(0, 150);


You can use the Paging widget (First, Prior, Next, Last buttons) that
comes with gwt-incubator.

            PagingOptions pagingOptions = new PagingOptions
(pagingScrollTable);
            painel.add(pagingOptions);

In my case painel is a VerticalPanel that I'm using to layout.

If you have followed until here I hope you have a PST working.

Be aware: My english is terrible (you noted already), and I'm starting
with GWT. The code is the example I got from gwt-incubator and updated
to work with Hibernate. It's a prototype, but it's working.

Regards,

Geraldo Lopes





Andrey wrote:
> Hello,
>
> Could somebody give the link for simple sample application (or lib, or
> etc...) providing and showing GWT approach for viewing the data from
> DB table.
>
> So, for example, I have JPA Entities and I would like to use some GWT
> lib to view them (with pagination and table decorators?...).
>
> In JSF world, Woodstock components provide such a tool, as an
> example...
>
> Regards,
>
> -Andrey
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to