wicket FilterToolBar demo2

----UsersPage----

import org.apache.wicket.extensions.markup.html.repeater.data.table.*;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.TextFilteredPropertyColumn;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import wcpackage.domain.entities.User;
import wcpackage.service.UserService;
import wcpackage.webapplication.view.BasePage;

public class UsersPage extends BasePage {


    @SpringBean
    private UserService userService;

    private User user;

    public UsersPage() {
        addUsersDataTable();
        addUsersAddForm();
    }

    private void addUsersDataTable() {
        IColumn<User>[] columns = new IColumn[3];

        columns[0] = new PropertyColumn<User>(new Model("Login"), "login", "login");
        columns[1] = new PropertyColumn<User>(new Model("First Name"), "firstName", "firstName");
        columns[2] = new TextFilteredPropertyColumn<User, String>(new Model("Last Name"), "lastName", "lastName");

        UsersProvider usersProvider = new UsersProvider(userService);
        DataTable<User> users = new DataTable<User>("users", columns, usersProvider, 10);
        users.addBottomToolbar(new NavigationToolbar(users));
        users.addTopToolbar(new HeadersToolbar(users, usersProvider));
        FilterForm<User> filterForm = new FilterForm<User>("filterForm", usersProvider);
        users.addTopToolbar(new FilterToolbar(users, filterForm, usersProvider));
        filterForm.add(users);
        add(filterForm);
    }

    private void addUsersAddForm() {
        user = new User();

        Form form = new Form("form", new CompoundPropertyModel(user));
        form.add(new TextField("login"));
        form.add(new TextField("firstName"));
        form.add(new TextField("lastName"));
        form.add(new Button("addButton") {
            @Override
            public void onSubmit() {
                userService.createNewUser(user);
            }
        });
        add(form);
    }
}

--------------------------------UsersProvider---------------------------------------------------

 

import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import wcpackage.domain.entities.User;
import wcpackage.service.UserService;

import java.util.Iterator;


public class UsersProvider extends SortableDataProvider<User> implements IFilterStateLocator<User> {

    private UserService userService;

    private User filterState;

    public UsersProvider(UserService userService) {
        this.userService = userService;
        filterState = new User();
        setSort("login", true);
    }

    public Iterator<? extends User> iterator(int first, int count) {

        System.out.println("filter: " + filterState.toString());

        String sortType = getSort().isAscending() ? "asc" : "desc";
        return userService.find(first, count, getSort().getProperty(), sortType).iterator();
    }

    public int size() {
        return userService.numberOfUsers();
    }

    public IModel<User> model(User object) {
        return new Model(object);
    }

    public void detach() {

    }

    public User getFilterState() {
        return filterState;
    }

    public void setFilterState(User state) {
        this.filterState = state;
    }
}

-----------------------------html---------------------

 

<html xmlns:wicket="http://www.w3.org/1999/xhtml">

<wicket:head>
    <title></title>
</wicket:head>
<body>
<wicket:extend>


    <br/>

    <form wicket:id="filterForm">
        <input type="hidden" name="tracker" wicket:id="focus-tracker"/>
        <span wicket:id="focus-restore">[call to focus restore script]</span>
        <table cellspacing="0" wicket:id="users">[users]</table>
    </form>
    <form wicket:id="form">
        First name: <input wicket:id="firstName"/>
        Last name: <input wicket:id="lastName"/>
        Login: <input wicket:id="login"/>
        <button wicket:id="addButton">add</button>
    </form>

</wicket:extend>
</body>
</html>

你可能感兴趣的:(wicket,FilterToolBar,demo2)