Carnell
Carnell

Reputation: 749

GWT 2.1 CellTable Column Header click events

Is there any way to add clickHandlers (or any type of handler) to the headers of the columns in a CellTable? I want to add some sorting functionality to my CellTable and I dont see any methods in the Column or Header classes that will allow this. I used this post to figure out how to use the CellTable.

Upvotes: 8

Views: 7385

Answers (4)

Ashwin Prabhu
Ashwin Prabhu

Reputation: 7624

There is no out of the box way of supporting sort as yet on the CellTable. However there is a manual workaround involving a lot of code drudgery. Refer the classes SortableHeader and SortableColumn in the bike shed under expenses sample. You will find the usage in com.google.gwt.sample.expenses.gwt.client.ExpenseDetails. You can use this until something concrete comes out in the next release.

check out directory: http://google-web-toolkit.googlecode.com/svn/trunk/bikeshed

Upvotes: 2

user1227465
user1227465

Reputation:

   CellTable<Contact> table = new CellTable<Contact>();

    // Create name column.
    final TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.name;
      }
    };
    // Create a data provider.
    ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);
    final List<Contact> list = dataProvider.getList();
    for (Contact contact : CONTACTS) {
      list.add(contact);
    }
    final ListHandler<Contact> columnSortHandler = new ListHandler<Contact>(
            list);
    Header<String> columnHeader = new Header<String>(new ClickableTextCell()) {
        @Override
        public String getValue() {
            return "Name";
        }
    };

    columnHeader.setUpdater(new ValueUpdater<String>() {
        @Override
        public void update(String value) {
            if (Window.confirm("Want to do?")){
                nameColumn.setSortable(true);
                columnSortHandler.setComparator(nameColumn,
                        new Comparator<Contact>() {
                          public int compare(Contact o1, Contact o2) {
                            if (o1 == o2) {
                              return 0;
                            }

                            // Compare the name columns.
                            if (o1 != null) {
                              return (o2 != null) ? o1.name.compareTo(o2.name) : 1;
                            }
                            return -1;
                          }
                        });
            } else nameColumn.setSortable(false);
        }
    });
    // Make the name column sortable.
    nameColumn.setSortable(false);

    // Create address column.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.address;
      }
    };

    // Add the columns.
    table.addColumn(nameColumn, columnHeader);
    table.addColumn(addressColumn, "Address");




    // Add the data to the data provider, which automatically pushes it to the
    // widget.


    // Add a ColumnSortEvent.ListHandler to connect sorting to the
    // java.util.List.
    //------------------ Code to add --------------------------------//
    VerticalPanel vp = new VerticalPanel();




    table.addColumnSortHandler(columnSortHandler);
  //------------------ Code end --------------------------------//
    // We know that the data is sorted alphabetically by default.
    table.getColumnSortList().push(nameColumn);

    // Add it to the root panel.
    vp.add(table);
    RootPanel.get().add(vp);

Upvotes: 0

Italo Borssatto
Italo Borssatto

Reputation: 15679

Workaround for click events:

Header<String> columnHeader = new Header<String>(new ClickableTextCell()) {
    @Override
    public String getValue() {
        return columnName;
    }
};

columnHeader.setUpdater(new ValueUpdater<String>() {
    @Override
    public void update(String value) {
        Window.alert("Header clicked!");
    }
});

table.addColumn(column, columnHeader);

Upvotes: 8

Jason
Jason

Reputation: 9

With the final release of GWT 2.1, has there been any support for sortable columns added to the CellTable? Or is it still a roll your own solution after looking at the bikeshed example?

Upvotes: 0

Related Questions