myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sourcex <itpsy...@gmail.com>
Subject [Tomahawk] Sorting a datatable - small error
Date Tue, 15 Jul 2008 15:24:50 GMT

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.


Mime
View raw message