Click
Click Examples

Source Viewer : WEB-INF/classes/org/apache/click/examples/page/table/TableSorting.java

package org.apache.click.examples.page.table;

import java.util.List;

import javax.annotation.Resource;

import org.apache.click.control.Column;
import org.apache.click.control.Table;
import org.apache.click.examples.domain.Customer;
import org.apache.click.examples.page.BorderPage;
import org.apache.click.examples.service.CustomerService;
import org.apache.click.dataprovider.DataProvider;
import org.springframework.stereotype.Component;

/**
 * Provides an demonstration of Table column sorting using a database.
 */
@Component
public class TableSorting extends BorderPage {

    private static final long serialVersionUID = 1L;

    @Resource(name="customerService")
    private CustomerService customerService;

    public TableSorting() {
        final Table table = new Table("table");

        // Setup customers table
        table.setClass(Table.CLASS_SIMPLE);
        table.setHoverRows(true);
        table.setSortable(true);

        Column column = new Column("id");
        column.setSortable(false);
        table.addColumn(column);

        table.addColumn(new Column("name"));

        column = new Column("email");
        column.setAutolink(true);
        table.addColumn(column);

        column = new Column("age");
        column.setTextAlign("center");
        table.addColumn(column);

        column = new Column("holdings");
        column.setFormat("${0,number,#,##0.00}");
        column.setTextAlign("right");
        table.addColumn(column);

        column = new Column("active");
        column.setTextAlign("center");
        table.addColumn(column);

        // Return sorted data to the table.
        table.setDataProvider(new DataProvider<Customer>() {
            public List<Customer> getData() {
                boolean useSharedCache = true;
                return customerService.getCustomersSortedBy(table.getSortedColumn(),
                                                            table.isSortedAscending(),
                                                            useSharedCache);
            }
        });

        table.setSorted(true);

        addControl(table);
    }

}