Reputation: 525
I have a DataTable
. I have added NavigationToolBar
at the bottom of it.
When I click on a page number (for example 3) and then click on a link in a cell of the table, it goes to another page. Then I click a link on top which goes back to the first page of the datatable. But I want to go back to the same page I was when I clicked the link in the cell (page 3).
How can I write a piece of code that works like back button of the browser? (something like javascript, history.go(-1)
)
Upvotes: 2
Views: 8040
Reputation: 27880
Assuming you're returning to the Page with the DataTable with setResponsePage()
, you'll be returning to a new instance of the Page, without information about its state before going clicking the cell link.
As I see it, you have two ways to handle this situation:
You could pass to the detail page's constructor all relevant info in order to 'rebuild state' in the page with the DataTable in your setResponsePage
(in this case maybe the page index would be enough). I've used this approach before with a search results page, passing all search criteria to the detail page and passing it back again to the search page in order to perform the same search.
You could also address this by passing a reference to the page with the table to the page you go to with the link, and using a PageLink
to link back to the same Page
as suggested in this thread in the Wicket Users list, or using it in setResponsePage()
.
I'd go for the first option, because passing a Page reference to another page can be dangerous, and you can end up with the whole DataTable page serialized in the detail page, and PageLink has been deprecated. See this discussion in the Wicket Users list for details.
To elaborate a little, you could use DataTable.getCurrentPage()
in your Link.onClick()
to pass the current page number to the DetailPage, store it there in an int
member, and pass it back to the DataTable Page to use it in DataTable.setCurrentPage()
:
public DataTablePage extends Page {
private DataTable dataTable;
public void setDataTablePage(int page){
dataTable.setCurrentPage(page);
}
// .....
// DataTable cell link onclick
public void onClick(){
int pageNumber = dataTable.getCurrentPage();
DetailPage detailPage = new DetailPage(id, params, pageNumber);
setResponsePage(page);
}
}
public class DetailPage extends Page {
int pageNumber;
public DetailPage(String id, Object params, int pageNumber){
//...
this.pageNumber = pageNumber;
//...
}
private void backToTablePage(){
DataTablePage dataTablePage = new DataTablePage(id, params);
dataTablePage.setDataTablePage(pageNumber);
setResponsePage(dataTablePage);
}
}
Upvotes: 2