You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Hasnain Badami <ha...@gmail.com> on 2007/12/07 17:26:32 UTC

Datatable Sorting issues

Guys

I am trying to upgrade to myfaces 1.2 and tomahawk 1.1.6. I have got some
strange problem with the datatable (it used to work fine with tomahawk 1.1.3
).

The problem is based around table sorting. I have provided a comparator to
sort as one of my datatable fields is a date. I see that the first time its
sorted according to the field date (not alphabetically), but as you click on
the heading to re-sort it gives you the result of an alphabetic sort. I
tried to print my backing bean list in its getter method after calling the
sort function and that result seems to be fine. It s only in the output
where I see alphabetic sorting. Any ideas what/why it is happening?
The code is


<t:dataTable

styleClass="tableWithoutFooter"

rowClasses="standardTableRow1, standardTableRow2"

var="doc"

value="#{testUI.tableContent}"

sortColumn="#{testUI.sort}"

sortAscending="#{testUI.ascending}"

preserveSort="true"

sortable="true"

align="center"

id="documentsDataTable"

forceId="#{doc.id}"

rowId="#{doc.id}"

>

<t:column styleClass="alignLeft" onmouseover="hiLite(this);" onmouseout=
"loLite(this);">

<f:facet name="header">

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

</f:facet>

<h:selectBooleanCheckbox id="selectCheckBox" value="#{doc.markedForDownload}
" />

</t:column>

<t:column styleClass="alignLeft" onmouseover="hiLite(this);" onmouseout=
"loLite(this);">

<f:facet name="header">

<t:commandSortHeader columnName="portfoliocontent" arrow="false" immediate=
"false">

<f:facet name="ascending">

<t:graphicImage value="img/ascending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

<f:facet name="descending">

<t:graphicImage value="img/descending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

<h:outputText value="#{messages['portfolio']} #{messages['content']}"/>

</t:commandSortHeader>

</f:facet>

<h:outputText value="#{doc.portfolioContent}"/>

</t:column>

<t:column styleClass="alignLeft" onmouseover="hiLite(this);" onmouseout=
"loLite(this);">

<f:facet name="header">

<t:commandSortHeader columnName="document" arrow="false" immediate="false">

<f:facet name="ascending">

<t:graphicImage value="img/ascending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

<f:facet name="descending">

<t:graphicImage value="img/descending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

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

</t:commandSortHeader>

</f:facet>

<h:outputText value="#{doc.name}"/>

</t:column>

<t:column styleClass="alignLeft" onmouseover="hiLite(this);" onmouseout=
"loLite(this);">

<f:facet name="header">

<t:commandSortHeader columnName="date" arrow="false" immediate="false" >

<f:facet name="ascending">

<t:graphicImage value="img/ascending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

<f:facet name="descending">

<t:graphicImage value="img/descending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

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

</t:commandSortHeader>

</f:facet>

<h:outputText value="#{doc.dateString}" />

</t:column>

<t:column styleClass="alignLeft" onmouseover="hiLite(this);" onmouseout=
"loLite(this);">

<f:facet name="header">

<t:commandSortHeader columnName="type" arrow="false" immediate="false" >

<f:facet name="ascending">

<t:graphicImage value="img/ascending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

<f:facet name="descending">

<t:graphicImage value="img/descending-arrow.png" rendered="true" border="0"
height="10px"/>

</f:facet>

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

</t:commandSortHeader>

</f:facet>

<h:outputText value="#{doc.type}"/>

</t:column>

</t:dataTable>



The comparator is



 private class PortfolioContentDocumentComparator implements
Comparator<PortfolioContentDocument>, Serializable
 {
  private static final long serialVersionUID = 1L;
  String sortColumn;
  Direction dir;

  public PortfolioContentDocumentComparator(String sortColumn, Direction
dir)
  {
   this.sortColumn = sortColumn;
   this.dir = dir;
  }

     public int compare(PortfolioContentDocument r0,
PortfolioContentDocument r1)
     {
       try
       {
        int toReturn = 0;
        // We do all the calculation for one direction then multiply it by
-1 id it is descend;
    if (r0 == null && r1 == null)
    {
     toReturn = 0;
    }
    else if (r0 == null && r1 != null)
    {
     toReturn = 1;
    }
    else if (r0 != null && r1 == null)
    {
     toReturn = -1;
    }
    else
    {
     Object o0 = getPropertyValue(r0);
     Object o1 = getPropertyValue(r1);
     if (o0 == null && o1 == null)
     {
      toReturn = 0;
     }
     else if (o0 == null && o1 != null)
     {
      toReturn = 1;
     }
     else if (o0 != null && o1 == null)
     {
      toReturn = -1;
     }
     else
     {
      toReturn = ((Comparable) o0).compareTo((Comparable) o1);
     }
    }
    if (dir == Direction.ASCEND) toReturn = -1 * toReturn;
    return toReturn;
       }
       catch (ClassCastException e)
       {
        e.printStackTrace();
        return 0;
      }
     }
     private Object getPropertyValue(PortfolioContentDocument r)
     {
      if(sortColumn.equals(HeadingName.PortfolioContent.name
().toLowerCase()))
       return r.getPortfolioContent();
      else if(sortColumn.equals(HeadingName.Document.name().toLowerCase()))
       return r.getName();
      else if(sortColumn.equals(HeadingName.Date.name().toLowerCase()))
       return r.getDate();
      else if(sortColumn.equals(HeadingName.Type.name().toLowerCase()))
       return r.getType();
      return null;
     }

 }
Backing bean code is

 public TestUI() throws ViewException
 {
  log.info("Executing PortfolioDetailDocumentsViewUI");
  initialize();
  log.info("Exiting PortfolioDetailDocumentsViewUI");
 }
 private void initialize() throws ViewException
 {
  FacesContext fc = FacesContext.getCurrentInstance();
  PortfolioDetailUI portfolioDetailUI =
(PortfolioDetailUI)fc.getApplication().getVariableResolver().resolveVariable(fc,
"portfolioDetailUI");
  this.sessionKey = portfolioDetailUI.getSessionKey();
  this.portfolio = "48581546";

  generateRecentDocumentsTable(); //used to get data from delegate functions
  sort="date";
  ascending=true;

 }
 public void sort()
 {
  log.info("Executing sort()");
  Direction direction = null;
  if(isAscending()) direction = Direction.ASCEND;
  else direction = Direction.DESCEND;
  Collections.sort(tableContent, new
PortfolioContentDocumentComparator(sort, direction));
  log.info("Exiting sort()");
  return;
 }

I can provide more code if required. Thanks in advance