RE: Data Table Sorting still not working
Thanks Geeta this was very helpful to me in getting my datatable column sort functionality to work. From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Friday, December 30, 2005 11:41 PM To: MyFaces Discussion Subject: RE: Data Table Sorting still not working Mike, Tom: FirstMike, maybe it was a typo, but you said you had Persons. Hope you meant persons (lower case). Here's a complete example which works fine for me. I'll try to explain what i understand what needs to be done. But first the code: My backing bean (or the bean that's behind the current jsp) is SearchBean. It has a list of contacts which I am trying to display in a datatable. This is the list that i want sorted. public class SearchBean extends AbstractViewController implements Serializable { private Contact contact = new Contact(); private List matchingContacts = new ArrayList(); private transient DataModel matchingContactsModel = new ListDataModel(); /* * Code below is added on for sorting. */ private String _sort; private boolean _ascending; public SearchBean() { _sort = lastname; _ascending = isDefaultAscending(lastname); } /* * This method is needed so consequtive clicks on header will sort * asecending/descending/ascending/descending etc.. */ public SearchBean(String defaultSortColumn) { _sort = defaultSortColumn; _ascending = isDefaultAscending(defaultSortColumn); } protected boolean isDefaultAscending(String sortColumn) { return true; } protected void sort(final String column, final boolean ascending) { Comparator comparator = new Comparator() { public int compare(Object o1, Object o2) { Contact c1 = (Contact) o1; Contact c2 = (Contact) o2; if (column == null) { return 0; } if (column.equals(firstname)) { return ascending ? c1.getFirstName().compareTo( c2.getFirstName()) : c2.getFirstName().compareTo( c1.getFirstName()); } else if (column.equals(lastname)) { return ascending ? c1.getLastName().compareTo( c2.getLastName()) : c2.getLastName().compareTo( c1.getLastName()); } else if (column.equals(ssn)) { return ascending ? c1.getSocialSecurityNumber().compareTo( c2.getSocialSecurityNumber()) : c2 .getSocialSecurityNumber().compareTo( c1.getSocialSecurityNumber()); } else if (column.equals(callbacknumber)) { return ascending ? c1.getCallBackNumber().compareTo( c2.getCallBackNumber()) : c2.getCallBackNumber() .compareTo(c1.getCallBackNumber()); } else if (column.equals(dateofbirth)) { return ascending ? c1.getDob().compareTo(c2.getDob()) : c2 .getDob().compareTo(c1.getDob()); } else return 0; } }; Collections.sort(matchingContacts, comparator); } public DataModel getMatchingContacts() { if ((matchingContactsModel == null) || (matchingContactsModel.getRowCount() = 0)) { matchingContactsModel = new ListDataModel(); matchingContactsModel.setWrappedData(matchingContacts); } sort(_sort, _ascending); return matchingContactsModel; } 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); } public String getSort() { return _sort; } public void setSort(String sort) { _sort = sort; } public boolean isAscending() { return _ascending; } public void setAscending(boolean ascending) { _ascending = ascending; } } Finally in my jsp: h:form id=contactscrollerpagerform t:saveState id=savesearchstate value=#{search} / h:panelGrid styleClass=scrollerTableNoBorder h:column t:dataTable id=contactsearchdata var=contact value=#{search.matchingContacts} rows=7 rowId=#{contact.id} sortColumn=#{search.sort} sortAscending=#{search.ascending} preserveSort=true h:column f:facet name=header t:commandSortHeader columnName=lastname arrow=true immediate=false h:outputText value=Last Name / /t:commandSortHeader /f:facet h:outputText value=#{contact.lastName} / /h:column h:column f:facet name=header t:commandSortHeader columnName=firstname arrow=true immediate=false h:outputText value=First Name / /t:commandSortHeader /f:facet h:outputText value=#{contact.firstName} / /h:column etc.. I quote below what Simon wrote and point out how I that is realised in my code: The sortColumn attribute should point to a backing bean property that is of type String. That property gets set (ie its setter gets called) with the columnName value of whatever column the user chose to sort on. So in above example, I have sortColumn=#{search.sort}. So my bean has a String called _sort, and a getSort() and a setSort(String). The sortAscending attribute should point to a backing bean property that is of type boolean
RE: Data Table Sorting still not working
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.z} expressions), so referring to that bean as the backing bean for the page is common
RE: Data Table Sorting still not working
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
RE: Data Table Sorting still not working
Simon - thanks for looking into this. I was just curious about this - my implementation is working, so please don't spend any time on my behalf. I was planning to add a print method to the SortableList.sort(string..) method to see if its actually being called - will let you know what I find out if I do this. thx -Original Message- From: Simon Kitching [mailto:[EMAIL PROTECTED] Sent: Sunday, January 01, 2006 6:43 PM To: MyFaces Discussion Subject: RE: Data Table Sorting still not working 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/SortableLis t.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
Re: Data Table Sorting still not working
Tom, I had the same problem. I later found that I needed to download the actual source code (source=documentation, unfortunately s). Check out: http://myfaces.apache.org/source.cgi HTH. -- Jim Tom Butler wrote: Mike do you know how to access the source code for the java components for the examples (i.e., the source code for the models/beans/controller components)? When I unzip the source code for the examples, I get the .jsp page source, but only the .class (compiled) source for the java components Id like to take a look at these source code files as well to see how they are working (i.e., how they have coded the sort method.) Ill keep you posted as to what I find out / or if I can get this to work sounds like we are both trying to get the same functionality working. Thanks. From: Mike [mailto:[EMAIL PROTECTED]] Sent: Friday, December 30, 2005 6:03 PM To: MyFaces Discussion Subject: Re: Data Table Sorting still not working 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 (Im assuming Updates is your arraylist/datamodel since you have value ="#{TD.updates}" ? Note Im not sure if this is needed or not you are ahead of where I am with regards to getting this working Im still working on understanding how to implement this; The reason Im 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/[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, Im still deciphering it for example, sort(getsortt(), getascending()) Im 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 t
Re: Data Table Sorting still not working
Hi Simon, This is great info, thanks. You should write a "how to" book on MyFaces (we could use it! s). I seem to have things set up the way you're describing, so I'm wondering if my faces-config is missing some listener or something since clicking the header doesn't actually fire the setSortColumn() method in my managed bean (the getSortColumn() does fire, but that's part of the normal startup). I'm going to try and get the source example working and I'll report back later on. Thanks again Mike Simon Kitching wrote: 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.z} 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
RE: Data Table Sorting still not working
Mike are you calling the sort method from within your getUpdates getter method in your bean (Im assuming Updates is your arraylist/datamodel since you have value =#{TD.updates} ? Note Im not sure if this is needed or not you are ahead of where I am with regards to getting this working Im still working on understanding how to implement this; The reason Im 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/[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, Im still deciphering it for example, sort(getsortt(), getascending()) Im 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] wrote: Mike [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,standardTable_Column,standardTable_ColumnCentered,standardTable_ColumnCentered,standardTable_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 ;))
Re: Data Table Sorting still not working
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 (Im assuming Updates is your arraylist/datamodel since you have value ="#{TD.updates}" ? Note Im not sure if this is needed or not you are ahead of where I am with regards to getting this working Im still working on understanding how to implement this; The reason Im 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/[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, Im still deciphering it for example, sort(getsortt(), getascending()) Im 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] wrote: Mike [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:
RE: Data Table Sorting still not working
Mike do you know how to access the source code for the java components for the examples (i.e., the source code for the models/beans/controller components)? When I unzip the source code for the examples, I get the .jsp page source, but only the .class (compiled) source for the java components Id like to take a look at these source code files as well to see how they are working (i.e., how they have coded the sort method.) Ill keep you posted as to what I find out / or if I can get this to work sounds like we are both trying to get the same functionality working. Thanks. From: Mike [mailto:[EMAIL PROTECTED] Sent: Friday, December 30, 2005 6:03 PM To: MyFaces Discussion Subject: Re: Data Table Sorting still not working 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 (Im assuming Updates is your arraylist/datamodel since you have value =#{TD.updates} ? Note Im not sure if this is needed or not you are ahead of where I am with regards to getting this working Im still working on understanding how to implement this; The reason Im 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/[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, Im still deciphering it for example, sort(getsortt(), getascending()) Im 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
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.z} 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.
RE: Data Table Sorting still not working
Mike, Tom:FirstMike, maybe it was a typo, but you said you had "Persons". Hope you meant "persons" (lower case).Here's a complete example which works fine for me. I'll try to explain what i understand what needs to be done. But first the code:My backing bean (or the bean that's "behind" the current jsp) is "SearchBean". It has a list of contacts" which I am trying to display in a datatable. This is the list that i want sorted.public class SearchBean extends AbstractViewController implements Serializable {private Contact contact = new Contact();private List matchingContacts = new ArrayList();private transient DataModel matchingContactsModel = new ListDataModel();/* * Code below is added on for sorting. */private String _sort;private boolean _ascending;public SearchBean() {_sort = "lastname";_ascending = isDefaultAscending("lastname");}/* * This method is needed so consequtive clicks on header will sort * asecending/descending/ascending/descending etc..*/public SearchBean(String defaultSortColumn) {_sort = defaultSortColumn;_ascending = isDefaultAscending(defaultSortColumn);}protected boolean isDefaultAscending(String sortColumn) {return true;}protected void sort(final String column, final boolean ascending) {Comparator comparator = new Comparator() {public int compare(Object o1, Object o2) {Contact c1 = (Contact) o1;Contact c2 = (Contact) o2;if (column == null) {return 0;}if (column.equals("firstname")) {return ascending ? c1.getFirstName().compareTo(c2.getFirstName()) : c2.getFirstName().compareTo(c1.getFirstName());} else if (column.equals("lastname")) {return ascending ? c1.getLastName().compareTo(c2.getLastName()) : c2.getLastName().compareTo(c1.getLastName());} else if (column.equals("ssn")) {return ascending ? c1.getSocialSecurityNumber().compareTo(c2.getSocialSecurityNumber()) : c2.getSocialSecurityNumber().compareTo(c1.getSocialSecurityNumber());} else if (column.equals("callbacknumber")) {return ascending ? c1.getCallBackNumber().compareTo(c2.getCallBackNumber()) : c2.getCallBackNumber().compareTo(c1.getCallBackNumber());} else if (column.equals("dateofbirth")) {return ascending ? c1.getDob().compareTo(c2.getDob()) : c2.getDob().compareTo(c1.getDob());}elsereturn 0;}};Collections.sort(matchingContacts, comparator);}public DataModel getMatchingContacts() {if ((matchingContactsModel == null)|| (matchingContactsModel.getRowCount() = 0)) {matchingContactsModel = new ListDataModel();matchingContactsModel.setWrappedData(matchingContacts);}sort(_sort, _ascending);return matchingContactsModel;}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);}public String getSort() {return _sort;}public void setSort(String sort) {_sort = sort;}public boolean isAscending() {return _ascending;}public void setAscending(boolean ascending) {_ascending = ascending;}}Finally in my jsp:h:form id="contactscrollerpagerform"t:saveState id="savesearchstate" value="#{search}" /h:panelGrid styleClass="scrollerTableNoBorder"h:columnt:dataTable id="contactsearchdata" var="contact"value="#{search.matchingContacts}" rows="7"rowId="#{contact.id}" sortColumn="#{search.sort}"sortAscending="#{search.ascending}" preserveSort="true"h:columnf:facet name="header"t:commandSortHeader columnName="lastname" arrow="true"immediate="false"h:outputText value="Last Name" //t:commandSortHeader/f:faceth:outputText value="#{contact.lastName}" //h:column h:columnf:facet name="header"t:commandSortHeader columnName="firstname" arrow="true"immediate="false"h:outputText value="First Name" //t:commandSortHeader/f:faceth:outputText value="#{contact.firstName}" //h:column etc..I quote below what Simon wrote and point out how I that is realised in my code:"The "sortColumn" attribute should point to a backing bean property that is of type String. That property gets set (ie its setter gets called) with the columnName value of whatever column the user chose to sort on."So in above example, I have sortColumn="#{search.sort}". So my bean has a String called "_sort", and a "getSort()" and a "setSort(String)"."The "sortAscending" attribute should point to a backing bean property that is of type boolean. That property gets set to true/false when the user clicks repeatedly on the same column header (ie sorts asc/desc/asc/desc)."In my example, I have sortAscending="#{search.ascending}". So my beanhas a getAscending() and setAscending(boolean)"Your worklist.assignments method (ie the one referred to by the table's "value" attribute) is then required to look at the backing bean's properties that are the target of sortColumn and sortAscending and return its list in the order specified by those (String, boolean)"properties.In my example, I have