Hi,

Getting Display tag to remember pagination and sorting is quite tricky.

DisplayTag maintains 4 parameters of the form: d-NNNNNNN-p, d-NNNNNNN-o,
d-NNNNNNN-n, d-NNNNNNN-s. You need to get your action bean to 'remember'
them.

Below are some codes sample, hope it solves your problem.

Regards,
Yee

--------- Action Bean ----------------------------------------------
        @DefaultHandler
        public Resolution view()
        {
                // retrieves the search criteria from HttpSession
                search = getContext().getCurrent(KEY, search);

                DisplayTagHelper dth = new DisplayTagHelper(TABLE_ID,
getContext().getRequest());

                if (dth.isParametersExist()) {
                        DisplayTagHelper.Decode(getContext().getRequest(), 
TABLE_ID, search,
WebConfig.getPageSize());

//                      logger.debug("d-1329491-p: {}",
getContext().getRequest().getParameter("d-1329491-p"));
//                      logger.debug("d-1329491-o: {}",
getContext().getRequest().getParameter("d-1329491-o"));
//                      logger.debug("d-1329491-n: {}",
getContext().getRequest().getParameter("d-1329491-n"));
//                      logger.debug("d-1329491-s: {}",
getContext().getRequest().getParameter("d-1329491-s"));
//                      logger.debug("----------");
                                
                } else {
                        // The DisplayTag parameters are not present, which 
means that the user
navigates to this page
                        // from 'outside' (menu, back button etc). 
                        // Attempt to restore the last search position from the 
Search object
stored in httpSession
                        // and set DisplayTag to remember the page number and 
sort column.
                        ForwardResolution resolution = new
ForwardResolution("/searchAnnualReturn.action");
                        resolution.addParameter(dth.getPageKey(), 
search.getPageNumber());
                        resolution.addParameter(dth.getExtraKey(), "1");
                        if (search.getSortColumn() != null) {
                                resolution.addParameter(dth.getSortKey(), 
search.getSortColumn());
                                resolution.addParameter(dth.getOrderKey(), 
search.isAsc()? "1" : "2");
                        }
                        return resolution;
                }
                        
                _search();
                
                return new ForwardResolution(VIEW);
        }

-------- Helper Class
----------------------------------------------------------------------


/**
 * DisplayTag helper class for external sorting and paging.
 * 
 * @author yee
 */
public class DisplayTagHelper
{
        private ParamEncoder encoder;

        private HttpServletRequest request;

    
    /**
     * Construction of this class requires the {...@code TableId} and {...@code
HttpServletRequest} object 
     * @param tableId the DisplayTag table id.
     * @param req the HttpServletRequest
     */
    public DisplayTagHelper(String tableId, HttpServletRequest req) {
        this.encoder = new ParamEncoder(tableId);
        this.request = req;
    }
    
    /**
     * Gets the DisplayTag parameter key for sort column.
     * The key is of the form "d-1329491-s".
     * @return the DisplayTag parameter key for sort column
     */
    public String getSortKey()
    {
        return encoder.encodeParameterName(PARAMETER_SORT);
    }
    
    /**
     * Gets the DisplayTag parameter key for sort direction.
     * The key is of the form "d-1329491-o".
     * @return the DisplayTag parameter key for sort direction.
     */
    public String getOrderKey()
    {
        return encoder.encodeParameterName(PARAMETER_ORDER);
    }
    
    /**
     * Gets the DisplayTag parameter key for page number.
     * The key is of the form "d-1329491-p".
     * @return the parameter key for page number.
     */
    public String getPageKey()
    {
        return encoder.encodeParameterName(PARAMETER_PAGE);
    }
    
    /**
     * A DisplayTag extra parameter key of the form "d-1329491-n".
     * This parameter is always of value "1". It is required for the up/down
arrow to show in sort column.
     */
        public String getExtraKey()
        {
                return encoder.encodeParameterName("n");
        }

        /**
     * Returns true if the DisplayTag parameters are present. The DisplayTag
parameters
     * are added in links generated by DisplayTag for sort columns and
pagination (next/prev/first/last/1,2,3...). 
     * @return true 
     */
    public boolean isParametersExist()
    {
        return request.getParameter(getPageKey()) != null;
    }
    

    /**
     * Static helper method for decoding the DisplayTag parameters into a
{...@code BaseSearch} object.
     * 
     * @param request the {...@code HttpServletRequest} object
     * @param tableId the DisplayTag table Id
     * @param search the {...@code BaseSearch} object for receiving the decoded
values. The decoded values are placed in the {...@code startPosition,
pageNumber, sortColumn and asc} fields 
     * @param pageSize the pagesize, for calculating start position from the
page number.
     * 
     * @see com.ssm.csi.biz.model.BaseSearch
     */
    public static void Decode(HttpServletRequest req, String tableId,
BaseSearch search, int pageSize)
    {
                ParamEncoder encoder = new ParamEncoder(tableId);
                if 
(req.getParameter(encoder.encodeParameterName(PARAMETER_PAGE)) != null)
{
                                
                
search.setSortColumn(req.getParameter(encoder.encodeParameterName(PARAMETER_SORT)));
                
search.setAsc("1".equals(req.getParameter(encoder.encodeParameterName(PARAMETER_ORDER))));
        
                        String pageNumber =
req.getParameter(encoder.encodeParameterName(PARAMETER_PAGE));
        
                        if (pageNumber == null) {
                                search.setPageNumber(1);
                                search.setStartPosition(0);
                        } else {
                                
search.setPageNumber(Integer.valueOf(pageNumber));
                                search.setStartPosition((search.getPageNumber() 
- 1) * pageSize);
                        }
                        
                        search.setPageSize(pageSize);
                }
    }
}

----------------------------------------------



akahn wrote:
> 
> I'm building a Stripes app.  I have a "item list" page that displays a
> list of items in a DisplayTag table.  I've added the ability to type into
> a text field and filter the contents of the table based on the item name.
> 
> What I'm finding however, is that pagination and sorting don't seem to
> work once I've started to filter.  This seems to be an issue others (on
> this forum and others) have had w/ DisplayTag and Ajax, and I've tried a
> couple of options over the last week, the most recent being AjaxAnywhere
> (http://ajaxanywhere.sourceforge.net/) -- It seems though, that nothing I
> do works and I need some guidance.
> 
> I have an item_list.jsp containing the following:
> 
>       <%-- The filter field --%>
>         <s:url var="url"
> beanclass="org.stripesbook.quickstart.action.ItemListActionBean"/>
>         Item Filter: <input type="text" onkeyup="filterItems(this,
> '${url}');"/><br/>
> 
>             <%-- This tag is the one called by the JS filter to redraw the
> table --%>
>         <div id="item_table">
>             <%...@include file="/WEB-INF/jsp/parts/item_table.jsp" %>  
>         </div>
> 
> Any suggestions on what I should be doing so I can maintain the same
> functionality but get the DisplayTags sorting and pagination to work
> properly?
> 
> Thanks in advance.
> 

-- 
View this message in context: 
http://www.nabble.com/DisplayTag-and-AJAX-filtering-in-Stripes-app-tp23476058p23646482.html
Sent from the stripes-users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com 
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to