Primefaces p:fileDownload inside p:treeTable doesn't work? - jsf-2

I have such structure:
<p:treeTable value="#{cmpDocumentTree.root}" var="v" id="#{tableId}" selectionMode="single"
selection="#{cmpDocumentTree.selectedNode}">
<p:ajax event="expand" listener="#{cmpDocumentTree.onNodeExpand}" />
<p:ajax event="collapse" listener="#{cmpDocumentTree.onNodeCollapse}" />
<p:ajax event="select" listener="#{crudBean.edit(cmpDocumentTree.selectedNode.data)}"
update=":#{formDialogUpdate}" oncomplete="#{formDialog}.show()" />
<p:column headerText="Nazwa" sortBy="#{v.name}" style="min-width: 200px;">
<h:outputText value="#{v.name}" />
</p:column>
<p:column headerText="Pliki" width="300">
<ui:repeat var="_file1" value="#{v.files}">
<h:commandLink value="#{_file1.originalFilename}">
<p:fileDownload
value="#{fileDownloadController.getFile(_file1.originalFilename, _file1.storedFilename, _file1.contentType)}" />
</h:commandLink>
<br />
</ui:repeat>
</p:column>
</p:treeTable>
But - p:fileDownload doesn't work. The fileDownloadController.getFile(...) method is not fired at all.
When I put the same method inside p:dataTable, then it works perfectly.
EDIT:
v.files comes from ComDocument entity - it is relation one-to-many document-to-files.
#Entity
#Table(name = "com_documents", schema = "public")
#SequenceGenerator(name = "COM_DOCUMENTS_SEQ", sequenceName = "COM_DOCUMENTS_SEQ", allocationSize = 1)
public class ComDocument implements EntityInt, java.io.Serializable {
(...)
#OneToMany(fetch = FetchType.EAGER, mappedBy = "document", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ComDocumentFile> files;
public void setFiles(List<ComDocumentFile> files) {
this.files = files;
}
public List<ComDocumentFile> getFiles() {
return files;
}
Could you help me? Thanks.

I've never had any luck with the FileDownload working inside an ajax request. I have not bothered to dig into the source as it is never a show stopper (and it really doesn't make sense to use it as an ajax request--you're getting a complete file, not updating a page).
The simple solution here is to make sure that your command button has "ajax=false"
<p:commandLink value="#{_file1.originalFilename}" ajax="false">
<p:fileDownload
value="#{fileDownloadController.getFile(_file1.originalFilename, file1.storedFilename, _file1.contentType)}" />
</p:commandLink>
http://www.primefaces.org/showcase/ui/fileDownload.jsf
You'll notice all the examples have the ajax functionality disabled. If you're using h:commandButton for style I suggest using p:commandLink and styling it to be a button.
Best of luck, I'm pretty sure this will fix it. Also--make sure your "download objects" are final. You need to make sure that the data-source is referring to the same reference and not creating a new instance on each request.

Related

Show/Hide components in primefaces 5/JSF 2.2 [duplicate]

This question already has an answer here:
Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
(1 answer)
Closed 7 years ago.
I thought this would be easy but I just cannot get it. I have a drop down and based on selection, I should be able to show/Hide another drop down based on a boolean. By default the boolean is false and only when it is true should the second drop down be rendered:
My page:
<h:outputLabel value="Select Report: "/>
<p:selectOneMenu value="#{daily.reportname}" id="sector" style="width: 250px;">
<f:selectItem itemLabel="ALL" itemValue="ALL" />
<f:selectItems value="#{daily.reportType()}"/>
<p:ajax event="change" update="branchrender" listener="#{daily.selectedReport()}" />
</p:selectOneMenu>
<h:panelGroup id="branchrender" rendered="#{daily.showBranchDimension}">
<h:outputText value="Branch" />
<p:selectOneMenu value="#{accounts.branchs}" id="branch" style="width: 250px;">
<f:selectItem itemLabel="ALL" itemValue="ALL" />
<f:selectItems value="#{dimensions.branchCode()}"/>
</p:selectOneMenu>
</h:panelGroup>
My selected report Method:
public void selectedReport() {
if (reportname.startsWith("cust_")) {
custrepname = reportname;
useBranch = pr.getCustRepProperties(custrepname + ".bi").getProperty("bi.useBranchDim");
showBranchDimension = useBranch.equalsIgnoreCase("true");
System.out.println("HERE: " + showBranchDimension);
} else {
custrepname = null;
}
}
showBranchDimension is a boolean having getter and setter:
public boolean isShowBranchDimension() {
return showBranchDimension;
}
public void setShowBranchDimension(boolean showBranchDimension) {
this.showBranchDimension = showBranchDimension;
}
Am I missing something? The System.out.println prints true but the component is NOT rendered.
You have to make the rendered component a rendered free because it must be exist when you submit the ajax request to server add another container to branchrender and move the rendered id to it
<h:panelGroup id="branchrender">
<h:panelGroup rendered="#{daily.showBranchDimension}">
<h:outputText value="Branch" />
<p:selectOneMenu value="#{accounts.branchs}" id="branch" style="width: 250px;">
<f:selectItem itemLabel="ALL" itemValue="ALL" />
<f:selectItems value="#{dimensions.branchCode()}"/>
</p:selectOneMenu>
</h:panelGroup>
</h:panelGroup>
See more about this.
I think problem could be with the way your implemented the p:ajax component. See the comments of p:ajax listener method not invoked.

How to pass a value on selected row change?

I have this permanent problem, I have a Datatable with value="#{myBean.Items}" var="itms", and I want to pass the selected item to my bean class.
In columns, we use <f:setPropertyActionListener value="#{itms}" target="#{myBean.selectedrow}" /> to pass the value, but I want it for Rows.
How to do that?, and where to put this listener?.
Thank you very much.
What richface version are you using?
For richfaces 4.3.x, the following example might do the trick:
XHTML:
<rich:extendedDataTable
id="myTable"
value="#{crudBean.rows}"
var="rowItem"
rowClasses="odd-row, even-row"
selection="#{crudBean.actionForm.selection}"
rows="#{crudBean.actionForm.occurrences}"
rowKeyVar="idx"
>
<a4j:ajax event="selectionchange"
listener="#{crudBean.actionForm.selectionListener}"
immediate="true" />
<rich:column width="100px" styleClass="#{rowItem.className}">
...stuff...
</rich:column>
<rich:column width="173px" styleClass="#{rowItem.className}">
...stuff...
</rich:column>
</rich:extendedDataTable>
The following code made sure the even got fired on row selection change:
<a4j:ajax event="selectionchange"
listener="#{crudBean.actionForm.selectionListener}"
immediate="true" />
The bean:
public void selectionListener(AjaxBehaviorEvent event) {
UIExtendedDataTable currentDataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = currentDataTable.getRowKey();
// debug log statement
log.debug("selectionListener() - rowKey = {} ", originalKey);
// debug log statement
if (log.isDebugEnabled()) {
log.debug("\n selectionListener() - rowIndex = {}", currentDataTable.getRowIndex());
}
if (currentDataTable.isRowAvailable()) {
// selectionItems.add(dataTable.getRowData());
IDataRow rowValue = transform((IDataGridRow)currentDataTable.getRowData());
changeActiveRow(rowValue);
}
}

To get the row number of the selected primefaces datatable row

I have a primefaces datatable i need to display (selected row number) of (total number of rows) in the JSF page.I could get the row numbers displayed in one of the columns using rowIndexVar attribute but i am not getting any idea to display the same numbers separately in the input text on row select.
What should i need to do in JSF page or managed bean to get selected row number.
Please help me in this regard.
Below is my JSF page
<p:dataTable id="workSpaceList" var="data"
value="#{workSpaceBean.lpInfoList}" widgetVar="multiSelection"
selection="#{workSpaceBean.selectedRows}" resizableColumns="true"
liveScroll="true" scrollRows="55" scrollWidth="85%"
scrollHeight="81%" styleClass="datatable"
scrollable="true" rowIndexVar="rowIndex"
filteredValue="#{workSpaceBean.filteredWorkSpaceItems}">
<p:column selectionMode="multiple" style="width:3%" />
<p:column headerText="#" style="width:3%">
#{rowIndex+1}
</p:column>
<p:column headerText="Insured" filterBy="#{data.insuredName}"
sortBy="#{data.insuredName}" style="width:24%">
<h:outputText value="#{data.insuredName}" />
<!-- style="width:250px" -->
</p:column>
<p:column headerText="City" filterBy="#{data.custAddress_City}"
sortBy="#{data.custAddress_City}" style="width:12%">
<h:outputText value="#{data.custAddress_City}" />
</p:column>
.
.
.
.
</p:dataTable>
I believe that there's not a straight forward way to do so. Although using two ajax requests is not pretty, you can at least achieve the result you expect when using plain PrimeFaces. You can reduce this to one call if you replace the p:ajax with the PrimeFaces extensions pe:javascript which does not do a roundtrip to the server
Every row (tr) rendered by your datatable has a attribute called data-rk with your rowKey and another attribute called data-ri with your rowIndexVar value.
You can get the data-rk attribute through dtWidgetVar.selection (dtWidgetVar is the name of the widgetVar in your datatable).
You can now send the indexRow to your model using a remoteCommand
Here is the code I used to test it:
The View
<p:remoteCommand name="displayIndex" process="#this" update="index" actionListener="#{viewMBean.displayRowIndex}"/>
<p:dataTable id="dt" var="data"
value="#{viewMBean.dataModel}"
selection="#{viewMBean.selectedRow}"
selectionMode="single"
widgetVar="dtVar"
rowIndexVar="index">
<p:ajax event="rowSelect"
oncomplete="displayIndex([{name:'index', value:jQuery('tr[data-rk=' + dtVar.selection + ']').attr('data-ri')}])" process="#this" />
<p:column headerText="#">
#{index + 1}
</p:column>
<p:column headerText="Dados">
#{data.name}
</p:column>
</p:dataTable>
<br />
Row Index: <p:inputText id="index" value="#{viewMBean.index}" />
Managed Bean
public void displayRowIndex() {
FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String pIndex = (String) map.get("index");
index = Integer.parseInt(pIndex);
}
In case you are using checkbox selection, you can retrieve the selected indexes like this:
function beforeDisplayingIndexes(){
var indexes = "";
jQuery("tbody .ui-chkbox-box").each(function(){
if (jQuery(this).hasClass("ui-state-active")){
indexes = indexes + (indexes === "" ? "" : ",") + jQuery(this).closest("tr").attr("data-ri");
}
});
//for debuging only
console.log(indexes);
displayIndex([{name:'index', value:indexes}])
}
You should now be able to make the proper modification to your code to make use of that.
I'm surprised by the complex solution in the other answer. Assuming you you have the #{workSpaceBean.lpInfoList} serverside when doing the 'select', you can easily do (code according to the PrimeFaces showcase, adapt according to your needs)
<p:ajax event="rowSelect" listener="#{dtSelectionView.onRowSelect}" update="..." />
public void onRowSelect(SelectEvent event) {
int rownum = cars.indexOf((Car)event.getObject());
}

retrive value from collection and show in datatable

I want to show language in view_lang.xhtml using datatable,Below are my classes
CountryBean.java
private ArrayList<Country> existingCountryList;
public ArrayList<Country> getExistingCountryList() {
System.out.println("CountryBean.getExistingCountryList::Enter");
existingCountryList = new ArrayList<Country>();
existingCountryList.addAll(getCountryService().getExistingCountry());
System.out.println("existingCountryList in countryBean"+existingCountryList);
System.out.println("CountryBean.getExistingCountryList:::Exit");
return existingCountryList;
}
country.java
private Set<CountryLanguage> countryLanguage = new HashSet<CountryLanguage>(0);
CountryLanguage.java
private CountryLanguageID countryLangPK = new CountryLanguageID();
CountryLanguageID.java
private Country country;
private Language language;
view_lang.xhtml
<h:dataTable id="existingCountry" var="countryLang" value="#{countryBean.existingCountryList}"
style="width: 100%" cellpadding="0" cellspacing="1" border="0" class="role_detail_section" rowClasses="activity_white, activity_blue">
<h:column>
<f:facet name="header">
<h:outputText value="Language(Code)" styleClass="heading_pm_det_white"/>
</f:facet>
<h:outputText value="#{countryLang.languageName}(#{countryLang.languageCode})" styleClass="heading_pm_det_white" />
</h:column>
</h:dataTable>
I am able to get country object with language but not able to print in datatabel.
what will be syntex do I have to use forEach, if yes then how.
thnx
You can use <ui:repeat> for this, but this doesn't support Set (because it's not ordered by an index). You need to convert it to a List or an array. If you're using EL 2.2, then you could use the Set#toArray() call directly in EL:
<ui:repeat value="#{countryLang.countryLanguage.toArray()}" var="countryLanguage">
...
</ui:repeat>
Update, as per the comments, you'd like ot print it comma separated, here's how you could do it:
<ui:repeat value="#{countryLanguage.language.languageName}" var="languageName" varStatus="loop">
#{languageName}#{loop.last ? '' : ', '}
</ui:repeat>
Note: if languageName is actually a Set instead of List, obviously use toArray() there.

form rendered on primefaces isn't calling the manage bean

I'm having problems with a form that is rendered by a booleanButtom on PrimeFaces 3.5
the form appear as expected but when I commit the values of the field come null.
The code of the pag:`
<p:panelGrid>
<p:row>
<p:column>
<h:form id="city">
<p:panelGrid columns="2">
<h:outputText value="Name: "/>
<p:inputText label="Name" value="#{managedBean.city.name}"/>
<h:outputText value="Status: "/>
<p:selectBooleanButton value="#{managedBean.city.status}" onLabel="Enable" offLabel="Disable">
<p:ajax listener="#{managedBean.changeStatusCity()}"/>
</p:selectBooleanButton>
<h:outputText value="Add neighborhood?"/>
<p:selectBooleanButton id="btAddNeighborhood" value="#{managedBean.addNeighborCity}" onLabel="Cancel" offLabel="Yes">
<p:ajax update=":addNeighbor" listener="#{managedBean.addNeighborCity()}"/>
</p:selectBooleanButton>
</p:panelGrid>
</h:form>
</p:column>
</p:row>
<p:row>
<p:column>
<h:form id="addNeighbor">
<p:panel header="Neighborhood" rendered="#{managedBean.addNeighborCity}">
<p:panelGrid columns="2">
<h:outputText value="Name: "/>
<p:inputText label="Name" value="#{managedBean.neighborhood.name}"/>
<h:outputText value="Status: "/>
<p:selectBooleanButton value="#{managedBean.neighborhood.status}" onLabel="Enable" offLabel="Disable" onIcon="ui-icon-check" offIcon="ui-icon-close">
<p:ajax listener="#{managedBean.changeStatusNeighbor()}"/>
</p:selectBooleanButton>
</p:panelGrid>
</p:panel>
</h:form>
<h:form id="formBt">
<p:commandButton id="bt" value="Add" actionListener="#{managedBean.saveNeighborCity()}" update=":addNeighbor, :city:btAddNeighborhood"/>
</h:form>
</p:column>
</p:row>
</p:panelGrid>`
And the manage bean
public void addNeighborCity(){
if(addNeighborCity){
neighborhood = new Neighborhood();
neighborhood .setStatus(true);
neighborhood .setStringStatus("Enable");
}else{
neighborhood = null;
}
}
public void changeStatusNeighbor() {
if (neighborhood .isStatus()) {
neighborhood .setStringStatus("Enable");
} else {
neighborhood .setStringStatus("Disable");
}
}
public void saveNeighborCity(){
city.getNeighborhoods().add(neighborhood );
neighborhood = null;
addNeighborCity = false;
}
All the input inside of the form that was rendered doesn't send the information to the manage bean and when I put the button that add the neighbor to the list of the city the button stop working and doesn't call the manage bean any more.
Does someone knows what I'm doing wrong or what is happening.
I'm using Primefaces3.5, GlassFish4.0 and JSF2.2
As you did not post the whole managed bean, I guess that you're using the default scope of the managed bean: #RequestScoped. In that case you should use #ViewScoped.
I solved the problem, I was using glassfish 4 and it forces the jsf2.2 even when you put the jsf2.1 in our project, then I changed to glassfish 3 and all work just fine, thanks for the answers, and I'm using #ScopeSession.

Resources