Hi,
First, thanks to all the folks making Display Tag such
a neat tag lib. Originally Ben had added this to our
framework, which included Tiles and Struts, and I
think it's a great choice.
Here's a simple fix, and I'm sure it could be written
much better, but here it is anyway.
See the attached file, but for a synopsis, when paging
backward (prev), the current page is set such that you
can only go one page at a time. This fix switches the
current page depending on which direction you are
going.
Here's one example of display tag in use:
http://www.amfam.com/affsveh/servlet/search?searchInput.listAllVehicles=true
-Mike
_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush
------------------------------------------------------------------------------
Issue: When selecting the 'Next/Prev' links, the starting page is set to the
current page. This is fine when selecting 'Next', however when selecting
'Prev' we'd like the ending page to be set to the current page so it would be
easier to navigate to the previous pages.
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Pagination.java
------------------------------------------------------------------------------
>>replace class variable urlFormat with:
/**
* MessageFormat for urls
*/
private MessageFormat urlFormatFirst;
private MessageFormat urlFormatPrev;
private MessageFormat urlFormatNext;
private MessageFormat urlFormatLast;
>>replace constructor with:
/**
* Constructor for Pagination
* @param urlFormatString String
*/
public Pagination(String urlFirst, String urlPrev, String urlNext, String
urlLast)
{
this.urlFormatFirst = new MessageFormat(urlFirst);
this.urlFormatPrev = new MessageFormat(urlPrev);
this.urlFormatNext = new MessageFormat(urlNext);
this.urlFormatLast = new MessageFormat(urlLast);
}
>>replace urlFormat with urlFormatFirst in the line below:
String urlString = this.urlFormat.format(new Object[] { pageNumber });
>>replace pageObjects declaration with:
Object[] pageObjects =
{
numberedPageString,
this.urlFormatFirst.format(new Object[] { getFirst()}),
this.urlFormatPrev.format(new Object[] {
getPrevious()}),
this.urlFormatNext.format(new Object[] { getNext()}),
this.urlFormatLast.format(new Object[] { getLast()}),
};
>>in public String toString()
>>replace urlFormat with the lines below:
.append("urlFormatFirst", this.urlFormatFirst)
.append("urlFormatPrev", this.urlFormatPrev)
.append("urlFormatNext", this.urlFormatNext)
.append("urlFormatLast", this.urlFormatLast)
------------------------------------------------------------------------------
SmartListHelper.java
------------------------------------------------------------------------------
>>add the class variable below:
/**
* true if [Prev] selected
*/
private boolean reverse=false;
>>change the getPageNavigationbar method signature to include the extra urls:
public String getPageNavigationBar(String urlFirstStr,String urlPrevStr, String
urlNextStr, String urlLastStr)
>>add the urls to the Pagination constructor call:
Pagination pagination = new Pagination(urlFirstStr, urlPrevStr, urlNextStr,
urlLastStr);
>>in the if (currentIndex<maxPages) if-else, add an else-if below to the original
>>'else'
else if (this.reverse){
endPage=currentIndex;
if (currentIndex>maxPages){
startPage=endPage-maxPages+1;
}else{
startPage=1;
}
} else {
startPage = currentIndex;
//etc
>>add a getter/setter for reverse:
/**
* @return
*/
public boolean isReverse() {
return reverse;
}
/**
* @param b
*/
public void setReverse(boolean b) {
reverse = b;
}
------------------------------------------------------------------------------
TableTag.java
------------------------------------------------------------------------------
>>add a private variable:
/**
*
* "true" if previous selected else false
*
*/
private String isPrevious;
>>in initParameters(), retrieve the value from the nvp in the request:
this.isPrevious=
requestHelper.getParameter(encodeParameter(TableTagParameters.PARAMETER_PREVIOUSPAGE));
>>in the getViewableData method, add the line below after setting the current page:
this.listHelper.setReverse(isPrevious==null?false:isPrevious.equals("true"));
>>in method getSearchResultAndNavigation, replace the 'if (this.pagesize!=0 &&..'
>>statement with:
if (this.pagesize != 0 && this.listHelper != null)
{
Href genericNavigationHref=new Href(this.baseHref);
// add page parameter with message format
genericNavigationHref.addParameter(encodeParameter(TableTagParameters.PARAMETER_PAGE),
"{0,number,#}");
genericNavigationHref.addParameter(encodeParameter(TableTagParameters.PARAMETER_PREVIOUSPAGE),"false");
// create a new href for First/Prev/Next/Last
Href navPrevHref = new Href(genericNavigationHref);
String genericNavStr = genericNavigationHref.toString();
navPrevHref.addParameter(encodeParameter(TableTagParameters.PARAMETER_PREVIOUSPAGE),"true");
String
navBar=this.listHelper.getPageNavigationBar(genericNavStr, navPrevHref.toString(),
genericNavStr, genericNavStr);
StringBuffer buffer =new StringBuffer();
buffer.append(this.listHelper.getSearchResultsSummary());
buffer.append(navBar);
return buffer.toString();
}
------------------------------------------------------------------------------
TableTagParameters.java
------------------------------------------------------------------------------
>>add the variable:
/**
* name of the parameter specifying the previous page number.
*/
public static final String PARAMETER_PREVIOUSPAGE = "pp";