开发者

How do I display a Wicket Datatable, sorted by a specific column by default?

I have a question regarding Wicket's Datatable. I am currently using DataTable to display a few columns of data.

My table is set up as follows:

DataTable<Column> dataTable = new DataTable<Column>("columnsTable", columns, provider, maxRowsPerPage) {
            @Override
            protected Item<Column> newRowItem(String id, int index, IModel<Column> model) {
                return new OddEvenItem<Column>(id, index, model);
            }
        };

The columns look like so:

columns[0] =  new PropertyColumn<Column>(new Model<String>("Description"), "description", "description");
columns[1] =  new PropertyColumn<Column>(new Model<String>("Logic"), "columnLogic");
columns[2] =  new PropertyColumn<Column>(new Model<String>("Type"), "dataType", "dataType");

Here is my column data provider:

public class ColumnSortableDataProvider extends SortableDataProvider<Column> {
private static final long serialVersionUID = 1L;

private List<Column> list = null;

public ColumnSortableDataProvider(Table table) {
    this.list = Arrays.asList(table.getColumns().toArray(new Column[0]));
}

public ColumnSortableDataProvider(List<Column> list) {
    this.list = list;
}

@Override
public Iterator<? extends Column> iterator(int first, int count) {
    /*
    first - first row of data
    count - minimum number of elements to retrieve
    So this method returns an iterator capable of iterating over {first, first+count} items
     */ 
    Iterator<Column> iterator = null;

    try {
        if(getSort() != null) {
            Collections.sort(list, new Comparator<Column>() {
                private static final long serialVersionUID = 1L;

                @Override
                public int compare(Column c1, Column c2) {
         开发者_C百科           int result=1;
                    PropertyModel<Comparable> model1= new PropertyModel<Comparable>(c1, getSort().getProperty());
                    PropertyModel<Comparable> model2= new PropertyModel<Comparable>(c2, getSort().getProperty());

                    if(model1.getObject() == null && model2.getObject() == null) 
                        result = 0;
                    else if(model1.getObject() == null) 
                        result = 1;
                    else if(model2.getObject() == null) 
                        result = -1;
                    else 
                        result = ((Comparable)model1.getObject()).compareTo(model2.getObject());

                    result = getSort().isAscending() ? result : -result;

                    return result;
                }
            });
        }

        if (list.size() > (first+count))
            iterator = list.subList(first, first+count).iterator();
        else
            iterator = list.iterator();
    } 
    catch (Exception e) {
        e.printStackTrace();
    }

    return iterator;
}

Sorting by clicking a column works perfectly, but I would like the table to initially be sorted, by default, by the Description column. I am at a loss to do this. If you need to see some other code, please let me know.

Thank you in advance!!! - D


There are a couple overloaded setSort methods and a setSortState method in SortableDataProvider that can be called to set this.

For sample code using setSort, see the SortableContactDataProvider source in the Sorting DataView example on this examples page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜