DataTable: Plain sort doesn't work (require filter)

UI Components for JSF
ekh
Posts: 15
Joined: 06 Sep 2011, 09:06

06 Sep 2011, 12:16

Dear All,

Since last weekends, I have been trying to fix this myself, however, I couldn't..

ISSUE
I am trying to get the example in the showcase work but It doesn't. In Firebug, I can see Ajax calls get fired. In the console, I can see the sort method actually is being called. But the table remains as is.

I tried the built-in sort with String, and the custom one with Double. Neither works.

I tested it against the two releases, 2.2.1 and 3.0.M3.

Any help or insight is appreciated :ugeek:

SNIPPETS

Data

Code: Select all

public List<Monthly> getMonthly() {
	return ejbFacade.findAll();
}
Table' Signature

Code: Select all

<p:dataTable var="data" value="#{monthlyController.monthly}">
Column' Signature

Code: Select all

<p:column sortBy="#{data.volume}" sortFunction="#{monthlyController.sortByVolume}">
	<f:facet name="header">
		<h:outputText value="Volume" />
	</f:facet>
	<h:outputText value="#{data.volume}" />
</p:column>
Server-side sort function

Code: Select all

public int sortByVolume(Object a, Object b) {
	Double x = (Double) a, y = (Double) b; // Casting
	System.err.println("a: "+x+", b: "+y+", " + (x < y)); // Debuging
	return x < y ? -1 : x == y ? 0 : 1;
}
Ajax's Response Body

Code: Select all

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_idt7:j_idt15"><![CDATA[<tbody id="j_idt7:j_idt15_data" class="ui-datatable-data ui-widget-content"><tr id="j_idt7:j_idt15_r_0" class="ui-widget-content ui-datatable-even"><td><div class="ui-dt-c">OMCCR_IP21</div></td><td><div class="ui-dt-c">TK503.NV</div></td><td><div class="ui-dt-c">0.0</div></td><td><div class="ui-dt-c">0.0</div></td></tr><tr id="j_idt7:j_idt15_r_1" class="ui-widget-content ui-datatable-odd"><td><div class="ui-dt-c">OMCCR_IP21</div></td><td><div class="ui-dt-c">TK821.NV</div></td><td><div class="ui-dt-c">848911.93</div></td><td><div class="ui-dt-c">-845051.6567</div></td></tr><tr id="j_idt7:j_idt15_r_2" class="ui-widget-content ui-datatable-even"><td><div class="ui-dt-c">OMCCR_IP21</div></td><td><div class="ui-dt-c">TK600.NV</div></td><td><div class="ui-dt-c">3420152.496</div></td><td><div class="ui-dt-c">-3874908.887</div></td></tbody>]]></update><update id="javax.faces.ViewState"><![CDATA[-1203497821118012635:-7198452462682231682]]></update></changes></partial-response>
Last edited by ekh on 06 Sep 2011, 13:03, edited 1 time in total.
Work Environment
Win7, Java[1.6.0.13], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.0]
Home Environment
iMac Lion, Java[1.6.0.26], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.1]

ekh
Posts: 15
Joined: 06 Sep 2011, 09:06

06 Sep 2011, 12:57

Accidently, as I was browsing the forum I came a cross the post dataTable: sort fails until filter used then ok and gave it a thought. Then I added a filter to my facelet:

Code: Select all

<p:column sortBy="#{data.tank}" filterBy="#{data.tank}">
	<f:facet name="header">
		<h:outputText value="Tank" />
	</f:facet>
	<h:outputText value="#{data.tank}" />
</p:column>
And amazingly, after applying the filter to the "Tank" column, the sorting actually works! (Both the built-in and customized ones)

However, after removing the filter by deleting whatever in the editbox, the sort stops working.

Hope this helps.
Work Environment
Win7, Java[1.6.0.13], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.0]
Home Environment
iMac Lion, Java[1.6.0.26], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.1]


ekh
Posts: 15
Joined: 06 Sep 2011, 09:06

06 Sep 2011, 17:38

Hi,

I guess I figured out the reason. The example itself in the showcase works because getCarsSmall() return a static/fix List of Car.

However, if the "value" of the dataTable actually is a method that returns a List from a container or entitymanager, then the value keeps refreshing.

What I mean is, in my example, i have "return ejbFacade.findAll()" which will always return the same List.

However, when we apply a filter, somehow (i guess) primefaces create a temporary variable that holds the filter data, hence it can be sorted.

To make the story short, <p:dataTable var="somevar" value="#{SOME_FIXED_LIST}"> in order for the sort to work.

If my conclusion is valid, I highly recommend the guide/showcase to be updated to include this tip/note.

/ekh
Last edited by ekh on 06 Sep 2011, 23:00, edited 1 time in total.
Work Environment
Win7, Java[1.6.0.13], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.0]
Home Environment
iMac Lion, Java[1.6.0.26], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.1]

sebastianovide
Posts: 90
Joined: 19 Dec 2010, 17:08

06 Sep 2011, 18:15

ekh wrote:To make the story short, <p:dataTable var="somevar" value="#{SOME_FIXED_LIST"> in order for the sort to work.
if that is the case, that would involve a lot of extra coding.... every time that we need to use a table need to create a list from the entity (or in your case EJB_facade)... and in some how refresh that list when needed (some sort of hand made cache !)... :(

ekh
Posts: 15
Joined: 06 Sep 2011, 09:06

06 Sep 2011, 23:05

I believe this is the nature of the function. My conclusion is based on the following:
  • Sort works on a fixed/static list without filtering it
  • For a dynamic list, filter is required for the sort to work.
  • An invalid filter or a filter that doesn't reduce the original list will not enable the sort
Hence, the current sortBy does what it is supposed to do.

To enhance this feature even further, sortBy could cache the data just like how filterBy caches it.

/ekh
Work Environment
Win7, Java[1.6.0.13], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.0]
Home Environment
iMac Lion, Java[1.6.0.26], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.1]

webel
Posts: 87
Joined: 18 Sep 2010, 09:29
Location: Sydney, Australia
Contact:

09 Sep 2011, 04:30

I am the author of similar reports at dataTable: sort fails until filter used then ok, and would like to weigh in on this.

There can be few more important features in a JSF framework that the ability to display, filter, and sort data fetched efficiently from database.

The pattern for getting/fetching that I have been using, namely only fetching the data from database once on a page load (using an f:event preRenderView to reset a backing bean list variable to null, and then a getter that tests for this null then fetches the data only once) has proved very effective in coping with multiple JSF getter calls during different phases. This solution has been used by many people to copy with this aspect of JSF. It is not efficient to pull the same data from database multiple times during a single page load, and the strategy I use addresses it well.

Another more complicated strategy is to employ PhaseListeners.

In any case, there is a crucial need for JSF dataTable solutions that play nice with these 2 widely used solutions described above.

JSF toolkits are not used in a vacuum, although frequently (and understandably) examples employ only self-sufficient backing bean variables. But that is rarely how JSF is used. Mostly it is used in combination with Enterprise Java and JPA.

Therefore addressing this matter, even if it does require a lot of coding by the Primefaces team, is well indicated, and would be much appreciated. End users should not have to perform a lot of extra coding just to handle their live fetched data in a dataTable properly.

Webel
Primefaces 6.1
JSF Mojarra 2.3.0
(Netbeans 8.2+Glassfish 4.1.1 OR Payara 4.1)
Mac OS X "Yosemite" 10.10.5 / Linux CentOS 6.7

webel
Posts: 87
Joined: 18 Sep 2010, 09:29
Location: Sydney, Australia
Contact:

09 Sep 2011, 04:36

However, after removing the filter by deleting whatever in the editbox, the sort stops working.
I would just like to confirm this; elsewhere I had previously reported that I was only having this problem with the sorting not working until a character was entered into the filter field (and a filtering had happened), but in fact without a filter the sorting is not working for me at all.
Primefaces 6.1
JSF Mojarra 2.3.0
(Netbeans 8.2+Glassfish 4.1.1 OR Payara 4.1)
Mac OS X "Yosemite" 10.10.5 / Linux CentOS 6.7

sebastianovide
Posts: 90
Joined: 19 Dec 2010, 17:08

09 Sep 2011, 10:29

webel wrote:
However, after removing the filter by deleting whatever in the editbox, the sort stops working.
I would just like to confirm this; elsewhere I had previously reported that I was only having this problem with the sorting not working until a character was entered into the filter field (and a filtering had happened), but in fact without a filter the sorting is not working for me at all.
I think that primefaces team should choose between:
1) disable the sorting
2) fixing the sorting

ekh
Posts: 15
Joined: 06 Sep 2011, 09:06

09 Sep 2011, 11:38

sebastianovide wrote: I think that primefaces team should choose between:
1) disable the sorting
2) fixing the sorting
I don't believe the sorting is borken. Technically, it is doing exactly how it was implemented for. In our examples, our table' source is a method that fetches the list from the container everytime the page loads. And by sorting, you don't actually sort the entities in the container, hence, it fires the same list for every call.

By knowing the nature of the current implementation of sort, a workaround works perfectly (see webel's post) but it is an extra step that could be spared if primefaces team decided to enhance the current implementation.

Also, the option to disable the sorting is something i cannot live with :geek:

/ekh
Work Environment
Win7, Java[1.6.0.13], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.0]
Home Environment
iMac Lion, Java[1.6.0.26], GF Server[3.1], JSF[2.0], EclipseLink[JPA 2.0], NetBean[7.1]

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 41 guests