You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by sourcex <it...@gmail.com> on 2008/07/15 17:24:50 UTC

[Tomahawk] Sorting a datatable - small error

I'm having an issue with sorting a t:dataTable component - seems like I'm
missing something. 

Here it is the table:

<t:dataTable var="timesheet"
						value="#{consultantBean.listUserTimesheetObjs}"
						sortColumn="#{consultantBean.sortColumn}"
						sortAscending="#{consultantBean.ascending}"
						 preserveDataModel="false"
						preserveSort="true">
						<h:column>
							<f:facet name="header">
								<t:commandSortHeader columnName="year" arrow="true">
									<h:outputText value="Year" />
								</t:commandSortHeader>
							</f:facet>
							<h:outputText value="#{timesheet.year}" />
						</h:column>
						<h:column>
							<f:facet name="header">
								<t:commandSortHeader columnName="month" arrow="true">
									<h:outputText value="Month" />
								</t:commandSortHeader>
							</f:facet>
							<h:outputText value="#{timesheet.month}" />
						</h:column>
						<h:column>
							<f:facet name="header">
								<t:commandSortHeader columnName="company" arrow="true">
									<h:outputText value="Company" />
								</t:commandSortHeader>
							</f:facet>
							<h:outputText value="#{timesheet.company.name}" />
						</h:column>
					</t:dataTable>

And that's the logic in my bean:


public void sort(final String sortColumn, final boolean ascending)
	{
		this.sortColumn = sortColumn;
	   Comparator<Timesheet> comparator = new Comparator<Timesheet>()
	   {
	       public int compare(Timesheet t1, Timesheet t2)
	       {
	           if (sortColumn == null)
	           {
	               return 0;
	           }
	           if (sortColumn.equals("year"))
	           {
	               return ascending ? (t1.getYear() - t2.getYear()) :
(t1.getYear() - t2.getYear());
	           }
	           else if (sortColumn.equals("month"))
	           {
	               return ascending ? (t1.getMonth() - t2.getMonth()) :
(t1.getMonth() - t2.getMonth());
	           }
	           else if (sortColumn.equals("company"))
	           {
	               return ascending ?
(t1.getCompany().getName().compareTo(t2.getCompany().getName())) :
(t2.getCompany().getName().compareTo(t1.getCompany().getName()));
	           }
	           else return 0;
	      }
	   };
	   this.ascending = ascending;
	   Collections.sort(getListUserTimesheetObjs(), comparator);
	}


Thanks in advance for the tips!
-- 
View this message in context: http://www.nabble.com/-Tomahawk--Sorting-a-datatable---small-error-tp18468002p18468002.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: [Tomahawk] Sorting a datatable - small error

Posted by sourcex <it...@gmail.com>.
The solution was changing the h:column with t:column with sortable="true"
attribute. ;)

Cheers
-- 
View this message in context: http://www.nabble.com/-Tomahawk--Sorting-a-datatable---small-error-tp18468002p18503260.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.