9 Replies Latest reply on Oct 24, 2008 10:03 AM by james.holder3

    DataTable, how to fire action on sort

      All,

      We're using rich:datatable, with built in sorting and pagination.

      As it stands by default, when you go to (for instance) page 6 of 10, and then sort, the entire data list is sorted. This is fine (perfect in fact), but the paginator keeps the table displaying page 6 (the one you were on).

      I'd like to reset the paginator back to page 1 whenever the table is sorted. (Basically starting with a fresh list)

      I dont know if there is a built in event I can bind to, or if I need something else. Could someone give me a push in the right direction? If nothing else, just a way to call a method whenever the table is sorted would be good enough.

        • 1. Re: DataTable, how to fire action on sort

          Hi,
          as i couldn't find any event either i made a property binding to rich:column. Each time you sort a column its getters and setters are called.
          for the pagination thing you can use the page-attribute which you reset to 1 in ur bean after a specific action. however the page will probably only rerender after an action-method call...

          • 2. Re: DataTable, how to fire action on sort

            I tired something similar to:

            public class GuiStateBean implements Serializable {
            
            private HtmlColumn sortColumn = new HtmlColumn();
            
             public HtmlColumn getSortColumn() {
             logger.debug("Getting the sortColumn");
             return uicSearchUicColumn;
             }
            
             public void setSortColumn(HtmlColumn uicSearchUicColumn) {
             logger.debug("Setting the sortColumn");
             this.uicSearchUicColumn = uicSearchUicColumn;
             }
            }
            


            and in my page
            <rich:datatable>
            <rich:column id="uic" sortBy="#{my.string}"
             binding="#{guiState.sortColumn}"
            >
            <f:facet name="header">
             <h:outputText id="Header" value="sort Me">
            </f:facet>
            
            <h:outputText value="#{my.string}"/>
            
            </rich:column>
            </rich:datatable>
            
            


            Thinking from the previous posters description, that these getters might be called whenever the table is sorted. However that does not appear to be the case.....

            Does anyone know of any to call a server side method from the gui to shortcircuit this issue with the pagination and sorting?

            I think that resetting the lists pagination onSort should be the default behavior, or at the very least, very simple for the user to implement. Something like actionOnSort="#{my.stringMethod}"


            • 3. Re: DataTable, how to fire action on sort

              I tired something similar to:

              public class GuiStateBean implements Serializable {
              
              private HtmlColumn sortColumn = new HtmlColumn();
              
               public HtmlColumn getSortColumn() {
               logger.debug("Getting the sortColumn");
               return sortColumn;
               }
              
               public void setSortColumn(HtmlColumn sortColumn) {
               logger.debug("Setting the sortColumn");
               this.sortColumn = sortColumn;
               }
              }
              


              and in my page
              <rich:datatable>
              <rich:column id="uic" sortBy="#{my.string}"
               binding="#{guiState.sortColumn}"
              >
              <f:facet name="header">
               <h:outputText id="Header" value="sort Me">
              </f:facet>
              
              <h:outputText value="#{my.string}"/>
              
              </rich:column>
              </rich:datatable>
              
              


              Thinking from the previous posters description, that these getters might be called whenever the table is sorted. However that does not appear to be the case.....

              Does anyone know of any to call a server side method from the gui to shortcircuit this issue with the pagination and sorting?

              I think that resetting the lists pagination onSort should be the default behavior, or at the very least, very simple for the user to implement. Something like actionOnSort="#{my.stringMethod}"


              • 4. Re: DataTable, how to fire action on sort

                Anyone? I'm still trying to figure out some workaround for this. Basically, if the table is sorted, i want to reset to the pagination to the first page. Resetting the pagination is hte easy part, I need to figure out how to attach somethign that will call that event when the table is sorted.

                Any help at all would be appreciated.

                • 5. Re: DataTable, how to fire action on sort
                  konstantin.mishin
                  • 6. Re: DataTable, how to fire action on sort

                    Thanks Konstantin.

                    Can you think of any way to pseudo implement this functionality?

                    • 7. Re: DataTable, how to fire action on sort
                      nbelaevski

                      What about model bean setters for sort mode/priority? You can try to use them.

                      • 8. Re: DataTable, how to fire action on sort

                        So far, the closest thing I've been able to come up with is to have my table in an a4j:region, and to utilize an ajaxListener to catch the ajax sorts. The caveats there are that in order for it to work, no other AJAX calls, other than the sort, can be fired inside the table.

                        I'm thinking there must be some way to differentiate between a sort call, and a form update call. I'm just not sure how that would be achieved.

                        Anyone have a better idea?

                        • 9. Re: DataTable, how to fire action on sort

                          All,

                          I've been trying to figure out some way to get this behavior to work in some fashion, and this has been the best I've been able to come up with. The problem is, there are intermittent issues where a columns data will disappear, or the data will be there but not the header text (but the arrows will be), or the entire table itself will not be there and I'll get a message saying that no element with an id of examples can be found on the page... Is there anything in the following code that I should be aware of that would cause these problems?

                          (I only put in the "relevant" code, accessors and mutators are as you would expect. The driving is a standard ArrayList, that is generated on the first access, and just returned unchanged otherwise.)



                          GuiStateBean

                          private HtmlDataTable exampleTable;
                           private HtmlDatascroller exampleTableScroller;
                           private Integer exampleTablePage;
                          
                           public void exampleTableReset(){
                           logger.info("Resetting: "+ getClass().getName()+".exampleTableReset()");
                           logger.info("The page was: "+this.exampleTableScroller.getPage()+". Resetting to page 1.");
                           this.exampleTableScroller.setPage(1);
                           }




                          table.xhtml


                          <h:form>
                          
                          <a4j:jsFunction name="resetScroller" action="#{guiState.exampleTableReset}" eventsQueue="exampleQueue"/>
                          <a4j:status for="exampleTable" onstart="showToaster('Updating table.')" onstop="hideToaster();"/>
                          
                          <a4j:region id="exampleTable" >
                           <a4j:outputPanel ajaxRendered="true">
                           <rich:dataTable
                           var="item"
                           value="#{example.examplesAsList}"
                           id="examples"
                           rows="10"
                           binding="#{guiState.exampleTable}"
                           >
                           <rich:column id="fname" sortBy="#{item.firstName}">
                           <f:facet name="header">
                           <a4j:outputPanel onclick="resetScroller();">
                           <h:outputText id="fnameHeader" value="First Name" />
                           </a4j:outputPanel>
                           </f:facet>
                           <h:outputText value="#{item.firstName}" />
                           </rich:column>
                          
                           <rich:column id="lname" sortBy="#{item.lastName}">
                           <f:facet name="header">
                           <a4j:outputPanel onclick="resetScroller();">
                           <h:outputText id="lnameHeader" value="Last Name"/>
                           </a4j:outputPanel>
                           </f:facet>
                           <h:outputText value="#{item.lastName}"/>
                           </rich:column>
                          
                           <rich:column id="age" sortBy="#{item.age}">
                           <f:facet name="header">
                           <a4j:outputPanel onclick="resetScroller();">
                           <h:outputText id="ageHeader" value="Age"/>
                           </a4j:outputPanel>
                           </f:facet>
                           <h:outputText value="#{item.age}"/>
                           </rich:column>
                          
                           <rich:column id="phone" sortBy="#{item.phoneNumber}">
                           <f:facet name="header">
                           <a4j:outputPanel onclick="resetScroller();">
                           <h:outputText id="phoneHeader" value="Phone Number"/>
                           </a4j:outputPanel>
                           </f:facet>
                           <h:outputText value="#{item.phoneNumber}"/>
                           </rich:column>
                           </rich:dataTable>
                           </a4j:outputPanel>
                          </a4j:region>
                          
                           <a4j:outputPanel ajaxRendered="true">
                           <rich:datascroller reRender="examples"
                           id="exampleDataScroller"
                           eventsQueue="exampleQueue"
                           binding="#{guiState.exampleTableScroller}"
                           page="#{guiState.exampleTablePage}"
                           for="examples"
                           immediate="true"
                           />
                           </a4j:outputPanel>
                          
                          
                           </h:form>