• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem with column sort in myFaces dataTable

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In my dataTable I am trying to do a column sort on each column, I got some examples off of the net, and tried, but when i click on the column header link nothing is happening. Could somebody please help. Please let me know if I am missing something.

Here is the code.

The jsf is as follows:

<h:panelGrid columns="1" styleClass="tableHeaderAction">
<t:dataTable id="fooS" value="#{supervisorsDto.tableRows}"
var="tableRow" rows"30" styleClass="dataTable"
headerClass="tableHeader" first="0"
columnClasses="string,string,string,string"
rowClasses="odd,even"
sortAscending="#{supervisorsDto.ascending}"
sortColumn="#{supervisorsDto.sort}"
preserveSort="true" >
<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="lastName" arrow="false">
<t:outputText value="#{screen.lastName}" />
</t:commandSortHeader>
</f:facet>
<t:outputText value="#{tableRow.lastName}" />
</t:column>
<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="firstName" arrow="false">
<t:outputText value="#{screen.firstName}" />
</t:commandSortHeader>
</f:facet>
<t:outputText value="#{tableRow.firstName}" />
</t:column>
<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="loginName" arrow="false">
<t:outputText value="#{screen.loginName}" />
</t:commandSortHeader>
</f:facet>
<t:outputText value="#{tableRow.loginName}" />
</t:column>

<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="team" arrow="false">
<t:outputText value="#{screen.team}" />
</t:commandSortHeader>
</f:facet>
<t:outputText value="#{tableRow.team}" />
<h:inputHidden id="key" value="#{tableRow.empID}" />
</t:column>
</t:dataTable>
</h:panelGrid>


The sort method in SupervisorsDto is as follows:

public class SupervisorsDto implements Serializable {

private String sort="lastName";
private boolean ascending= true;


@SuppressWarnings("unchecked")
public void sort(final String column,final boolean ascending)
{
Comparator comparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
Supervisors c1 = (Supervisors)o1;
Supervisors c2 = (Supervisors)o2;
if (column == null)
{
return 0;
}
if (column.equals("lastName"))
{
return ascending ? c1.getLastName().compareTo(c2.getLastName()) : c2.getLastName().compareTo(c1.getLastName());
}
else if (column.equals("firstName"))
{
return ascending ? c1.getFirstName().compareTo(c2.getFirstName()) : c2.getFirstName().compareTo(c1.getFirstName());
}
else if (column.equals("loginName"))
{
return ascending ? c1.getLoginName().compareTo(c2.getLoginName()) : c2.getLoginName().compareTo(c1.getLoginName());
}
else if (column.equals("team"))
{
return ascending ? c1.getTeam().compareTo(c2.getTeam()) : c2.getTeam().compareTo(c1.getTeam());
}
else return 0;
}
};
Collections.sort(tableRows, comparator);
}

public void sort(String sortColumn)
{
if (sortColumn == null)
{
throw new IllegalArgumentException("Argument sortColumn must not be null.");
}

if (sort.equals(sortColumn))
{
//current sort equals new sortColumn -> reverse sort order
ascending = !ascending;
}
else
{
//sort new column in default direction
sort = sortColumn;
ascending = isDefaultAscending(sort);
}

sort(sort, ascending);
}

protected boolean isDefaultAscending(String sortColumn)
{
return true;
}

public String getSort() {
return sort;
}

public void setSort(String sort) {
this.sort = sort;
}

public boolean isAscending() {
return ascending;
}

public void setAscending(boolean ascending) {
if(ascending != this.ascending)
this.ascending = ascending;
}

Thanks,
Soumya
 
soumya kalla
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Could somebody please help me with this problem,i've been waiting for a reply for a long time.

Thanks,
Soumya
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We are using the commandSortHeader and it is working fine.
Here is the code......

The Jsf page has....

<t:dataTable id="individual" value="#{customerSearchBean.selectableItemList}" var="customer" styleClass="headerTable" headerClass="tableHeader" rowClasses="tableOddRow, tableEvenRow" rows="10" rowId="#{customer.item.customerId}" sortColumn="#{customerSearchBean.sortColumn}" sortAscending="#{customerSearchBean.ascending}" preserveDataModel="false" preserveSort="true" binding="#{customerSearchBean.customerSearchResultsTable}"
width="650">

<h:column ><f:facet name="header">
<h:outputText value="#{messages.customerSearchResults_selectHeader}"></h:outputText>
</f:facet>
<h:selectBooleanCheckbox value="#{customer.selected}" >
<a4j:support event="onclick" action="#{customerSearchBean.selectCustomerAction}" reRender="viewActivity,merge,next,searchAgain"/>
</h:selectBooleanCheckbox>
</h:column>

<h:column >
<f:facet name="header">
<h:outputText value="#{messages.customerSearchResults_selectHeader}" styleClass="labelTextWhiteBold"></h:outputText>
</f:facet>
<t:selectOneRadio id="selectRadio" forceId="true" forceIdIndex="false" value="#{customerSearchBean.itemId}">
<f:selectItem itemValue="#{customer.item.customerId}" itemLabel="" />
</t:selectOneRadio>
</h:column>

<h:column>
<f:facet name="header">
<t:commandSortHeader id="firstName" columnName="Name" arrow="true" styleClass="labelHeaderWhiteBold">
<f:verbatim escape="false">      </f:verbatim><h:outputText value="#{messages.customerSearchResults_name}"></h:outputText>
</t:commandSortHeader>
</f:facet>
<h:panelGrid border="0" columns="1" cellpadding="0" cellspacing="0" styleClass="resultDataWhite" >
<h:outputText value="#{customer.item.name}"></h:outputText>
</h:panelGrid>
</h:column>

<h:column>
<f:facet name="header">
<t:commandSortHeader id="incareOf" columnName="incare" arrow="true" styleClass="labelHeaderWhiteBold" >
<f:verbatim escape="false">      </f:verbatim><h:outputText value="#{messages.customerSearchResults_incareof}" ></h:outputText>
</t:commandSortHeader>
</f:facet>
<h:panelGrid border="0" columns="1" cellpadding="0" cellspacing="0" styleClass="resultDataWhite">
<h:outputText value="#{customer.item.physicalAddress.inCareOfName}"></h:outputText>
</h:panelGrid>
</h:column>

<h:column>
<f:facet name="header">
<t:commandSortHeader id="addressLine1" columnName="addressLine1" arrow="true" styleClass="labelHeaderWhiteBold">
<f:verbatim escape="false">      </f:verbatim><h:outputText value="#{messages.customerSearchResults_address}"></h:outputText>
</t:commandSortHeader>
</f:facet>
<h:panelGrid border="0" columns="1" cellpadding="0" cellspacing="0" styleClass="resultDataWhite">
<h:outputText value="#{customer.item.physicalAddress.address.addressLine1}"></h:outputText>
<h:outputText value="#{customer.item.physicalAddress.address.addressLine2}"></h:outputText>
<h:outputText value="#{customer.item.physicalAddress.address.city},#{customer.item.physicalAddress.address.state} #{customer.item.physicalAddress.address.zipBase} #{customer.item.physicalAddress.address.zipExt} "></h:outputText>
</h:panelGrid>
</h:column>

<h:column>
<f:facet name="header">
<h:outputText value="#{messages.customerSearchResults_updateLink}" styleClass="labelTextWhiteBold"></h:outputText>
</f:facet>
<f:verbatim escape="false">    </f:verbatim>
<h:commandLink id="updateLink" value="#{messages.customerSearchResults_updateLink}" action="#{customerAddBean.updateCustomer}" styleClass="labelTextBlack">
<f:param name="selectedCustId" value="#{customer.item.customerId}" />
</h:commandLink>
</h:column>
</t:dataTable>


And the sort method is


protected void sort(final String column, final boolean ascending) {
Comparator comparator = new Comparator() {

public int compare(final Object o1, final Object o2) {
int result = 0;

if (o1 instanceof IndividualCustomer
&& o2 instanceof IndividualCustomer) {
IndividualCustomer c1 = (IndividualCustomer) o1;
IndividualCustomer c2 = (IndividualCustomer) o2;
if (column == null) {
result = 0;
}
if (column.equals(FIRST_NAME_COLUMN)) {
result = ascending ? c1.getFirstName().compareTo(
c2.getFirstName()) : c2.getFirstName()
.compareTo(c1.getFirstName());
} else if (column.equals(MIDDLE_NAME_COLUMN)) {
result = ascending ? c1.getMiddleName().compareTo(
c2.getMiddleName()) : c2.getMiddleName()
.compareTo(c1.getMiddleName());
} else if (column.equals(LAST_NAME_COLUMN)) {
result = ascending ? c1.getLastName().compareTo(
c2.getLastName()) : c2.getLastName().compareTo(
c1.getLastName());
} else if (column.equals(ADDRESS_LINE_1_COLUMN)) {
// TODO: sort logic
} else if (column.equals(ADDRESS_LINE_2_COLUMN)) {
// TODO: sort logic
} else if (column.equals(CITY_COLUMN)) {
// TODO: sort logic
} else if (column.equals(STATE_COLUMN)) {
// TODO: sort logic
} else if (column.equals(ZIP_COLUMN)) {
// TODO: sort logic
} else {
result = 0;
}
} else if (o1 instanceof BusinessCustomer
&& o2 instanceof BusinessCustomer) {
// business logic
} else {
// government logic
}

return result;
}
};

Collections.sort(listToSort, comparator);
}
 
rajani varma
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public final List getListToSort() {
sort(getSortColumn(), isAscending());
return listToSort;
}

public final void setListToSort(List listToSort) {
this.listToSort = listToSort;
}

public final void sort(final String sortColumn) {
if (sortColumn == null) {
throw new IllegalArgumentException("Argument sortColumn must not be null.");
}

if (this.sortColumn.equals(sortColumn)) {
// current sort equals new sortColumn -> reverse sort order
ascending = !ascending;
} else {
// sort new column in default direction
this.sortColumn = sortColumn;
ascending = isDefaultAscending(this.sortColumn);
}

sort(this.sortColumn, ascending);
}

/**
* @return Returns the ascending.
*/
public final boolean isAscending() {
return ascending;
}

/**
* @param ascending
* The ascending to set.
*/
public final void setAscending(final boolean ascending) {
this.ascending = ascending;
}

/**
* @return Returns the sortColumn.
*/
public final String getSortColumn() {
return sortColumn;
}

/**
* @param sortColumn
* The sortColumn to set.
*/
public final void setSortColumn(final String sortColumn) {
this.sortColumn = sortColumn;
}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorting works fine, but what happens if you navigate to another page, and back to the page with the data table?

in my case the backingBean is holding the right sortColumns in its getter Method, but it doesn't sort on loading the page with the pre klicked sorted properties.
Is there a binding for the <t:commandSortHeader necessary, or how can I solve the problem?

Thanks for reply
 
    Bookmark Topic Watch Topic
  • New Topic