如何在提交“搜索”按钮后刷新Wicket DataTable

时间:2016-02-02 21:59:31

标签: search button datatable refresh wicket

我想在提交搜索按钮后刷新Wicket DataTable。 这是代码:

public class SearchPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private SearchService searchService;

    public SearchPanel(String id,final SearchService searchService) {
        super(id);
        this.searchService = searchService;
        add(new SearchForm("searchForm"));
    }

    private class SearchForm extends Form {

        private static final long serialVersionUID = 1L;
        private String searchString;

        public SearchForm(String id) {
            super(id);
            add(new TextField("searchString", new PropertyModel(this, "searchString")));
        }

        public void onSubmit() {
            PageParameters params = new PageParameters();
            searchService.findByTitleProperty(params.get("searchString").toString());
        }

        public String getSearchString() {
            return searchString;
        }

        public void setSearchString(String searchString) {
            this.searchString = searchString;
        }
    }
}

我的"主页"如下:

public class BooksTablePage extends WebPage implements SearchService {

    private static final long serialVersionUID = 1L;
    @SpringBean
    private BookService service;
    private List<IColumn<BookDto, String>> columns;
    private SortableBookDtoDataProvider sortableBookDtoDataProvider;
    private DataTable table;

    public BooksTablePage() {

        add(new SearchPanel("searchPanel",this));

        columns = new ArrayList<IColumn<BookDto, String>>();

        columns.add(new PropertyColumn(new Model<String>("ID"), "id"));
        columns.add(new PropertyColumn(new Model<String>("ISBN"), "isbn"));
        columns.add(new PropertyColumn(new Model<String>("TITLE"), "title"));
        columns.add(new PropertyColumn(new Model<String>("AUTHOR"), "author",
            "author"));
        columns.add(new PropertyColumn(new Model<String>("COPIES"), "copies",
            "copies"));

        //sortableBookDtoDataProvider = new SortableBookDtoDataProvider(service,"Brave");
        sortableBookDtoDataProvider = new SortableBookDtoDataProvider(service);
        table = new DataTable("datatable", columns, sortableBookDtoDataProvider, 3);
        table.addTopToolbar(new HeadersToolbar(table, (ISortStateLocator) table.getDataProvider()));
        add(table);

    }

    @Override
    public void findByTitleProperty(String value) {
        System.out.println("Search string: "+value);
        sortableBookDtoDataProvider = new SortableBookDtoDataProvider(service,value);
        table = new DataTable("datatable", columns, sortableBookDtoDataProvider, 3);
        table.addTopToolbar(new HeadersToolbar(table, (ISortStateLocator) table.getDataProvider()));
        this.replace(table);
    }
}

}

SearchService如下:

public interface SearchService {

    public void findByTitleProperty(String value);

}

这是BooksTablePage.html

<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Wicket Library Application</title>
  <link rel="stylesheet" href="style.css" type="text/css" media="screen" title="Stylesheet" />
</head>

<body>
  <span wicket:id="searchPanel"></span>
  <table wicket:id="datatable"></table>
</body>

</html>

SortableBookDtoDataProvider:

public class SortableBookDtoDataProvider extends SortableDataProvider<BookDto, String> {

    private static final long serialVersionUID = 1L;

    private static List<BookDto> list = new ArrayList<BookDto>();

    private SortableBookDtoDataProviderComparator comparator = new SortableBookDtoDataProviderComparator();

    public SortableBookDtoDataProvider(BookService service) {
        list.addAll(service.findAll());
        setSort("author", SortOrder.ASCENDING);
    }

    public SortableBookDtoDataProvider(BookService service,String title) {
        list.addAll(service.findByTitle(title));
        setSort("author", SortOrder.ASCENDING);
    }

    @Override
    public Iterator<BookDto> iterator(long first, long count) {
        List<BookDto> newList = new ArrayList<BookDto>(list);
        Collections.sort(newList, comparator);
        return newList.subList(safeLongToInt(first), safeLongToInt(first + count)).iterator();
    }

    @Override
    public long size() {
        return list.size();
    }

    @Override
    public IModel<BookDto> model(final BookDto object) {
        return Model.of(object);
    }

    @Override
    public void setSort(String property, SortOrder order) {
        super.setSort(property, order);
    }

    public int safeLongToInt(long l) {
        if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
            throw new IllegalArgumentException
                (l + " cannot be cast to int without changing its value.");
        }
        return (int) l;
    }

    class SortableBookDtoDataProviderComparator implements Comparator<BookDto>, Serializable {
        public int compare(final BookDto b1, final BookDto b2) {
            PropertyModel<Comparable> model1 = new PropertyModel<Comparable>(
                b1, getSort().getProperty());
            PropertyModel<Comparable> model2 = new PropertyModel<Comparable>(
                b2, getSort().getProperty());

            int result = model1.getObject().compareTo(model2.getObject());

            if (!getSort().isAscending()) {
                result = -result;
            }

            return result;
        }
    }
}

4 个答案:

答案 0 :(得分:2)

由于您不使用Ajax提交表单,Wicket将与DataTable一起重新整理整个页面。

如果您需要使用Ajax提交,那么您只需要执行target.add(dataTable)

答案 1 :(得分:0)

public class SortableBookDtoDataProvider extends SortableDataProvider<BookDto, String> {

private static final long serialVersionUID = 1L;

private static List<BookDto> list = new ArrayList<BookDto>();

private SortableBookDtoDataProviderComparator comparator = new SortableBookDtoDataProviderComparator();

public SortableBookDtoDataProvider(BookService service) {
    list.addAll(service.findAll());
    setSort("author", SortOrder.ASCENDING);
}

public SortableBookDtoDataProvider(BookService service,String title) {
    list.addAll(service.findByTitle(title));
    setSort("author", SortOrder.ASCENDING);
}

@Override
public Iterator<BookDto> iterator(long first, long count) {
    List<BookDto> newList = new ArrayList<BookDto>(list);
    Collections.sort(newList, comparator);
    return newList.subList(safeLongToInt(first), safeLongToInt(first + count)).iterator();
}

@Override
public long size() {
    return list.size();
}

@Override
public IModel<BookDto> model(final BookDto object) {
    return Model.of(object);
}

@Override
public void setSort(String property, SortOrder order) {
    super.setSort(property, order);
}

public int safeLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
                (l + " cannot be cast to int without changing its value.");
    }
    return (int) l;
}

class SortableBookDtoDataProviderComparator implements Comparator<BookDto>, Serializable {
    public int compare(final BookDto b1, final BookDto b2) {
        PropertyModel<Comparable> model1 = new PropertyModel<Comparable>(
                b1, getSort().getProperty());
        PropertyModel<Comparable> model2 = new PropertyModel<Comparable>(
                b2, getSort().getProperty());

        int result = model1.getObject().compareTo(model2.getObject());

        if (!getSort().isAscending()) {
            result = -result;
        }

        return result;
    }

}

}

答案 2 :(得分:0)

这是WicketApplication文件

public class WicketApplication extends WebApplication {

public WicketApplication(){

}

@Override
public Class<BooksTablePage> getHomePage() {
    return BooksTablePage.class; 
}

@Override
public void init()
{
    super.init();
    getComponentInstantiationListeners().add(new SpringComponentInjector(this));

}

}

答案 3 :(得分:0)

我现在能够运行您的代码,数据表实际上会更新,它只是将所有新搜索结果附加到旧代码。原因是数据提供程序中的列表是静态的:

private static List<BookDto> list = new ArrayList<BookDto>();

因此,即使您在每次搜索后丢弃数据提供者并创建一个新数据提供者:

sortableBookDtoDataProvider = new SortableBookDtoDataProvider(service,value);
table = new DataTable("datatable", columns, sortableBookDtoDataProvider, 3);
table.addTopToolbar(new HeadersToolbar(table, (ISortStateLocator) table.getDataProvider()));
this.replace(table);

...这个新数据表的新数据提供者仍在使用相同的书籍列表。只需删除static list成员的SortableBookDtoDataProvider即可,更新应该有效。