Parameterized method in Datatable sometimes receives a null Parameter - jsf

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 :)

Related

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

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.

Primefaces Datatable RowSelect Event

I have the following in my xhtml
<h:form id="clientTableForm" prependId="false">
<p:dataTable id="clientTable" widgetVar="clientTableVar"
var="client" value="#{resendEmailController.lazyDataModel}"
paginator="true" rows="15"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}"
rowsPerPageTemplate="5,10,15,20,25,50,75,100"
paginatorPosition="bottom" pageLinks="5" lazy="true"
sortBy="#{client.cclnCode}" sortOrder="ascending"
selection="#{resendEmailController.selectedClient}"
selectionMode="single" filterDelay="500" scrollable="true"
scrollHeight="380">
<p:ajax event="rowSelect"
listener="#{resendEmailController.changeClient}"
update="_accountTableForm_accountTable" />
<p:column id="cclnCodeColumn" headerText="Client Code"
style="width:25%;" sortBy="#{client.cclnCode}"
filterBy="#{client.cclnCode}" filterMaxLength="10">
<h:outputText value="#{client.cclnCode}"
converter="#{trimStringConverter}" />
</p:column>
<p:column id="cclnNamenColumn" headerText="Client Name"
style="width:75%" sortBy="#{client.cclnName}"
filterBy="#{client.cclnName}" filterMaxLength="50">
<h:outputText value="#{client.cclnName}"
converter="#{trimStringConverter}" />
</p:column>
</p:dataTable>
</h:form>
</p:layoutUnit>
<script type="text/javascript">
$(document).ready(function()
{
autoSelectClient();
});
function autoSelectClient()
{
if (clientTableVar.isEmpty() == false)
{
clientTableVar.selectRow(1, false);
}
}
</script>
And I have this in my backing bean
public void changeClient(SelectEvent selectEvent)
{
ResendEmailClient client = (ResendEmailClient) selectEvent.getObject();
selectedClient = client;
String cclnCode = client.getCclnCode();
selectedAccounts = getService().listAccounts(cclnCode);
}
I would just like to ask why the "selectedClient" variable in the backing bean is NULL when executed by the "autoSelectClient();". But if I clicked the rows the "selectedClient" is already set.
As you can see in my backing bean I can get the value I want by getting the object inside the SelectEvent but I just want to know what is the cause of the difference.
Also if possible can any also suggest how to replicate the emulate the second scenario so that the "selectedClient" is already set before the "changeClient()" is invoked.
Using
JSF 2.1
PrimeFaces 3.5
Mojarra 2.1
try to send your datatable id in process of your ajax selection event like this:
<p:ajax event="rowSelect" listener="#{resendEmailController.changeClient}" update="_accountTableForm_accountTable" process="clientTable" />
Because when you call your event the real situation of your selection has no sent.

"p: datatable" selection

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.

p:dataTable, lazy loading and global filter

I am using lazy loading ,paging and global filter in the dataTable
In the lazy load function I add 2 function in order to support 2 scenarios:
1) When the user is paging
2) When the user add a value in the global filter and press "Enter"
Basically what I need to know is what activity was execute in order to decide what function to use.
dataTable:
<p:dataTable id="osTable"
var="object"
value="#{bean.lazyModel}"
selection="#{bean.selectedObjectSet}"
selectionMode="single"
rowKey="#{object.ID}"
paginator="true"
paginatorPosition="bottom"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15"
rows="10"
widgetVar="objectTable">
<f:facet name="header">
<p:outputPanel style="float: right">
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter"
onkeypress="if (event.keyCode == 13) {objectSetTable.filter()}"
style="width:150px"/>
</p:outputPanel>
</f:facet>
load function:
#Override
public List<CfgSe2deMapping> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
List<Mapping> data = new ArrayList<Mapping>();
if (// if user press enter ) {
data.addAll(MappingHelper.getViaFilter(filters.get("globalFilter")));
} else {
data.addAll(MappingHelper.getViaOffSet(first + 1, first + pageSize));
}
I am using primefaces 3.2
Thanks
you are thinking this the wrong way. no matter what the user did, you should always do the same: retrieve data (applying filters, if any) and paginate (return pageSize rows starting from first.
why aren't you including the 'first' and 'pageSize' parameters when there's a filter? rows have to be paginated either way. otherwise the user would be able to see only the first page of filtered results.
Also, you have to override getRowCount method, so that your table shows the right number of pages.

Resources