Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic Calculation Using Filter in Datatable  RSS feed

 
marcel vieira
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.

I have a (*)filterOption and a (**) single summaryRow for all the results of a column displayed in my dataTable.

When the user applies the filter in the dataTable (by filterOption or searchField), I want the summaryRow to be dynamically calculated (show the sum for the corresponding displayed data only).

How can I do it?

For better understanding, please see Primefaces examples:
(*) http://www.primefaces.org/showcase/ui/datatableFiltering.jsf
(**) http://www.primefaces.org/showcase/ui/datatableSummaryRow.jsf

See below my main code for this issue:

The jsf:



The method soma in the managed bean/controller:




Thanks in advance.
 
K. Tsang
Bartender
Posts: 3630
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you triggering the getSoma() method in the filter? You should able to add an update attribute or ajax tag to the filter to update the soma value.
 
marcel vieira
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:How are you triggering the getSoma() method in the filter? You should able to add an update attribute or ajax tag to the filter to update the soma value.


Ok, I thought about it, but I'm not sure if just triggering the getSoma() method (no matter how, by update or ajax etc) will calculate only the filtered dataSet, since the enhaced for loop in the method runs over all the data in the database table.
What do you think?

Thanks.
 
K. Tsang
Bartender
Posts: 3630
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is the case, your getItems() list will also need to get updated.

Instead of re-populating from db, maybe you use the List.contains() or similar way to get the filtered records.
 
marcel vieira
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:If that is the case, your getItems() list will also need to get updated.

Instead of re-populating from db, maybe you use the List.contains() or similar way to get the filtered records.


This sounds nice. So I need to write a new method in the managed bean, which capture the filtered dataSet, process the calculation for those values and return the result? And this method will be invoked every time a filter event occurs?

Can you help me build this? Or do you have any link that could help on it?

Thanks.
 
K. Tsang
Bartender
Posts: 3630
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primeface showcase should give you a clue. They have a filteredCars and and smallCars (all cars) so that's 2 lists. Also check on the p:column tag "filterMatchMode" and the p:datatable tag "filteredValue" attributes.
 
marcel vieira
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:The primeface showcase should give you a clue. They have a filteredCars and and smallCars (all cars) so that's 2 lists. Also check on the p:column tag "filterMatchMode" and the p:datatable tag "filteredValue" attributes.


Is this the link: http://www.primefaces.org/showcase/ui/datatableFiltering.jsf ? Sorry, but It's not clear for me yet. What would be the first thing you should do?
I found a very good example and this is exactly what I want to do in Primefaces: http://datatables.net/examples/advanced_init/footer_callback.html

What do you think?

Thanks.
 
K. Tsang
Bartender
Posts: 3630
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that's the link. In it you can compare the p:table tag line with yours and see what attributes are missing. From my understanding the "filteredValue" attribute only shows the filtered or searched records allowing you to sum only these rows. Then check the p:column line against yours.

In the example link you provided, the sum is clearly per page. If I use the search, it's the filtered rows.

The idea is the same.
 
marcel vieira
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Yes that's the link. In it you can compare the p:table tag line with yours and see what attributes are missing. From my understanding the "filteredValue" attribute only shows the filtered or searched records allowing you to sum only these rows. Then check the p:column line against yours.

In the example link you provided, the sum is clearly per page. If I use the search, it's the filtered rows.

The idea is the same.


Ok, so in the example there is 2 instance variables (filteredCars e carsSmall) and both are a Cars' list. The populateRandomCars(line 55) populate the carsSmall list through the constructor call TableBean (line 48). Well, I created an instance variable too, with getters and setters, so it could roll over the dataTable filtered values and do the calculation with them, but I'm geting a NullPointerException because the list have not been automatically loaded. How can I load my list with only the filtered values? How the filteredCars list is 'automatically' populated? Thanks.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitpick time: A JSF Managed Bean (backing bean) is not a Controller. You don't write Controllers in JSF. They're presupplied in the FacesServlet and in the tags. Backing beans are Models with optional business logic in them. A Controller is specialized code whose sole purpose is to sync the Model and View and your backing bean shouldn't be doing that, since, as mentioned, JSF handles that task.

Beyond that, what I do is subclass my DataModel class to add extra properties for the summary lines, update those properties when the DataModel changes, and AJAX-refresh the View so that JSF's Controllers will update the display.

The main difference is that I'm doing this when the basic data in the Model changes, not based on what data I'm displaying, but that part is just a variant on the same technique.

 
marcel vieira
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Nitpick time: A JSF Managed Bean (backing bean) is not a Controller. You don't write Controllers in JSF. They're presupplied in the FacesServlet and in the tags. Backing beans are Models with optional business logic in them. A Controller is specialized code whose sole purpose is to sync the Model and View and your backing bean shouldn't be doing that, since, as mentioned, JSF handles that task.

Beyond that, what I do is subclass my DataModel class to add extra properties for the summary lines, update those properties when the DataModel changes, and AJAX-refresh the View so that JSF's Controllers will update the display.

The main difference is that I'm doing this when the basic data in the Model changes, not based on what data I'm displaying, but that part is just a variant on the same technique.


Thank you so much for sharing your knowledge. I´ll never forget this nitpick . Now I'll research on how can I do this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!