1 Reply Latest reply on Sep 6, 2012 10:29 AM by Hong Ju

    <rich:column's attribute comparator not working

    Hong Ju Newbie

      Hi,

       

      I have followed this example to do my sorting on a column: http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=dataTable&sample=tableSorting&skin=blueSky

      but the column does not sort, I have seen my comparator being called.

       

      My Bean:

       

      public Ordering getSubmittedBySort() {

              return SubmittedBySort;

          }

       

      public void setSubmittedBySort(Ordering sort) {

              this.SubmittedBySort = sort;

          }

       

      public Comparator<ApprovalsTableBean> getDateSubmittedComparator() {

              return new Comparator<ApprovalsTableBean>() {

                  public int compare(ApprovalsTableBean a1, ApprovalsTableBean a2) {

                      return a1.getDateSubmitted().compareTo(a2.getDateSubmitted());

                   }

              };

          }

       

      My xhtml:

      <rich:column styleClass="#{approvals.rowcolor}" width="10%" sortBy="#{approvals.dateSubmitted}" sortOrder="#{manageOutstandingApprovals.dateSubmittedSort}" comparator="#{manageOutstandingApprovals.dateSubmittedComparator}" >

                     <f:facet name="header">

                      <h:outputText value="#{messages['outstandingApprovals.dateSubmitted']}"/>

                    </f:facet>

                    <h:outputText value="#{approvals.dateSubmitted}" id="col3"/>

      /rich:column>

       

      Does anyone have a working example? Thank you in advance. Lily

        • 1. Re: <rich:column's attribute comparator not working
          Hong Ju Newbie

          I solved myself, here is my code:

           

          In my xhtml:

           

          //always load the list

          <head>#{manageOutstandingApprovals.loadOutstandingApprovals}</head>

           

          <rich:column styleClass="#{approvals.rowcolor}" width="5%" comparator="#{manageOutstandingApprovals.comparator}" sortBy="#{approvals.dateOutstanding}" sortOrder="#{manageOutstandingApprovals.numOutstandingSort}">

                        <f:facet name="header">           

           

                          <h:outputText styleClass="wrapcn" value="#{messages['outstandingApprovals.daysOutstanding']}"/>

           

                        </f:facet>

                        <h:outputText value="#{approvals.dateOutstanding}"  id="col9"/>

                      </rich:column>

           

          in my bean:

           

          public String getLoadOutstandingApprovals() {

                 

                  if (listOfApprovals != null && organizationID == selectedOrgID)

                      return "";

                 

                  List<OutstandingApprovalsSearchResults> searchResults = new ArrayList<OutstandingApprovalsSearchResults>();

                  listOfApprovals = new ArrayList<ApprovalsTableBean>();

          ......

          }

           

          public Comparator<ApprovalsTableBean> getComparator() {

                 

                  Collections.sort(listOfApprovals, new MyComparable());

                  return null;

              }

           

          class MyComparable implements Comparator<ApprovalsTableBean>{

                  

                  @Override

                  public int compare(ApprovalsTableBean a1, ApprovalsTableBean a2) {

                     

                       int tz1Int = Integer.valueOf(a1.getDateOutstanding().split(DAY_HOUR_SEPARATOR)[0]);

                       int tz2Int = Integer.valueOf(a2.getDateOutstanding().split(DAY_HOUR_SEPARATOR)[0]);

                

                      if (NumOutstandingSort.equals(Ordering.ASCENDING)) {

                     

                          if (tz1Int == tz2Int) {

                              return 0;

                          }

                          if (tz1Int > tz2Int) {

                              return -1;

                          } else {

                              return 1;

                          }

                      }

                      else {

                          if (tz1Int == tz2Int) {

                              return 0;

                          }

                          if (tz2Int > tz1Int) {

                              return -1;

                          } else {

                              return 1;

                          }

                      }

                         

                  }

           

              }

           

           

          // I use Collections.sort to sort my listOf Approvals