Hi Tom,

I presume we're talking about the "public void sort(String col)" method
defined in

examples/simple/src/java/org/apache/myfaces/examples/listexample/SortableList.java

I don't understand what that method is doing either, and suspect it is
not used at all. The SimpleSortableCarList.getCars method invokes its
local sort(col, asc) method, and that's what I expect to be done.

Of course when the data is being retrieved from a database rather than
from a local array then there may be no sort method at all; the SQL
query simply specifies the appropriate "ORDER BY" clause. All that
really matters is that the list returned by the getList method is in the
order specified by the current values of the column name and
ascending/descending properties.

I'll try to find time to check the example and verify whether that
SortableList.sort method is used at all. Or you could try deleting it
and letting us know if the example still works. As I mentioned earlier,
I'm also not a fan of methods that mysteriously manipulate the state of
an object, like the "sort" methods do, so it would be nice to change
that too.

Regards,

Simon


On Sun, 2006-01-01 at 17:52 -0500, Tom Butler wrote:
> Thanks for your help with this Simon - I was able to get my sort to work.
> 
> One question I still have - in the MyFaces examples, a SortTableList
> abstract class is used (see snippet below.)  In my code I call a sort method
> that I implement within my bean - it has 2 parameters: the column name and
> the ascending values.  This is the sort routine I call within my bean from
> my getDataModel getter method.  What I wasn't able to understand is
> where/when is the  sort method (with one parameter - sortColumn) within the
> SortableList class called from?
> 
> thanks
> 
> 
> /***** sortablist class from myfaces examples ********
> public abstract class SortableList
> {
>     private String _sort;
>     private boolean _ascending;
> .....
> 
>         public void sort(String sortColumn)
>     {
>         ....
>     }
> *********************************************/
> 
> /*********  sort routine in my bean **************
> protected void sort(final String column, final boolean ascending) {
>       Comparator comparator = new Comparator() {
>       public int compare(Object o1, Object o2) {
>               bean_providerdata p1 = (bean_providerdata) o1;
>               bean_providerdata p2 = (bean_providerdata) o2;
>               if (column == null) {
>                       return 0;
>               }
>               if (column.equals("id")) {
>                       return ascending ? p1.getId().compareTo(p2.getId())
> : p2
>       
> .getId().compareTo(p1.getId());
>               } else if (column.equals("category")) {
>                       return ascending ? p1.getCategory().compareTo(
>                       p2.getCategory()) : p2.getCategory().compareTo(
>                                                       p1.getCategory());
>               } else
>                       return 0;
>               }
>       };
>       Collections.sort(arrlist_providers, comparator);
> }
> ********************************/
> 
> -----Original Message-----
> From: Simon Kitching [mailto:[EMAIL PROTECTED] 
> Sent: Friday, December 30, 2005 11:20 PM
> To: MyFaces Discussion
> Subject: Re: Data Table Sorting still not working
> 
> Hi Tom/Mike,
> 
> The "sortColumn" attribute on the <t:dataTable> tag points to a javabean 
> *property* of type String.
> 
> eg:
>    <t:dataTable
>      value="#{mybean.dataToDisplay}
>      sortColumn="#{mybean.sortByColumn}" .../>
> 
> where the managed bean declaration maps name "mybean" to an instance of 
> MyBean which has:
> 
>    public class MyBean {
>      private String sortCol;
>      public void setSortByColumn(String columnName) {
>          this.sortCol = columnName;
>      }
>      public String getSortByColumn() {
>          return this.sortCol;
>      }
> 
>      public List getDataToDisplay() {
>          // get the data to display from wherever is appropriate
>          // sort it according to the current value of this.sortCol
>          // return the list
>      }
>    }
> 
> Each <t:commandSortHeader> tag has a "columnName" attribute. When the 
> user clicks on that column, the page is submitted. The commandSortHeader 
> looks into its parent t:dataTable to find the "sortColumn" EL expression 
> it assigns to, rather than requiring the mapping to be defined on each 
> and every commandSortHeader tag. In this example, it finds 
> "#{mybean.sortByColumn}" so ends up passing the columnName value for the 
> clicked-on header to method MyBean.setSortByColumn [1].
> 
> The commandSortHeader then forces processing to jump to the rendering 
> phase immediately (it is an IMMEDIATE component by default), at which 
> point the dataTable then fetches its "value", ie calls method 
> getDataToDisplay. The table is then re-rendered using this data list and 
> the user sees the new table, in the new order.
> 
> [1] Model properties aren't normally updated during the "validation" 
> phase. However some special-case code does this for the sortColumn 
> property because the commandSortHeader is usually immediate yet the 
> method that returns the data (getDataToDisplay in this example) needs to 
> know that value.
> 
> The setSortByColumn method should therefore be called once each time a 
> click on a sortable header occurs. I don't know whether the 
> getSortByColumn method is ever called; it *might* be needed in order to 
> know which column to render the arrow on, or maybe that's figured out in 
> some other way in which case it might not be called at all.
> 
> I don't know what the '#' is doing on the end of your URL, but I don't 
> think it's important. That's just an "anchor" identifier in HTML terms, 
> and shouldn't be significant in this case.
> 
> You can always see the latest source code here:
>    http://svn.apache.org/repos/asf/myfaces/examples/trunk/simple/
> see especially the "src" subdirectory.
> 
> A "managed bean" and a "backing bean" are really the same thing.
> Technically, there is no such thing as "a backing bean" in JSF, as each 
> component in a page can map to a different bean if it wants. However in 
> practice most/all components on a page map to the same bean (ie have the 
> same bean name for all #{beanname.zzzzz} expressions), so referring to 
> that bean as "the backing bean for the page" is common. And technically 
> that bean does not *have* to be a managed bean (ie an instance defined 
> in the <managed-bean> config tags) - though in practice almost all the 
> time this *is* how beans referenced from a page are defined.
> 
> Regards,
> 
> Simon
> 
> Mike wrote:
> > Hi Tom,
> > 
> > The way I understand it is that the sort() method gets called 
> > automatically when you define your data table with an entry like this:
> > 
> > sortColumn="#{list.sort}" 
> > 
> > (you have to have, in this case, a sort property with a getter/setter in 
> > whatever class the list managed bean points to)
> > 
> > (per the sortTable.jsp example with the MyFaces source code).
> > 
> > So the list.sort, as I understand it, is the sort property in the list 
> > class (or whatever class it really is based on faces-config).
> > 
> > Then, as I understand it, the <t:commandSortHeader...> will (somehow) 
> > send the value (what you've defined in the columnName) into the list 
> > class (in the above example and also magically call the sort() method 
> > based on the sortColumn you defined.
> > 
> > This is the magical part that doesn't work for me at all.
> > 
> > So, I'm clearly not understanding ... SOMETHING!
> > 
> > I can match what I've done almost method for method, property to 
> > property, to the "SimpleCarSort" in the example source.
> > 
> > Maybe I haven't configured something in faces-config.xml correctly -- 
> > maybe I need another listener?
> > 
> > I really don't know.
> > 
> > Tomorrow I'm going to try to get the example one working from the source 
> > locally (the car type/color) example. That may help validate I'm OK or 
> > there's some wacky configuration problem.
> > 
> > Frustrating? Yup, I've spent two full days just trying to get the click 
> > of the header to actually do something.
> > 
> > My URL also gets a "#" when I click the column appended to the end. I 
> > have no idea what that signifies. But, if I manually remove it, I see 
> > that the sort code runs but ALWAYS on the initial sort column -- nothing 
> > to do with me clicking a particular colulmn.
> > 
> > My up arrow is always pointed down and it always stays on the first 
> > column even when I click the second "sortable" column.
> > 
> > Also, my getSortColumn() never gets called.
> > 
> > I have a managed bean, but the posts talk about a "backing bean". I 
> > think this is really the same, but, yet another question mark.
> > 
> > Sigh....
> > 
> > I'll write again tomorrow with my results from running, 'er, attempting 
> > to run, the source example.
> > 
> > Write with any results you get too.
> > 
> >     Mike
> > 
> > ------------------------------------------------------------------------
> > 
> > 
> > Tom Butler wrote:
> >>
> >> Mike - are you calling the sort method from within your getUpdates 
> >> getter method in your bean (I'm assuming Updates is your 
> >> arraylist/datamodel since you have value ="#{TD.updates}" ?
> >>
> >>  
> >>
> >> Note I'm not sure if this is needed or not - you are ahead of where I 
> >> am with regards to getting this working  - I'm still working on 
> >> understanding how to implement this;  The reason I'm asking about 
> >> including the sort in the getter method is because of a response I 
> >> received from another mail list (mistakedly sent this to the dev mail 
> >> list earlier):
> >>
> >>  
> >>
> >> "-----Original Message-----
> >> From: Thomas Spiegl [mailto:[EMAIL PROTECTED]
> >> Sent: Thursday, December 29, 2005 6:02 AM
> >> To: MyFaces Development
> >> Subject: Re: Tomahawk DataTable Sort Question - where/how is Sort Coded?
> >>
> >>  
> >>
> >> Sort your data in getArrlist_providers depending on the values of
> >>
> >> sortcol and sortascending.
> >>
> >> Collections.sort(arrlist_providers, YourComparator) may sort the list.
> >>
> >> "
> >>
> >>  
> >>
> >> Also, here is another example I just found that shows calling the sort 
> >> within the getter method: 
> >>
> >>
> http://mail-archives.apache.org/mod_mbox/myfaces-commits/200505.mbox/%3C2005
> [EMAIL PROTECTED]
> >>
> >>  
> >>
> >> One good working example end-to-end (complete code) would be very 
> >> helpful.  The link above is the closet I have found, however, I'm 
> >> still deciphering it - for example, sort(getsortt(), getascending()) - 
> >> I'm not sure what the getsort() actually calls or resolves to?
> >>
> >>  
> >>
> >>  
> >>
> >> Thanks
> >>
> >> Tom
> >>
> >>                     
> >>
> >>  
> >>
> >>  
> >>
> >> ------------------------------------------------------------------------
> >>
> >> *From:* Mike [mailto:[EMAIL PROTECTED]
> >> *Sent:* Friday, December 30, 2005 4:23 PM
> >> *To:* MyFaces Discussion
> >> *Subject:* Data Table Sorting still not working
> >>
> >>  
> >>
> >> Hi Geeta ,
> >>
> >> (Sorry I got your name wrong before....)
> >>
> >> Below was a typo. Sorry. It should have been Persons, not getPersons.
> >>
> >> I tried to replace TD with a longer name but that didn't help.
> >>
> >> I'm baffled. The "hard" stuff, the programming, is so easy compared to 
> >> just "hooking up" the Data Table component. I've now spent another day 
> >> on this and have gotten nowhere.
> >>
> >> For some reason, the commandSortHeader is just not working. I've also 
> >> tried to download a fresh "myfaces-all.jar" in case that was it. And, 
> >> I've tried to disable my firewall software. No dice. When I compare 
> >> the sample to what I have, they seem identically set up. Of course, I 
> >> can't yet verify the sample would run here either.
> >>
> >> I suppose what I'll need to do is get the example installed an 
> >> working. I'm afraid that will be as difficult as what I'm facing now 
> >> with all the config file tweaks and such.
> >>
> >> Thanks very much for your help!!!
> >>
> >>     Mike
> >>
> >>
> >>
> >>
> >> [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> wrote:
> >>
> >>
> >> Mike <[EMAIL PROTECTED]> 
> >> <mailto:[EMAIL PROTECTED]> wrote on 12/30/2005 12:36:39 PM:
> >>
> >> > Hi Gretta!
> >> >
> >> > Thanks for your reply.
> >> > Please see my comments in-line below....
> >>
> >> >
> >> > Mine is:  value ="#{TD.getPersons}"  
> >> >
> >>
> >> Shouldn't you simply have "#{TD.persons}" ? Here's what i have:
> >> <t:dataTable id="contactsearchdata" styleClass="scrollerTable"
> >>         headerClass="standardTable_BgWhiteHeader"
> >>         footerClass="standardTable_Header"
> >>         rowClasses="standardTable_Row1,standardTable_Row2"
> >>         
> >>
> columnClasses="standardTable_ColumnCentered,standardTable_Column,standardTab
> le_Column,standardTable_ColumnCentered,standardTable_ColumnCentered,standard
> Table_ColumnCentered" 
> >>
> >>         var="contact"
> >>       value="#{search.matchingContacts}"
> >>         rows="7"
> >>         rowId="#{contact.id}"
> >>         sortColumn="#{search.sort}"
> >>         sortAscending="#{search.ascending}"
> >>         preserveSort="true">
> >>
> >>
> >> Also, if all else fails, try replacing your name "TD" with something 
> >> else. I remember (though this was a long time ago!) short names which 
> >> were capitalised created trouble with reflection..
> >>
> >> >
> >> >
> >> > Strange, huh?
> >> >
> >> >     Mike
> >>
> >> Regards,
> >> Geeta (..umm.. not "Gretta" ;))
> >>
> >>  
> >>
> > 
> 

Reply via email to