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))
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
PageLinkto link back to the samePageas suggested in this thread in the Wicket Users list, or using it insetResponsePage().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 yourLink.onClick()to pass the current page number to the DetailPage, store it there in anintmember, and pass it back to the DataTable Page to use it inDataTable.setCurrentPage():