Where do the values in a p:datatable for the currentPageReportTemplate come from? - jsf

I am trying to find out how the currentPageReportTemplate attribute of the dataTable tag and the resolving of its values works. I found e.g. this:
<p:dataTable currentPageReportTemplate="{startRecord} - {endRecord} von {totalRecords}">
There are some keywords which primefaces seems to replace with values, but where does it get the values from and how do I know what keywords are available?
The contents of the dataTable I am given are backed with an extension of org.primefaces.model.LazyDataModel, which overrides the load method to provide the content for the table rows. But I cant manage to find out where the data for totalRecords of the currentPageReportTemplate comes from.
I did some research, but could not find any kind of documentation that satisfied me. I read e.g. the tag api documentation: https://www.primefaces.org/docs/vdl/3.5/primefaces-p/dataTable.html
But there is just one little sentence about the currentPageReportTemplate attribute:
Template of the currentPageReport UI.
I also found this page https://www.primefaces.org/showcase/ui/data/datatable/paginator.xhtml with an example, but no explanation on how the values (e.g. for totalRecords) are determined.
How does Primefaces determine the value for e.g. totalRecords in the dataTable?

Related

Primefaces dynamic datatable with json dataset of rows and columns [duplicate]

I'm in the process of rewriting a (very) big and old Richfaces application using Primefaces. I now have a situation where I get two lists, one with column names and one with objects that should be represented as rows.
The object looks like this:
public class AVeryGenericThingThatIDoNotLike {
Object[] values;
//Other parameters that I dont care about
}
When I should display this I want it to map values[0] to the first column, values[1] to the second and so forth.
I have looked at the primefaces showcase and there is an example of dynamic columns but I have tried that and I can not get it to work when I do not know what columns that can exist. The reason that I do not know this is that the columns are decided from a database that holds a row saying what columns that should exist and this can change, so I can not build a class to have all the columns available like in the showcase example.
Can anyone give me any pointers here? I've been stuck on this for a few days and I really need help.
EDIT: Now it is kinda working with the solution from Laabidi Raissi. My problem now is that for some columns I want to have buttons being rendered and command links. With this approach I am only able to get strings and if I try to return objects like CommandButtons it will just print a java object hash.
If you are using JSF2, then I think this solution may work for you. First, in your managed bean, you build your list of columns (dynamically) from DB, and then:
<p:columns value="#{yourManagedBean.listOfColumnsFromDB}" var="column" columnIndexVar="colIndex">
<f:facet name="header"> #{column}</f:facet>
<h:outputText value="#{yourManagedBean.values[colIndex]}" />
</p:columns>
EDIT: Base on your comment, I think adding commandbutton or outputlink is very obvious (instead of the outputText):
<h:commandButton value="#{yourManagedBean.values[colIndex]}" action="#{yourManagedBean.someActionThatTakesAParam(colIndex)}" rendered="#{colIndex == 4}"/>
Supposing you want commandButton in column 5.
Is that what you are looking for ?
Hope this helps

Pagination not working in jsf rich:datascroller

how to make work rich:datascoller with nested components.
Data table iteration works fine but pagination is not working.
does rich:datascroller works with nested component?
<a4j:repeat >
<rich:datatable id="test">
......
</rich:datatable>
<rich:datascroller for="test"></rich:datascroller>
</a4j:repeat >
any help will be greatly appreciated
Firstly please answer the my question asked in comment section.
About your issue i do not know why you did not add at lease full code of Datatable .
If you read the documentation of the component(Rich_Datascroller) its mentioned below thing
The component should be placed into the footer of the parent table or be bound to it with the "for" attribute. Note, that "for" is
evaluated on view build, not on view render, that is why it will
ignore JSTL tags.
The table should also have the defined "rows" attribute limiting the quantity of inputted table rows.
The scroller could limit the maximum quantity of rendered links on the table pages with the help of the "maxPages" attribute.
So according to above points its look like you missed
The table should also have the defined "rows" attribute limiting the
quantity of inputted table rows.
As per documentation its a mandatory. point to remember . Apart from your issue its good practice before asking in stackoverflow create a simple example and try it out of the box.
About my question I am more interested to know how your code will be work without generating dynamic id as per your code your component id is Static . It should throw the exception because <a4j:repeat> nothing but a for loop.

f:facet - there is a list of predefined names attributes?

I have searched across the web with no luck, I know there are some predefined names, such as 'header' and 'footer' associated with the dataTable, also 'first' inside the head tag to run first this metatag.
There are other predefined names I should be aware of?
Thanks,
-M
You cannot find a predefined names list for facets. because facets are related to components. Every components supports different facets. e.g. h:datatable supports header and footer. primefaces datatable supports header, footer and emptyMessage facets. best way to find supportted facets you should look source code of component renderers. Primefaces DatatableRenderer, Mojarra TableRenderer

Can't update component in p:dataTable (not whole p:dataTable) in Primefaces

I'm using Primefaces 3.3.1 with Tomcat 7.0.22.0.
I have p:dataTable which has p:inputText in it. The id of that p:dataTable is "houseTabID:tabView:form0:table". This is copy-pasted from HTML source. And I let dialog (which is outside of the form dataTable resides) open by button in p:dataTable, and p:commandButton in that dialog updates the dataTable.
p:commandButton looks like this;
<p:commandButton ajax="true" action="#{myBean.setInputText()}"
value="OK"
update="#{myBean.getUpdateTarget(0)}"/>
myBean.getUpdateTarget(0) retruns proper string to point target component.
I can successfully update whole dataTable by specifying "houseTabID:tabView:form0:table" (meaning myBean.getUpdateTarget(0) returns that string). But I have many lines in the table so updating whole table takes longer to finish, and scroll position is reset which is really irritating. That makes me want to update only one row in the table, not whole.
So I first returned "houseTabID:tabView:form0:table:inputBox" to update p:inputText that I want to update. In p:dataTable I have something looks like this;
<p:column headerText="Value"
width="300" style="height:16px; font-size:9pt;">
<p:inputText id="inputBox" value="#{myItem.value}"
style="width:95%; height:11px; font-size:9pt;">
<f:ajax execute="#this" event="blur" />
</p:inputText>
</p:column>
Result: no update, no error log. I know "houseTabID:tabView:form0:table:inputBox" won't work since it does not specify row in table. So by using rowIndexVar, I tried with "houseTabID:tabView:form0:table:0:inputBox" with making :0: part from rowIndexVar. The string is copy-pasted from HTML source. But unfortunatelly, I got exception.
javax.faces.FacesException: Cannot find component with identifier ":houseTabID:tabView:form0:table:0:inputBox" referenced from "houseTabID:j_idt181:j_idt186".
Why? There is obviously "houseTabID:tabView:form0:table:0:inputBox", I see it on HTML source, and I can update whole talbe with "houseTabID:tabView:form0:table" but throw exception with "houseTabID:tabView:form0:table:0:inputBox"? It does not make sense to me. Also, then, why it won't throw any exception or spill out error log with "houseTabID:tabView:form0:table:inputBox"? I verified HTML source of that p:commandButton;
onclick="PrimeFaces.ab({source:'houseTabID:j_idt190:j_idt195',update:'houseTabID:tabView:form0:table:inputBox',
I desparately want to update just one row instead of whole p:dataTable. Please help me.
I found a solution. According to this conversation,
position = JQuery(".ui-datatable-scrollable-body").scrollTop();
JQuery(".ui-datatable-scrollable-body").scrollTop(position);
can do the trick. It seems this is the only way to get and set vertical scroll position of p:dataTable. I saved position before opening dialog, and set position after updating p:dataTable from dialog. It worked and this is what I wanted to do.
But above method has serious issue. That code scans given class ".ui-datatable-scrollable-body" from top and perform at the first found one. So it works only for one p:dataTable. Fortunately I could come up with 2nd solution.
<script type="text/javascript">
$ = jQuery;
var scrollPos;
function saveScrollPos()
{
scrollPos = $("#houseTabID\\:tabView\\:form0\\:table_data").parents(".ui-datatable-scrollable-body").scrollTop();
}
function getScrollPos()
{
$("#houseTabID\\:tabView\\:form0\\:table_data").parents(".ui-datatable-scrollable-body").scrollTop(scrollPos);
}
</script>
FireBug in FireFox helped me find out there is componnet id which "_data" is appedded by Primefaces. Please see my question, my data table's id is "table" and someone created "table_data". And I could point it with escaped syntax shown above and function parents() leads to that spot. It is working fine in my applictaion.
I really thank stier01 who posted his solution which is the only one I could find over the net. And I really feel sad that Primefaces p:dataTable does not support such a basic thing. I know many of us wanted to keep scroll position of p:dataTable when updating it. I hope it will be improved in near future.
I am teetering on the edge of what I know here but I suspect that it may be happening because of the timing of the page construction. When the DOM tree is built, your table rows do not yet exist so your button outside of the table cannot find the reference to the row (because it does not yet exist). After the tree is built and the data is filled in, the row is then there. That is why after the page renders you can look up the column id, but not reference it from outside of the table. I am not sure of a solution. Maybe you can put a remote command in the datatable tags to update it. Maybe can do something in JS.

Sorting panelgrid JSF

Can we sort values in column of a panelGrid. If yes, can I see an example
Rather use h:dataTable to represent a dynamic table which is backed by a List<RowObject>, wherein RowObject is basically just a Javabean (DTO - Data Transfer Obejct, VO - Value Object, whatever you call it :) ) representing a single row.
Such a List can be sorted using Collections#sort(). All you need to do is to implement a Comparator which takes care about the ordering. Then in the JSF page just have a bunch of buttons or links which calls Collections#sort() on the List. Those buttons/links just have to pass the sortfield along.
You can find here a basic example: http://balusc.blogspot.com/2006/06/using-datatables.html#SortingDatatable
No, you can't. <h:panelGrid> = <table>
If you want sorting, look at Richfaces or another component framework (Icefaces, primefaces, trinidad, tomahawk, etc). There are <x:dataTable> components there which allow sorting on their <x:colum>.

Resources