How to add sorting in Apache Wicket DataTable with content from Hibernate?

I have an existing DataTable built in Wicket 1.5 as follows:

The page of the 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 supplier

 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> 

I tried just adding a sort option to the PropertyColumn so that the column is clickable but does nothing.

 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 tried to add setSort (new SortParam ("name", true)); supplier designer

I also tried adding a full backend to the DAO to return the sorted part of the table.

I also tried to add a field to the properties file for a page similar to the first resource located below.

All my attempts lead to the same error. I think I need to skip something simple, but I checked a few examples and found nothing.

Resources that I have already tested include the following:

+4
source share
2 answers

Do not pass 'null' to the new HeadersToolbar (exampleTable, null).

+2
source

You need to define a custom data provider by extending SortableDataProvider , and then pass it to the HeadersToolbar . For instance:

 new HeadersToolbar(exampleTable, exampleSortableDataProvider) 
+4
source

All Articles