aspose file tools*
The moose likes JSF and the fly likes Problem with column sort in myFaces dataTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Problem with column sort in myFaces dataTable" Watch "Problem with column sort in myFaces dataTable" New topic
Author

Problem with column sort in myFaces dataTable

soumya kalla
Greenhorn

Joined: Jun 05, 2006
Posts: 12
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">
<tutputText value="#{screen.lastName}" />
</t:commandSortHeader>
</f:facet>
<tutputText value="#{tableRow.lastName}" />
</t:column>
<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="firstName" arrow="false">
<tutputText value="#{screen.firstName}" />
</t:commandSortHeader>
</f:facet>
<tutputText value="#{tableRow.firstName}" />
</t:column>
<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="loginName" arrow="false">
<tutputText value="#{screen.loginName}" />
</t:commandSortHeader>
</f:facet>
<tutputText value="#{tableRow.loginName}" />
</t:column>

<t:column>
<f:facet name="header">
<t:commandSortHeader columnName="team" arrow="false">
<tutputText value="#{screen.team}" />
</t:commandSortHeader>
</f:facet>
<tutputText 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

Joined: Jun 05, 2006
Posts: 12
Hello,

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

Thanks,
Soumya
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
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">
<hutputText value="#{messages.customerSearchResults_selectHeader}"></hutputText>
</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">
<hutputText value="#{messages.customerSearchResults_selectHeader}" styleClass="labelTextWhiteBold"></hutputText>
</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><hutputText value="#{messages.customerSearchResults_name}"></hutputText>
</t:commandSortHeader>
</f:facet>
<h:panelGrid border="0" columns="1" cellpadding="0" cellspacing="0" styleClass="resultDataWhite" >
<hutputText value="#{customer.item.name}"></hutputText>
</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><hutputText value="#{messages.customerSearchResults_incareof}" ></hutputText>
</t:commandSortHeader>
</f:facet>
<h:panelGrid border="0" columns="1" cellpadding="0" cellspacing="0" styleClass="resultDataWhite">
<hutputText value="#{customer.item.physicalAddress.inCareOfName}"></hutputText>
</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><hutputText value="#{messages.customerSearchResults_address}"></hutputText>
</t:commandSortHeader>
</f:facet>
<h:panelGrid border="0" columns="1" cellpadding="0" cellspacing="0" styleClass="resultDataWhite">
<hutputText value="#{customer.item.physicalAddress.address.addressLine1}"></hutputText>
<hutputText value="#{customer.item.physicalAddress.address.addressLine2}"></hutputText>
<hutputText value="#{customer.item.physicalAddress.address.city},#{customer.item.physicalAddress.address.state} #{customer.item.physicalAddress.address.zipBase} #{customer.item.physicalAddress.address.zipExt} "></hutputText>
</h:panelGrid>
</h:column>

<h:column>
<f:facet name="header">
<hutputText value="#{messages.customerSearchResults_updateLink}" styleClass="labelTextWhiteBold"></hutputText>
</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

Joined: Dec 21, 2006
Posts: 30
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;
}
Klaus Schuster
Ranch Hand

Joined: Feb 10, 2009
Posts: 30
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
 
wood burning stoves
 
subject: Problem with column sort in myFaces dataTable