GWT - How to sort celltable column?

12,264

I suppose you are reffering to Developer's Guide - Cell Table . If you got the first example running you can make the name colum sortable (add the little arrow to the name) with

table.getColumn(0).setSortable(true);

Btw this is the codes I had to addeto get your example running (just copy it after "RootPanel.get().add(table);" and replace "_01_scheduleDeferred" with your project name)

    // Create a data provider.
    ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);

    // Add the data to the data provider, which automatically pushes it to the
    // widget.
    List<Contact> list = dataProvider.getList();
    for (Contact contact : CONTACTS) {
      list.add(contact);
    }

    // Add a ColumnSortEvent.ListHandler to connect sorting to the
    // java.util.List.
    ListHandler<Contact> columnSortHandler = new ListHandler<_01_scheduleDeferred.Contact>(
        list);
    columnSortHandler.setComparator(nameColumn,
        new Comparator<_01_scheduleDeferred.Contact>() {
        @Override
        public int compare(_01_scheduleDeferred.Contact o1, _01_scheduleDeferred.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;
        }
        });
    table.addColumnSortHandler(columnSortHandler);

    // We know that the data is sorted alphabetically by default.
    table.getColumnSortList().push(nameColumn);
Share:
12,264
sap
Author by

sap

Updated on July 30, 2022

Comments

  • sap
    sap over 1 year

    I'm reading about how to add column sort functions to a cell table but I'm not understanding the code provided by google. Following is my celltable, How would I add make the nameColumn sort-able?

    public class CellTableExample implements EntryPoint {
    
        private static class Contact {
            private String address; 
            private String name;
    
            public Contact(String name, String address) {
                super();
                this.address = address; 
                this.name = name; 
            } 
        }
    
        // The list of data to display.
          private static List<Contact> CONTACTS = Arrays.asList(
            new Contact("John", "123 Fourth Road asdf asdf asdfasdf"),
            new Contact("Mary", "222 Lancer Lane")
    
          );
    
        @Override
        public void onModuleLoad() {
            CellTable<Contact> table = new CellTable<Contact>(); 
    
            //address column
            TextColumn<Contact> addressColumn = new TextColumn<Contact>(){
                @Override
                public String getValue(Contact contact) {
                    return contact.address;
                }
            };
    
            //name column
            TextColumn<Contact> nameColumn = new TextColumn<Contact>(){
                @Override
                public String getValue(Contact contact) {
                    return contact.name;
                }
            };
    
            // Add the columns.
            table.addColumn(nameColumn, "Name");
            table.addColumn(addressColumn, "Address");
    
            table.setRowCount(CONTACTS.size(), true);
            table.setRowData(0, CONTACTS);
    
            RootPanel.get().add(table); 
        }
    
    }