4 Replies Latest reply on May 2, 2011 2:30 PM by lvdberg

    First and last page don't reRender for EntityQuery

    mmulligan03

      I have a class that extends the EntityQuery and performs a search. This all works fine until I start the page navigation. The previous and next pages work fine until it gets to the first and last page or firstResult is 0 and firstLastResult.  It makes the ajax call, I can see it hitting the database but nothing is returned...


      Here is my class.



      @Name("linkRecList")
      public class LinkRecList extends EntityQuery<Map<String, Object>> { 
      
           private static final String EJBQL = "select distinct new Map(lr.lrLink as lrLink) "
                                                        + "from LinkRec as lr " +
                                                             "join lr.linkItemRecs as lir " +
                                                             "join lir.itemm as itm ";
                                                        
           private static final String[] RESTRICTIONS = {
                     "itm.cmdept = #{linkRecList.dept}",
                     "lower(itm.mfDesc) like lower(concat('%',concat(#{linkRecList.itemm.mfDesc},'%'))) ",
                     };
           
           private Itemm itemm = new Itemm();
           private Byte dept;
      
           public LinkRecList() {
                setEjbql(EJBQL);
                setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                setMaxResults(25);
                setOrder("lr.lrLink");
           }
      
           public Itemm getItemm() {
                return itemm;
           }
           
           public void setItemm(Itemm itemm) {
                this.itemm = itemm;
           }
           
           /**
            * @param dept the dept to set
            */
           public void setDept(Byte dept) {
                this.dept = dept;
           }
      
           /**
            * @return the dept
            */
           public Byte getDept() {
                return dept;
           }
      
           public Map<String, Object> getLinkedItems(LinkRec link){
                return getLinkedItems(link.getLrLink());
           }
           
           public Map<String, Object> getLinkedItems(Integer lrLink){
                
                Map<String, Object> currList = new HashMap<String, Object>();
                List<Map<String, Object>> childEls = new ArrayList<Map<String, Object>>();
                
                if(lrLink == null){
                     return currList;
                }
                
                System.out.println("lrLink: " + lrLink);
                System.out.println("First Row: " + this.getFirstResult());
                
                Query q = getEntityManager().createQuery(
                          "select new Map(" + "lr.liItem as liItem, "
                                    + "lr.linkRec.lrLink as linkRec, "
                                    + "itm.mfDesc as mfDesc, "
                                    + "itm.itemcomtotals.id.cmdept as comDept, "
                                    + "itm.mfDeln as mfDeln, "
                                    + "itm.mfPack as mfPack, "
                                    + "itm.mfWeight as mfWeight, "
                                    + "itm.mfStaff as mfStaff " + ") "
                                    + "from LinkItemRec lr join lr.itemm as itm "
                                    + "where lr.linkRec.lrLink = :lrLink "
                                    + " ORDER BY lr.liItem ");
                q.setParameter("lrLink", lrLink);
                
                StringBuilder sb = new StringBuilder();
                boolean first = true;
                
                List<Map<String, Object>> results = (List<Map<String, Object>>)q.getResultList();
                
                if(results.size() <= 0){
                     return currList;
                }
                
                for(Map<String, Object> rec : results){
                     
                     if(first){
                          currList.put("mfItemDescLink", rec.get("mfDesc"));
                          
                          first = false;
                     }
                     
                     Map<String, Object> currItem = new HashMap<String, Object>();
                     currItem.put("liItem", rec.get("liItem"));
                     currItem.put("mfDesc", rec.get("mfDesc"));
                     currItem.put("pkWt", ItemmUtils.packOrWeight((Integer)rec.get("mfStaff"), 
                                                                                 (Short)rec.get("mfPack"), 
                                                                                 (Double)rec.get("mfWeight")));
                     try{
                          currItem.put("mfDeln", "" + ItemmUtils.convertDelnString((Integer)rec.get("mfDeln")));
                     }catch (NumberFormatException nfe){ }
                     
                     //Build a comma delimited string of item codes 
                     if(sb.length() > 0)
                          sb.append(",");
                     sb.append( rec.get("liItem") );
                     
                     // build the list of items.
                     childEls.add(currItem);
                }
                
                currList.put("childEls", childEls);
                currList.put("itemLinkString", sb.toString());//comma delimited string of items
                
                return currList;
           }
      
      }



      Here is my view:



      <a4j:form id="linkRecSearch" styleClass="edit" reRender="link_items_panel,page_navigation"
                                            status="right_panel_status"
                                              eventsQueue="aggSearchQueue"  >
               <table><tr><td>
                     <table><tr>
                          <td>Links</td>
                          <td><h:inputText id="mfDesc" value="#{linkRecList.itemm.mfDesc}" 
                                             onkeypress="if( event.keyCode == 13 ) { $('linkRecSearch:links_search').click(); return false; }" />
                          </td>
                     </tr></table>
                </td>
                <td>
                     <table><tr>
                          <td>Dept</td>
                          <td>
                               <h:selectOneMenu id="cmdept" value="#{linkRecList.dept}" title="select department"  >
                                    <f:selectItems value="#{identity.departmentList}" />
                               </h:selectOneMenu>
                          </td>
                     </tr></table>
                </td>
                <td>
                   <div class="actionButtons">
                       <a4j:commandButton id="links_search" value="Search" 
                                      reRender="linkRecSearch"
                                      status="right_panel_status"
                                         eventsQueue="aggSearchQueue" >
                            <a4j:actionparam name="firstResult" value="0" />
                       </a4j:commandButton>
                       <f:verbatim>&amp;nbsp;</f:verbatim>
                       <a4j:commandButton id="reset" value="Reset" includePageParams="false" 
                                      reRender="linkRecSearch" 
                                      onclick="$('linkRecSearch:mfDesc').value='';"
                                      status="right_panel_status"
                                         eventsQueue="aggSearchQueue" >
                            <a4j:actionparam name="firstResult" value="0" />
                       </a4j:commandButton>
                   </div>
                  </td>
                </tr></table>
                
               <div style="overflow-y: auto; overflow-x: hidden; height: 375px; border-top:1px solid #C0C0C0;border-bottom:1px solid #C0C0C0;">
                     <rich:panel id="link_items_panel">
                          <rich:dataGrid var="_mainItem"
                               value="#{linkRecList.resultList}"
                               rendered="#{not empty linkRecList.resultList}"
                               rowClasses="row1,row2">
                               
                          <rich:dataGrid value="#{linkRecList.getLinkedItems(_mainItem.get('lrLink'))}"  var="_link" >
                               <div>
                                    <!--  buildSingleItemList -->
                                     <h:outputLink
                                         onclick="parent.runItems('#{_link.itemLinkString}');"
                                         value="javascript:void();">
                                         <h:outputText value="#{_link.mfItemDescLink}" />
                                    </h:outputLink>
                               </div>
                               <h:dataTable value="#{_link.childEls}" var="_itm" 
                                    style="width:90%">
                                         <h:column>
                                              <f:facet name="header">
                                                              CODE
                                                         </f:facet>
                                              <h:outputText value="#{_itm.liItem}"></h:outputText>
                                         </h:column>
                                         <h:column>
                                              <f:facet name="header">
                                                              ITEM DESC
                                                         </f:facet>
                                              <h:outputText value="#{_itm.mfDesc}"></h:outputText>
                                         </h:column>
                                         <h:column>
                                              <f:facet name="header">
                                                              STATUS
                                                         </f:facet>
                                              <h:outputText value="#{_itm.mfDeln}"></h:outputText>
                                         </h:column>
                                         <h:column>
                                              <f:facet name="header">
                                                              PK/WT
                                                         </f:facet>
                                              <h:outputText value="#{_itm.pkWt}"></h:outputText>
                                         </h:column>
                                    </h:dataTable>
                               </rich:dataGrid>
                          </rich:dataGrid>
                     </rich:panel>
                </div>
                <div class="clr" ></div>
                <div class="tableControl">
                
                   <div class="left">
                        <a4j:commandLink rendered="#{linkRecList.previousExists}" 
                                              value="#{messages.left}#{messages.left} First Page"
                                              reRender="linkRecSearch"
                                              id="firstPage" 
                                              status="right_panel_status"
                                              eventsQueue="aggSearchQueue"
                                              >
                               <a4j:actionparam name="firstResult" value="0" />
                          </a4j:commandLink>
                
                          <a4j:commandLink rendered="#{linkRecList.previousExists}" 
                                              value="#{messages.left} Previous Page"
                                              reRender="linkRecSearch"
                                              id="previousPage" 
                                              status="right_panel_status"
                                              eventsQueue="aggSearchQueue" >
                               <a4j:actionparam name="firstResult" value="#{linkRecList.previousFirstResult}" />
                          </a4j:commandLink>
                     </div>
                     <div class="right" >
                          <a4j:commandLink rendered="#{linkRecList.nextExists}" 
                                              reRender="linkRecSearch"
                                              value="Next Page #{messages.right}"
                                              id="nextPage"  
                                              status="right_panel_status"
                                              eventsQueue="aggSearchQueue">
                               <a4j:actionparam name="firstResult" value="#{linkRecList.nextFirstResult}" />
                          </a4j:commandLink>
                          
                          <a4j:commandLink rendered="#{linkRecList.nextExists}" 
                                              value="Last Page #{messages.right}#{messages.right}"
                                              reRender="linkRecSearch"
                                              id="lastPage" 
                                              status="right_panel_status"
                                              eventsQueue="aggSearchQueue" >
                               <a4j:actionparam name="firstResult" value="#{linkRecList.lastFirstResult}" />
                          </a4j:commandLink>
                     </div>
                    <div class="clr" ></div>
                </div>
           </a4j:form>




        • 1. Re: First and last page don't reRender for EntityQuery
          mmulligan03
          If I remove the rendered="#{linkRecList.previousExists}" from the command links they all work, can anyone tell me why?

          • 2. Re: First and last page don't reRender for EntityQuery
            khosro_question
            • 3. Re: First and last page don't reRender for EntityQuery
              mmulligan03

              That helped a little but still didn't get it to work. 


              What I ended up doing was rapping the links in a div and depending on the results hide or show the div




              <div class="left" style="#{((linkRecList.previousExists) ? '' : 'display:none;')}">
                   <a4j:commandLink rendered="#{linkRecList.pageCount > 1}" 
                                  value="#{messages.left}#{messages.left} First Page"
                                  reRender="linkRecSearch"
                                  id="firstLinksPage"
                                  status="right_panel_status"
                                  eventsQueue="aggSearchQueue" >
                        <a4j:actionparam name="firstResult" value="0" />
                   </a4j:commandLink>
                   
                   <a4j:commandLink rendered="#{linkRecList.pageCount > 1}"
                                       value="#{messages.left} Previous Page"
                                       reRender="linkRecSearch"
                                       id="previousPage" 
                                       status="right_panel_status"
                                       ajaxSingle="true"
                                       eventsQueue="aggSearchQueue" >
                        <a4j:actionparam name="firstResult" value="#{linkRecList.previousFirstResult}" />
                   </a4j:commandLink>
              </div>
              <div class="right" style="#{((linkRecList.nextExists) ? '' : 'display:none;')}" >
                   <a4j:commandLink rendered="#{linkRecList.pageCount > 1}"
                                       reRender="linkRecSearch"
                                       value="Next Page #{messages.right}"
                                       id="nextPage"  
                                       status="right_panel_status"
                                       eventsQueue="aggSearchQueue" 
                                       >
                        <a4j:actionparam name="firstResult" value="#{((linkRecList.nextFirstResult > linkRecList.resultCount) ? linkRecList.firstResult : linkRecList.nextFirstResult)}" />
                   </a4j:commandLink>
                   
                   <a4j:commandLink rendered="#{linkRecList.pageCount > 1}"
                                       value="Last Page #{messages.right}#{messages.right}"
                                       reRender="linkRecSearch"
                                       id="lastPage" 
                                       status="right_panel_status"
                                       eventsQueue="aggSearchQueue" >
                        <a4j:actionparam name="firstResult" value="#{linkRecList.lastFirstResult}" />
                   </a4j:commandLink>
              </div>



              • 4. Re: First and last page don't reRender for EntityQuery
                lvdberg

                Hi,


                A problem when you use the rendered is with a false-condition the elements are NOT rendered at all. So if you try to reRender, you're referencing a not-exiting itme in the objecttree. As imple trick is to use the s:span or s:div elements around the stuff that you want to reRender. The condition goes in as an element something like




                <a4j:outputPanel ...>
                <s:div rendered="youCondition" id="SOMEiD">
                ...
                ...
                </s:div>
                </a4j:outputPanel>
                



                whatever the condition is, there is always an element in the tree with our without content, preventing something like the previous.


                Leo