我应该如何使用 Hibernate 的内容向 Apache Wicket DataTable 添加排序?
我在 Wicket 1.5 中构建了一个现有的 DataTable,如下所示:
页面的 java 文件。
public class ExamplePage extends WebPage {
public ExamplePage(){
List<IColumn<Example>> columns = new ArrayList<IColumn<Example>>();
columns.add(new PropertyColumn<Example>(Model.of("name"), "name"));
ExampleProvider provider = new ExampleProvider();
DataTable<Example> exampleTable = new DataTable<Example>("exampleTable", columns, provider, 10);
exampleTable.addTopToolbar(new HeadersToolbar(exampleTable, null));
exampleTable.addBottomToolbar(new NavigationToolbar(exampleTable));
add(exampleTable);
}
}
提供者
public class ExampleProvider extends SortableDataProvider<Example> {
@SpringBean
ExampleDao exampleDao;
public ExampleProvider() {
Injector.get().inject(this);
}
@Override
public Iterator<Example> iterator(int first, int count) {
List<Example> examples = exampleDao.find(first, count);
Iterator<Example> exampleIterator = examples.iterator();
return exampleIterator;
}
@Override
public IModel<Example> model(Example object) {
return Model.of(object);
}
@Override
public int size() {
return exampleDao.count();
}
}
html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org"
xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<wicket:extend>
<table wicket:id="exampleTable" />
</wicket:extend>
</body>
</html>
我尝试将排序参数添加到 PropertyColumn 中,以便该列可以单击,但是 不会做任何事。
columns.add(new PropertyColumn<Example>(Model.of("name"), "name", "name"));
这产生了以下错误。
Last cause: null
WicketMessage: Exception in rendering component: [ [Component id = header]]
java.lang.NullPointerException
at org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByLink$CssModifier.onComponentTag(OrderByLink.java:190)
at org.apache.wicket.Component.renderComponentTag(Component.java:3885)
at org.apache.wicket.Component.internalRenderComponent(Component.java:2506)
at org.apache.wicket.MarkupContainer.onRender(MarkupContainer.java:1576)
at org.apache.wicket.Component.internalRender(Component.java:2345)
at org.apache.wicket.Component.render(Component.java:2273)
at org.apache.wicket.MarkupContainer.renderNext(MarkupContainer.java:1474)
...
我尝试添加 setSort(new SortParam("name", true));对于 Provider 构造函数,
我还尝试将完整的后端添加到 DAO,以便返回表的排序部分。
我还尝试向页面的属性文件添加一个字段,类似于底部的第一个资源。
我的所有尝试都会导致相同的错误。我想我一定错过了一些简单的东西,但我检查了很多例子,但什么也没发现。
我已经检查过的资源包括以下内容:
I have an existing DataTable built in Wicket 1.5 as follows:
The Page's java file.
public class ExamplePage extends WebPage {
public ExamplePage(){
List<IColumn<Example>> columns = new ArrayList<IColumn<Example>>();
columns.add(new PropertyColumn<Example>(Model.of("name"), "name"));
ExampleProvider provider = new ExampleProvider();
DataTable<Example> exampleTable = new DataTable<Example>("exampleTable", columns, provider, 10);
exampleTable.addTopToolbar(new HeadersToolbar(exampleTable, null));
exampleTable.addBottomToolbar(new NavigationToolbar(exampleTable));
add(exampleTable);
}
}
The Provider
public class ExampleProvider extends SortableDataProvider<Example> {
@SpringBean
ExampleDao exampleDao;
public ExampleProvider() {
Injector.get().inject(this);
}
@Override
public Iterator<Example> iterator(int first, int count) {
List<Example> examples = exampleDao.find(first, count);
Iterator<Example> exampleIterator = examples.iterator();
return exampleIterator;
}
@Override
public IModel<Example> model(Example object) {
return Model.of(object);
}
@Override
public int size() {
return exampleDao.count();
}
}
The html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org"
xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<wicket:extend>
<table wicket:id="exampleTable" />
</wicket:extend>
</body>
</html>
I tried just adding the sort parameter to the PropertyColumn so that the column would be clickable but
would not do anything.
columns.add(new PropertyColumn<Example>(Model.of("name"), "name", "name"));
This created the following error.
Last cause: null
WicketMessage: Exception in rendering component: [ [Component id = header]]
java.lang.NullPointerException
at org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByLink$CssModifier.onComponentTag(OrderByLink.java:190)
at org.apache.wicket.Component.renderComponentTag(Component.java:3885)
at org.apache.wicket.Component.internalRenderComponent(Component.java:2506)
at org.apache.wicket.MarkupContainer.onRender(MarkupContainer.java:1576)
at org.apache.wicket.Component.internalRender(Component.java:2345)
at org.apache.wicket.Component.render(Component.java:2273)
at org.apache.wicket.MarkupContainer.renderNext(MarkupContainer.java:1474)
...
I have tried adding setSort(new SortParam("name", true)); to the Provider constructor
I have also attempted adding the complete backend to the DAO so that a sorted portion of the table would be returned.
I have also tried adding a field to the properties file for the page similar to the first resource sited at the bottom.
All of my attempts result in the same error. I think I must be missing something simple, but I have checked quite a few examples and found nothing.
The resources I have already checked include the following:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要通过扩展
SortableDataProvider
来定义自定义数据提供程序,然后将其传递到HeadersToolbar
中。例如:You need to define a custom data provider by extending
SortableDataProvider
and then pass it inHeadersToolbar
. For example:不要在 new HeadersToolbar(exampleTable, null) 处传递“null”。
Don't pass 'null' at new HeadersToolbar(exampleTable, null).