homena / jmesa

Automatically exported from code.google.com/p/jmesa
0 stars 0 forks source link

How to set the page number of a table in the controller for jsp (tag) #305

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi Jeff,

I am new to JMesa.  I am trying to set the page number of a table when I click 
the submit button to go back to the previous page with a JMesa Table (i.e. 
showing start from page 2 of 4, instead of starting from 1).

I am using JMesa Tag instead of API.  My jsp (tag) is using sort, pagination 
and filter.  I am not sure about what I should do with this limit stuff in the 
controller.

I found an example when I read “The Limit Exposed” on your documentation.   
But I still need further explanation.
<jmesa:tableModel limit="${limit}">   Where does ${limit} come from?  Is there 
a completed simple example in class or controller somewhere that use TAG 
(limit=${limit}) I can find?

Let me show you the simple codes and give you an idea of what I am trying to 
do.  Please give me an example of how to do it.

@Controller
@RequestMapping(method = RequestMethod.GET)public String home(ModelMap model, 
SearchFilters searchFilters, HttpServletRequest request, HttpSession session) 
{ 
   String id = request.getParameter("type");
   //   Data for the table
   model.put(companyData, companyService.getAll(searchFilters));     

// Without this below limit example, I can see the data in the JMesa table 
which is working fine.
   TableModel tableModel = new TableModel(id, request, response);    
   tableModel.setItems(companyService.getAll());         
   ??????  What codes should I put here?    
   Limit limit = new Limit(id) //java.lang.NullPointerException :(    
   limit.getRowSelect().setPage(2);
   model.put(“limit”,limit);               

   return MAIN_PAGE;
}

<form:form modelAttribute="companySearch" id="companyMainForm" 
name="companyMainForm" action="company.html" method="get"> 
<input type="hidden" name="type" value="company"/> 
<jmesa:tableModel 
      items="${companyData}" 
      id="company"        
      limit="${limit}"        
      stateAttr="restore"
      var="bean"
      editable="false"  
      toolbar="org.company.jmesa.table.CustomToolbar"
      view="org.company.jmesa.table.CustomView"   
      filterMatcherMap="org.company.jmesa.table.TagFilterMatcherMap"
>           
<jmesa:htmlTable width="98.5%">                            
 <jmesa:htmlRow>                    
  <jmesa:htmlColumn property="status" width="100px" title="Status" 
    sortable="true"
    filterEditor="org.jmesa.view.html.editor.DroplistFilterEditor"/>  
  <jmesa:htmlColumn property="companyName" title="Company Name" 
    sortable="true"
    filterable="true"                       filterEditor="org.jmesa.view.html.editor.DroplistFilterEditor"/>      </jmesa:htmlRow>               
</jmesa:htmlTable>      
</jmesa:tableModel>      
</form:form>

Thanks,
Zach

Original issue reported on code.google.com by zach.ro...@gmail.com on 1 Mar 2011 at 8:58

GoogleCodeExporter commented 9 years ago
helping the developer through email...closing this issue here

Original comment by jeff.johnston.mn@gmail.com on 9 Mar 2011 at 7:24

GoogleCodeExporter commented 9 years ago
Hi Jeff, I'm really curious how this was solved as I'm running into a similar 
problem. 

I have a list of records, upon clicking a row, a GET request is performed to 
show the detail view of that record, but when I navigate away from that detail 
view I want to arrive on the same page where I left off. The 
'stateAttr="restore"' and the URL parameter (which I add when nvaigating away 
from the detail view) 'restore=true', don't seem to do the trick. 

Is't it possible to show the solution here? Thanks in advance.

Original comment by rpbran...@gmail.com on 13 Apr 2011 at 2:29

GoogleCodeExporter commented 9 years ago
Strange. What version of JMesa are you on? Are your table id's unique in the 
app?

http://code.google.com/p/jmesa/wiki/State

The issue with this post was something else...

Original comment by jeff.johnston.mn@gmail.com on 13 Apr 2011 at 8:53

GoogleCodeExporter commented 9 years ago
Do you mean that the row that you left is not highlighted, or is it that the 
State feature itself is not working?

Original comment by jeff.johnston.mn@gmail.com on 14 Apr 2011 at 5:01

GoogleCodeExporter commented 9 years ago
Hi Jeff, we're using the latest 3.0.4 release and use unqiue id's. Yes what I 
mean is that with the tutorial code, the state feature isn't working unless 
additional code is added. 

I finally got it working with some extra code that I don't see appearing in 
your tutorials. 
In the BasicPresidentController I see a single method handling the GET and POST 
requests. What I don't 
see there is that you retrieve the page number from the state and feeding it to 
the service to retrieve
the page that you actually want to view. It seems you're always retrieving the 
same set of presidents 
from the database (all of them) and let JMesa take a subset from that. In a 
case where you use paging, 
you typically deal with lot's of records that you don't want to be retrieved 
from the database. 
Anyway, this is the setup based on SpringMVC with annotations:

What happens is that not all our requests are POSTs, the scenario is this:

1. User arrives on list view via a GET request, since no state is available the 
user is shown the first page
2. User jumps to page X via a POST request and is still shown the list view
3. User clicks on a row and arrives on a detail page via a GET request
4. User performs some action/or cancel and should arrive back on page X

In the list view JSP:

<jmesa:springTableModel id="client_table" items="${clients}" var="bean" 
limit="${limit}" stateAttr="restore">
    <jmesa:htmlTable>
        <jmesa:htmlRow filterable="false" uniqueProperty="id">
            <jmesa:htmlColumn property="name" sortable="false" headerClass="header-name" titleKey="jmesa.table.client.name"/>
            <jmesa:htmlColumn property="description" sortable="false" headerClass="header-description" titleKey="jmesa.table.client.description"/>
            <jmesa:htmlColumn property="" sortable="false" filterable="false" headerClass="header-delete" styleClass="cell-delete" title=" "/>
        </jmesa:htmlRow>
    </jmesa:htmlTable>
</jmesa:springTableModel>

In the controller:

@RequestMapping(value = "/admin/listClients.htm", method = RequestMethod.GET)
public ModelAndView listClients(final HttpServletRequest request, 
@ModelAttribute(CRUD_COMMAND) final ClientCommand command) {
    ModelAndView modelAndView = new ModelAndView("clientListView");

    TableFacade tableFacade = new TableFacade("tclients", request);
    tableFacade.setStateAttr("restore");
    Limit limit = tableFacade.getLimit();

    int pageNumber = 1;
    if (limit.hasRowSelect()) {
        pageNumber = limit.getRowSelect().getPage();
    }

    Page page = getPageNavigationAwareService().retrievePage(pageNumber, 10);
    tableFacade.setItems(page.getResults());

    limit.setRowSelect(new RowSelect(page.getPageNumber(), page.getSize(), page.getTotalRecords()));

    modelAndView.addObject("items", page.getResults());
    modelAndView.addObject("limit", limit);
    return modelAndView;
}

/*
 * In the method below, note the request parameter in the signature. If we click on one of the page numbers or next/previous/first/last 
 * we get that value as a request parameter but it it not available in the tablefacade.limit.rowselect.page since the rowselect is null.
 * So, we pickup the request parameter ourselves to feed it to our back-end so that it knows which page to retrieve.
 */
@RequestMapping(value = "/admin/listClients.htm", method = RequestMethod.POST)
public ModelAndView navigateClients(final HttpServletRequest request, 
@ModelAttribute(CRUD_COMMAND) final ClientCommand command,
                                @RequestParam(value = "tclients_p_", required = false) final Integer pageId) {
    ModelAndView modelAndView = new ModelAndView("clientListView");

    TableFacade tableFacade = new TableFacade("tclients", request);
    tableFacade.setStateAttr("restore");

    // START CODE ADDED BY ME
    State state = tableFacade.getState();   // Retrieving the Limit directly from the facade returns a Limit object with a null rowSelect 
    Limit limit = state.retrieveLimit();

    int pageNumber = pageId;
    if (limit != null && limit.hasRowSelect()) {
        pageNumber = limit.getRowSelect().getPage();
    } else {
        limit = new Limit("tclients");
        state.persistLimit(limit);
    }

    Page page = getPageNavigationAwareService().retrievePage(pageNumber, 10); // Note that we always load a specific page
    limit.setRowSelect(new RowSelect(page.getPageNumber(), page.getSize(), page.getTotalRecords()));
    // END CODE ADDED BY ME        

    modelAndView.addObject("items", page.getResults());
    modelAndView.addObject("limit", limit);
    return modelAndView;
}

Original comment by rpbran...@gmail.com on 15 Apr 2011 at 9:22

GoogleCodeExporter commented 9 years ago
Because you are using the 3.0 release this could be much more simple. Did you 
see the new PageItems interface? It will abstract out all the low level work 
with the facade that you are doing. 

http://code.google.com/p/jmesa/wiki/LimitTutorialV3

When using the tag library you would use the TableModelUtils.getItems() and 
then not set the Limit on the the tag...take out the limit="${limit}". The 
utils method will pass that for you.

If you do want to go to a specific page you could set the 
Limit.getRowSelect().setPage() first in the PageItems.getItems() method. This 
would recalculate the RowSelect for you based on the new page.

The only thing I do not see in your example is where you get the max rows from. 
You need the max rows to get a proper RowSelect, which is why the PageItems 
asks for it explicitly.

You should also set the autoFilterAndSort to false on the tag as you are doing 
the filtering and sorting yourself.

Hope that helps! The new TableModel stuff was created to abstract out the 
details of working with the facade so I would always use that...unless you are 
doing something so custom that you need the lower level object.

Original comment by jeff.johnston.mn@gmail.com on 15 Apr 2011 at 3:14

GoogleCodeExporter commented 9 years ago
Ok thanks Jeff!

Original comment by rpbran...@gmail.com on 20 Apr 2011 at 11:28

GoogleCodeExporter commented 9 years ago
Hi ,
I am new to jmesa .
I have created table using jmesa tag libray .Now the requirement is to do the 
pagination. i.e is to pull only the required records from the database. Please 
help me with the examples.

Regards,
Resh

Original comment by reshmabh...@gmail.com on 2 Feb 2012 at 4:36