GWT-如何对celltable列进行排序? [英] GWT - How to sort celltable column?

查看:122
本文介绍了GWT-如何对celltable列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关如何向单元格表中添加列排序功能的信息,但我不理解google提供的代码.以下是我的单元格,我该如何添加名称列以使其可排序?

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); 
    }

}

推荐答案

我想您要使用开发人员指南-单元格表.如果您运行了第一个示例,则可以通过以下方式使名称colum可排序(在名称中添加小箭头):

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);

这是我必须添加代码才能运行您的示例的代码(只需将其复制到"RootPanel.get().add(table);"之后,然后用您的项目名称替换"_01_scheduleDeferred"即可)

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);

这篇关于GWT-如何对celltable列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆