"p: datatable" selection - jsf

I am using a p:datatable and selection to handle in my bean the selected record. This works. But I want to preset a selected row when initializing the datatable. Is this possible? How?
Now it doesn't preset a record, even though selectedCompany is set. Is it because I do it before the return to jsf has been made?
This is the code I have
Bean
public Map<String, Object> getSelectedCompany() {
return selectedCompany;
}
public void setSelectedCompany(Map<String, Object> selectedCompany) {
this.selectedCompany = selectedCompany;
}
public MWSGenericMapList getHandlingCompanies() {
if (companyItems == null) {
companyItems = retrieveHandlingCompanyItems(partnerIds);
for (int i = 0; i < companyItems.size(); i++) {
if (companyItems.get(i).get("businesspartnerid").equals(getnewCustomerPartnerId())) {
setSelectedCompany(companyItems.get(i));
}
}
}
return companyItems;
}
Jsf
<p:dataTable styleClass="ptable100" id="handlingCompanies" var="company" value="#{switchCompany.handlingCompanies}" width="100%" height="200"
emptyMessage="#{msg.all_lists_no_records_found}" paginator="true" rows="10" rowsPerPageTemplate="5,10,20,50,100"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} #{msg.all_lists_numberOfRowsDisplayed_label} {RowsPerPageDropdown}"
selection="#{switchCompany.selectedCompany}" selectionMode="single" rowKey="#{company.businesspartnerid}">
<p:ajax event="rowSelect" update="#form" listener="#{switchCompany.handleCompanyChange}"/>

nevermind, the selected row did get set, I was just not on the first page (too many rows to display on 1 page) so I missed it. But it works the way I did it.

Related

rowsPerPageTemplate Dropdown dissapears after selection change and clicking away

I have Datatables(PrimeFaces 8.0.1) on multiple pages, only one of them had rows and rowsPerPageTemplate attributes, now i need to modify all other DataTables in the same way. All DataTables (dataTableA,dataTableB,..) are almost identical and have own #SessionScoped Beans(ControllerA, ControllerB,..). At first all DataTables seem to be OK, but if i change the rows per page selection of a dataTableX and click away to another page/dataTable and then i turn back to dataTableX, rows per page dropdown isn't there anymore (for the rest of the session). Can anyone help please? Thank you.
listViewA.xhtml
<h:form id="listAUserForm">
<p:dataTable id="dataTableA"
value="#{controllerA.listA}"
reflow="true"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} #{msg.rows_per_page}: {RowsPerPageDropdown}"
currentPageReportTemplate="#{prop.current_page_report_template}"
paginatorAlwaysVisible="false"
var="entryA"
rows="#{controllerA.rowsPerPage}"
rowsPerPageTemplate="10,20,30,{ShowAll|'All'}"
rowKey="#{entryA}"
selection="#{controllerA.entriesSelected}">
<p:ajax event="rowSelectCheckbox" listener="#{controllerA.onRowSelect}" update=":toolbarA" />
<p:ajax event="rowUnselectCheckbox" listener="#{controllerA.onRowUnselect}" update=":toolbarA" />
<p:ajax event="toggleSelect" process="#this" partialSubmit="true" update=":toolbarA"/>
<p:column selectionMode="multiple" style="width:16px;text-align:center"/>
....
....
</h:form>
ControllerA.java
#ManagedBean(name = "ControllerA")
#SessionScoped
public class ControllerA extends BaseController
{
private int rowsPerPage = 10;
private List<EntryA> entries= null;
public int getRowsPerPage()
{
return rowsPerPage;
}
public void setRowsPerPage(int rowsPerPage)
{
this.rowsPerPage = rowsPerPage;
}
public List<EntryA> getListA()
{
if (entries== null)
{
entries= new ArrayList<>();
List<EntryA> tmpListA;
try
{
tmpListA= getSecurityServiceLocator().getBeanA().getAllEntries();
}
catch (SecurityException ex)
{
LOGGER.error("Error retrieving listA", ex);
return Collections.emptyList();
}
if (!isFilterExist())
{
entries.addAll(tmpListA);
return entries;
}
for (EntryA tmpEntryA : tmpListA)
{
if (tmpEntryA.filterMatch(filterValue.toLowerCase()))
{
entries.add(tmpEntryA);
}
}
}
return entries;
}
...

Unable to Select Table Rows in PrimeFaces Using RadioButtons

I am trying to develop a web application using a PrimeFaces data table. I am having difficulty getting the object that corresponds to the selected row. Whenever I do that, a null value gets selected. Here is the relevant code:
<p:dataTable id="presTable" var="pres" value="#{presBean.presentations}"
rows="10" scrollable="true" scrollWidth="85%"
selection="#{presBean.selectedPres}" rowKey="#{pres.id}" paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,25,100" widgetVar="presTable" filteredValue="#{presBean.filteredPres}" selectionMode="single">
</p:column>
<f:facet name="footer">
<p:commandButton id="updateButton" process="presTable" value="Update" action="#{presBean.printSelectedPres()}"/>
</f:facet>
</p:dataTable>
Java backing bean:
public Presentation getSelectedPres() {
System.out.println("Returning selected presentation: " + selectedPres);
return selectedPres;
}
public void setSelectedPres(Presentation selectedPres) {
System.out.println("Setting selected presentation to " + selectedPres);
this.selectedPres = selectedPres;
}
public void printSelectedPres() {
System.out.println("Printing list of checked presentations:");
System.out.println(selectedPres);
}
Add a column with seletionMode=single
<p:column selectionMode="single" style="width:16px;text-align:center"/>
you can find sample for radio button here : https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml

Primefaces slected row id == null

I have a working datatable that can list out restaurant objects.
I want to delete/edite the selected ones but when I select one the following exception shows up:
org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!
Here is the table:
<h:form id="restaurantForm">
<p:dataTable var="restaurant"
value="#{restaurantLazyBean.lazyDataModel}" paginator="true"
rows="10" rowsPerPageTemplate="5,10,50" id="carTable" lazy="true"
selectionMode="single" selection="#{RestaurantEditBean.selected}"
rowKey="#{restaurant.id}"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}">
<p:ajax event="rowSelect"
listener="#{RestaurantEditBean.onRowSelect()}"/>
<p:column headerText="ID">
<h:outputText value="#{restaurant.id}" />
</p:column>
</p:dataTable>
</h:form>
Now all ids appear in the table but on selection the exception shows up.
I tried to do everything according to the primefaces example. But they didn't even have the rowKey attribute.
Heres the bean if thats relevant.
#Named("RestaurantEditBean")
#ViewScoped
#EJB(name = "ejb.RestaurantService", beanInterface = RestaurantService.class)
public class RestaurantEditBean {
#EJB
private RestaurantService restaurantService;
private RestaurantDTO selected;
public void onRowSelect(SelectEvent event) {
selected = ((RestaurantDTO) event.getObject());
}
public RestaurantService getRestaurantService() {
return restaurantService;
}
public void setRestaurantService(RestaurantService restaurantService) {
this.restaurantService = restaurantService;
}
public RestaurantDTO getSelected() {
return selected;
}
public void setSelected(RestaurantDTO selected) {
this.selected = selected;
}
}
Primefaces: 5.3
JSF: 2.2
I found out that I did a terrible mistake.
In my LazyDataModel I had to override a function.
#Override
public RestaurantDTO getRowData(String rowKey) {
Long id = Long.parseLong(rowKey);
return restaurantService.findById(id);
}
The issue was cause by the previous Long.getLong(rowKey) and that one returned null.

p:dataExporter not exporting just the displayed page

I'm doing project in PrimeFaces. I want export datatable data using dataexporter. My problem is using a dataexporter attribute to export the current displayed page content.
I try this below code:
<p:dataTable id="tbl" var="car" value="#{dataExporterView.employee}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink}
{PageLinks} {NextPageLink} {LastPageLink} {Exporters}"
paginator="true" rows="10" style="margin-bottom:20px">
............................
<h:commandLink>
<p:graphicImage name="/demo/images/xml.png" />
<p:dataExporter type="pdf" target="tbl" fileName="employee" pageOnly="true" />
</h:commandLink>
</p:dataTable>
But even though I added pageOnly="true", it will export all pages but I want export particular displayed page only.
You cannot export particular pages of a p:dataTable, but you can add a pre or post processor changing the exported content similar to
<p:dataExporter preProcessor="#{bean.preProcessExport}"
postProcessor="#{bean.postProcessExport}"/>
with
public void preProcessExport(Object document) {
if (document instanceof com.lowagie.text.Document) {
// ...
}
}
public void postProcessExport(Object document) {
if (document instanceof com.lowagie.text.Document) {
// ...
}
}
you should remove pageOnly="true" from your code then it export current page only.

Parameterized method in Datatable sometimes receives a null Parameter

I have the following datatable:
<p:dataTable id="persons" styleClass="hide-column-names personTable"
value="#{personListDisplayerBean.allPersonsModel}" var="person"
paginator="true" rows="25"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,25,50,100"
selection="#{personDetailsDisplayerBean.selectedPerson}"
filteredValue="#{personListDisplayerBean.filteredPersons}"
selectionMode="single" widgetVar="personTable">
...
...
<p:column styleClass="person-list-image-column">
<p:graphicImage styleClass="thumbnail"
value="#{personListDisplayerBean.getImageForPerson(person)}" />
</p:column>
...
...
</p:dataTable>
(Made it a little shorter)
Now to my problem: The Method getImageForPerson is invoked twice. The reason why is explained in this post here:
Display dynamic image from database with p:graphicImage and StreamedContent
The first time invoking everything is fine, but the second time it is invoke i only receive a null as Parameter...
The method itself looks something like this:
String fileName = person.getObjectGUID();
File f = new File(fileName + ".jpg");
FileInputStream inputStream;
try {
inputStream = new FileInputStream(f);
} catch (FileNotFoundException e) {
return new DefaultStreamedContent();
}
stream = new DefaultStreamedContent(inputStream,"image/jpg");
return stream;
Why does JSF give me a null as parameter the second time the Method is invoked? Can i overcome this problem somehow?
Thanks in advance :)

Resources