views:

548

answers:

1

Hello everyone!

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

+1  A: 

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.

Don Roby
This worked perfectly. Thanks!!!
David
You're welcome! Glad I could help.
Don Roby
Hi donroby@, I've got another question related to the same code. I've posted it here, could you take a look if you are able?Thanks!http://stackoverflow.com/questions/2777448/page-expired-issue-with-back-button-and-wicket-sortabledataprovider-and-datatable
David
I've looked, but will have to think a bit about an answer. Also posted a suggestion there for improving the question formatting.
Don Roby