Search code examples
liferay-6

Sorting on multiple column header in liferay searchcontainer


i done with sorting on single column header of liferay searchcontainer in liferay 6.0.6. now i want to apply sorting on multiple fields i.e FirstName,LastName,Date either in asc or desc order. can anybody help me out..

Thanks in advance....


Solution

  • view.jsp

    <%
    PortalPreferences portalPrefs = PortletPreferencesFactoryUtil.getPortalPreferences(request);
    String orderByCol = ParamUtil.getString(request, "orderByCol");
    String orderByType = ParamUtil.getString(request, "orderByType");
    System.out.println("Col  "+ orderByCol);
    
    
    if (Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)) {
    portalPrefs.setValue("NAME_SPACE", "order-by-col", orderByCol);
    portalPrefs.setValue("NAME_SPACE", "order-by-type", orderByType);
    
    } else {
    
    orderByCol = portalPrefs.getValue("NAME_SPACE", "order-by-col", "Date");
    orderByType = portalPrefs.getValue("NAME_SPACE", "order-by-type", "asc");
    
    }
     %>
    
    
     <liferay-ui:search-container  delta='20' emptyResultsMessage="No Form Submitted" orderByCol="<%= orderByCol %>" orderByType="<%= orderByType %>">
    
     <liferay-ui:search-container-results>
    
    <%
                List<User> userList = UserLocalServiceUtil.getUsers(-1,-1);
                OrderByComparator orderByComparator =       
               CustomComparatorUtil.getUserOrderByComparator(orderByCol, orderByType);        
    
               Collections.sort(userList,orderByComparator);
    
              results = ListUtil.subList(userList, searchContainer.getStart(),   
                         searchContainer.getEnd());
    
                   if (userList.size()< total)
                    {total = userList.size();
                    }
    
                   pageContext.setAttribute("results", results);
                   pageContext.setAttribute("total", total);
    
     %>
    
    
     </liferay-ui:search-container-results>
    
        <liferay-ui:search-container-row
         className="com.liferay.portal.model.User"
    
         modelVar="user">
    
         <liferay-ui:search-container-column-text
          name="Screen Name"
          property="screenName"
          orderable="<%= true %>"
          orderableProperty="screenName"
         />
    
          <liferay-ui:search-container-column-text
          name="Email"
          property="emailAddress"
          orderable="<%= true %>"
         orderableProperty="emailAddress"
         />
    
          <liferay-ui:search-container-column-text
          name="Date"
          property="createDate"
          orderable="<%= true %>"
    
         />
    
    
       </liferay-ui:search-container-row>
    
        <liferay-ui:search-iterator />
    
    </liferay-ui:search-container>
    

    CustomComparatorUtil

    public static OrderByComparator getUserOrderByComparator(
                   String orderByCol, String orderByType) {
    
    
                   boolean orderByAsc = false;
    
                   if (orderByType.equals("asc")) {
                   orderByAsc = true;
                   }
    
                   OrderByComparator orderByComparator = null;
    
                    System.out.println("Custom "+ orderByCol);
                   if (orderByCol.equalsIgnoreCase("screenName")) {
                       System.out.println("1");
                    orderByComparator = new FirstNameComparator(orderByAsc);
                   }
                   else if (orderByCol.equalsIgnoreCase("emailAddress")) {
                       System.out.println("2");
                    orderByComparator = new EmailComparator(orderByAsc);
                   }
                   else if (orderByCol.equalsIgnoreCase("Date")) {
                     System.out.println("3");
                    orderByComparator = new DateComparator(orderByAsc);
                   }/*
                   else if (orderByCol.equalsIgnoreCase("Job Title")) {
    
                    orderByComparator = new JobTitleComparator(orderByAsc);
                   }*/
    
                   return orderByComparator;
                   }
    

    FirstNameComparator

    public static String ORDER_BY_ASC = "status ASC";

     public static String ORDER_BY_DESC = "status DESC";
    
    
      public FirstNameComparator()
      {
       this(false);
      }
    
      public FirstNameComparator(boolean asc) {
       _asc = asc;
      }
    
     public int compare(Object obj1, Object obj2) {
    
       User instance1 = (User) obj1;
       User instance2 = (User) obj2;
    
       int value = instance1.getFirstName().toLowerCase().compareTo(instance2.getFirstName().toLowerCase());
    
       if(_asc)
       {
        return value;
       } else
       {
        return -value;
       }
    
     }
    
    
     public String getOrderBy() {
    
      if (_asc) {
       return ORDER_BY_ASC;
      }
      else {
       return ORDER_BY_DESC;
      }
      }
    
     private boolean _asc;
    

    }

    similarly u can make class for emailaddress and date..