how to Close p:accordionPanel on button click - jsf

I am using to enter address field .by default am setting active-index="-1" so the panel will be closed when the page gets loaded.After the details has been entered by user i want the panel area which means i want the accordion panel to be closed on button(save) click.but i tried a lot using java script but am not able to achieve ,and here is page
<h:body>
<ui:composition template="/WEB-INF/templates/layout.xhtml">
<ui:define name="content">
<h:form id="quotesForm">
<div class="headerbg">
<div id="innerheaderbg">
<div id="iconarea">
<img src="#{request.contextPath}/images/headerimages/productlist.png" />
</div>
<div id="headertextarea">
<p class="headingtext">New Quote</p>
<p id="breadCrumbtext">Order  <img src="#{request.contextPath}/images/error-bullet.gif" />
 Quote List  <img src="#{request.contextPath}/images/error-bullet.gif" />
 New Quote
</p>
</div>
</div>
</div>
<p:growl id="msgs" />
<div class="widget widget-table action-table">
<div class="widget-header" style="text-align:center;padding-top: 52px">
<h3>Create New Quote</h3>
</div>
<div class="widget-content" style="background-color: #DAEDF4;height: 600px;" >
<div id="calender" style="margin-left: 52px;margin-top: -26px">
<table>
<tr>
<td>
<p:outputLabel id="qDate" value="#{quotesBean.qtCreationDate}"></p:outputLabel>
</td>
<td>
<p:calendar id="popup" showOn="button" display="inline" >
<p:ajax event="dateSelect" listener="#{quotesBean.onDateSelect}" update="qDate" />
</p:calendar>
</td>
</tr>
</table>
<p:outputLabel value="Invoice # #{quotesBean.qCount}" style="margin-top:-15px;margin-left:10px;"/>
</div>
<div id="company">
<table align="right" style="margin-right: 52px">
<thead>
<tr>
<th style="text-align: center;padding: 0;"><img src="#{request.contextPath}/images/favicon.ico"/><p:spacer width="5px"/>SolvEdge</th>
</tr>
</thead>
<tr><td style="text-align: center;padding: 0;">Zameen Pallavaram,</td></tr>
<tr><td style="text-align: center;padding: 0;">Chennai,</td></tr>
<tr><td style="text-align: center;padding: 0;">INDIA</td></tr>
</table>
</div>
<h:form id="addForm">
<p:accordionPanel id="accordion" cache="false" activeIndex="-1" style="margin-bottom:20px;width:330px;" widgetVar="acc" >
<p:tab title="Bill To Address:" titleStyle="width:330px;background-color:#DAEDF4" >
<h:panelGrid columns="2">
<h:outputLabel value="Name" />
<h:inputText value="#{quotesBean.name}"/>
<h:outputLabel value="Street" />
<h:inputText value="#{quotesBean.street}"/>
<h:outputLabel value="City" />
<h:inputText value="#{quotesBean.city}"/>
<h:outputLabel value="ZIP" />
<h:inputText value="#{quotesBean.zip}"/>
</h:panelGrid>
<p:commandButton icon="ui-icon-save" value="Save" action="#{quotesBean.saveAddress}" update=":quotesForm:addForm:accordion" ajax="false" onclick="PF('quotesForm:addForm:accordion').hide();"/>
</p:tab>
</p:accordionPanel>
</h:form>
</div>
</div>
<script type="text/javascript">
var link = document.getElementById('quotesForm:popup_input');
link.style.display = 'none';
function closeAccordianPanel()
{
var panel = document.getElementById('quotesForm:addForm:accordion');
link.style.activeIndex = '-1';
/* PF('widget_accordion').unselect(0); */
}
</script>
</h:form>
<br></br>
</ui:define>
</ui:composition>
</h:body>
</html>
Note:Java Script i tried here is nothing worked for me,i would like to close the accordion panel on button click.

I bet you have javascript errors in the console...(PF('quotesForm:addForm:accordion').hide(); is wrong, it should be PF('acc').hide(); IF it works at all (nothing about that in the PrimeFaces docs). And why close a ui component via javascript if you do an 'ajax="false"'. And sending a click event to the tab you want to toggle works, but even better is to use the official client-side api: PF('acc').unselect(...);
Ahhhh... you have additional code below the commandbutton... but there is a wrong widgetVar value... and it is commented out... and the other part does nothing...

Unselect(index of tab) method only hide the p:tab.
If you want to hide your accordionPanel, you can use rendered attribute.

Here is the panel:
<p:panel id="toggleable" header="Toggleable" toggleable="true"
toggleOrientation="horizontal" toggleSpeed="500" closeSpeed="500"
widgetVar="panel" class="left-menu-panel" >
</p:panel>enter code here`
and button or commandLink which collapse panel
<p:commandLink styleClass="open-left-menu-button"
onclick="PF('panel').toggle()">
</p:commandLink>
You can set it with toggleOrientation="horizontal/vertical".
normal panel - not collapsed
collapsed

Related

Multiple forms in jsf page

Here is my view
<h:body>
<div id="header"></div>
<div id="upload">
<h:form id="frmMain" enctype="multipart/form-data">
<p:inputTextarea id="text" style="width: 99%" value="#{uploadfile.content}" placeholder="What are you thinking?"></p:inputTextarea>
<p:fileUpload auto="true" oncomplete="input()" id="fileIdPhoto" previewWidth="200" style="width: 100%" messageTemplate="{name}" fileUploadListener="#{uploadfile.upload}" mode="advanced" dragDropSupport="true"
multiple="true" update="messages" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:messages id="messages" autoUpdate="true" closable="true" />
<p:menubar autoDisplay="false">
<p:menuitem />
<f:facet name="options">
<p:commandButton value="Publish" update="fileIdPhoto" icon="ui-icon-extlink" actionListener="#{uploadfile.uploadPhoto}">
</p:commandButton>
</f:facet>
</p:menubar>
</h:form>
</div>
<div id="body">
<div id="post_container">
<ul>
<ui:repeat value="#{image.allpost}" var="value">
<h:form id="form">
<li>
<div id="post">
<p:commandButton rendered="false" id="dynaButton" process="#this,form" value="Show" type="button" icon="ui-icon-extlink"/> cant not trigger it so I set render false
<p:menu overlay="true" trigger="dynaButton" my="left top" at="left bottom">
<p:submenu label="Ajax">
<p:menuitem value="Delete" icon="ui-icon-disk"/>
<p:menuitem value="Update" icon="ui-icon-arrowrefresh-1-w"/>
</p:submenu>
</p:menu>
<b>#{value.content}</b> <br/>
#{value.createtime} <br/>
<br />
<p:galleria value="#{image.getImgByID(value.id)}" rendered="#{not empty image.getImgByID(value.id)}" var="a" panelWidth="720" panelHeight="400" showCaption="true">
<h:graphicImage value="data:image/jpeg;base64,#{a.imgUrl}" style="width:100%;height:100% " alt="Image Description for #{a.imgName}" title="#{a.imgName}"/>
</p:galleria>
<p:rating value="#{value.rating}" stars="10" cancel="false"/>cant change those stars by clicking on it
</div>
<div id="post_ceperator"/>
</li>
</h:form>
</ui:repeat>
</ul>
</div>
</div>
</h:body>
What I am trying to do is change the value of p:rating tag and p:menu to trigger but its seem like my page is only submitting the first form. Also how can I submit a post and div that contains my post will automatically update?
In my opinion the problem is that your components inside of the ui:repeat have all the time the same id's for each iteration.
The way i used to define ids in loops like that was to add some sort of unique prefix or suffix to them based on some field of the data over which i am iterating:
<ui:repeat value="#{image.allpost}" var="value">
<h:form id="form_#{value.id}">
<li>
<div id="post_#{value.id}">
Now each of your components, including the form, which is highly likely to be the cause of your problem, will have its unique id for each of the loop repetition.

#RequestScoped Bean not working as expected? (JSF)

I have a JSF page where I can add or delete "planes" in my database (JBDC). To help with that I have two beans (Both #RequestScoped), one "controller" and one "backing bean".
Here is the JSF page:
And with some inputs:
After inputting info and adding a photo, the "airplane" is added to my page, and I reload the page.
Here is how the page looks after submitting:
My issue here is that, the input textfields gets "populated" from the bean with the info, and also the delete plane field gets populated with the airplane id. I want these fields to be empty after refreshing, and thats why I thought using #RequestScoped would be useful. Also, If I try to refresh the page from my web browser, it just tries to resend the form.
How can I avoid the fields being populated with data from the bean?
Also, here is bean code:
#Named
#RequestScoped
public class AddAirplaneCtrl implements Serializable{
#Inject
private FlightModel flightModel;
private ListAirplaneBB listAirplaneBB;
protected AddAirplaneCtrl(){
;
}
#Inject
public void addListAirplaneBB(ListAirplaneBB listAirplaneBB){
this.listAirplaneBB = listAirplaneBB;
}
public String addPlane(){
listAirplaneBB.setError(null);
listAirplaneBB.setMessage(null);
if(failed any of the validation parts){
return /some/site?faces-redirect=false";
}
//add plane
return /some/site?faces-redirect=true";
}
}
And the Backing Bean:
#Named
#RequestScoped
public class ListAirplaneBB implements Serializable{
#Inject
private FlightModel flightModel;
//private variable fields
public List<Airplane> getAllPlanes(){
return flightModel.getAirplaneList().findAll();
}
public int getTotalPlanes(){
return getAllPlanes().size();
}
//GETTERS and SETTERS
}
And finally my jsf page:
<div class="contbox">
<h3>
<h:outputLabel value="Edit Planes" />
</h3>
<c:if test="#{not empty listAirplaneBB.error}">
<div class="alert alert-danger"
id="success-alert">
<span class="glyphicon glyphicon-remove" />
<h:outputText value="#{listAirplaneBB.error}" />
<button type="button"
class="close"
data-dismiss="alert">
<h:outputLabel value="x" />
</button>
</div>
</c:if>
<c:if test="#{not empty listAirplaneBB.message}">
<div class="alert alert-success">
<span class="glyphicon glyphicon-remove" />
<h:outputText value="#{listAirplaneBB.message}" />
<button type="button"
class="close"
data-dismiss="alert">
<h:outputLabel value="x" />
</button>
</div>
</c:if>
<h4>Add A Plane</h4>
<h:form enctype="multipart/form-data">
<table id="addtable"
class="table">
<thead>
<tr>
<th>
<h:outputLabel value="Plane Make" />
</th>
<th colspan="1">
<h:outputLabel value="Plane Model" />
</th>
<th colspan="1">
<h:outputLabel value="Plane Seats" />
</th>
<th colspan="1">
<h:outputLabel value="Plane Photo" />
</th>
</tr>
</thead>
<tr>
<td>
<h:inputText value="#{listAirplaneBB.make}">
</h:inputText>
</td>
<td>
<h:inputText value="#{listAirplaneBB.model}">
</h:inputText>
</td>
<td>
<h:inputText value="#{listAirplaneBB.seats}">
</h:inputText>
</td>
<td>
<h:inputFile value="#{listAirplaneBB.file}" >
</h:inputFile>
</td>
<td>
<h:commandButton value="Add Plane"
class="btn btn-primary"
action="#{addAirplaneCtrl.addPlane()}" >
</h:commandButton>
</td>
</tr>
</table>
</h:form>
<h4>Delete Plane</h4>
<h:form>
<h:panelGrid columns="3" class="table">
<h:outputLabel value="Plane ID" />
<h:inputText value="#{listAirplaneBB.airPlaneId}"/>
<h:commandButton value="Delete"
class="btn btn-primary"
action="#{addAirplaneCtrl.deleteAirplane}" >
</h:commandButton>
</h:panelGrid>
</h:form>
</div>
Your browser cache the inputs.
JSF 2.2 Solution
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
...
<f:passThroughAttribute name="autocomplete" value="off"/>
...
jQuery Solution:
<h:form styleClass="form">...</h:form>
<script>
$(function(){
$(".form").attr("autocomplete", "off");
});
</script>
You can find more information here.

JSF button to dynamically add row to table also inserts extra table

I am using Primefaces 5.2 on an internal network that uses IE11 in compatibility mode. I have a button and a table. When I press the button I get a new row in the table just fine but I also get an empty table <table></table> inserted between the button and the table. This happened each and every time I press the button - three presses three unwanted empty tables.
Sanitized JSF source follows:
<tr>
<td>
<h:outputText value="#{enr['birth.date']}"/>*:
</td>
<td>
<p:calendar id="birthDate" value="#{II.birthdate}" mode="popup" showOn="button" popupIconOnly="true" pattern="yyyy-MM-dd" mask="9999-99-99" maxdate="#{UserBean.currentDate}" yearrange="c-120" navigator="true" showButtonPanel="true" required="true" requiredMessage="#{enr['missing.required.field']}: #{enr['birth.date']}" tabindex="4"/>
<p:tooltip for="birthDate" value="yyyy-MM-dd" showEffect="fade" hideEffect="fade" />
</td>
</tr>
<tr>
<td>
<h:outputText value="#{enr['other.last.names']}"/> / <h:outputText value="#{enr['maiden.name']}"/>
</td>
<td>
<p:commandButton icon="ui-icon-plus" title="Icon Only" tabindex="5" immediate="true">
<f:ajax listener="#{II.addOtherName()}" immediate="true" render="otherNameFields"/>
</p:commandButton>
<h:dataTable id="otherNameFields" value="#{II.otherNames}" var="otherNameField">
<h:column size="30" maxlength="50">
<h:inputText value="#{otherNameField.otherName}" tabindex="6">
<f:ajax event="change" listener="#{II.populateName()}" immediate="true" render="otherNameFields" />
</h:inputText>
</h:column>
<h:column>
<p:commandButton icon="ui-icon-trash" title="Icon Only" immediate="true" tabindex="6">
<f:ajax listener="#{II.deleteOtherName(otherNameField)}" immediate="true" render="otherNameFields" />
</p:commandButton>
</h:column>
</h:dataTable>
</td>
</tr>
Here is the generated output I see in the DOM Explorer:
<tr>
<td>
<button name="dataForm:j_id605621413_2_2c601363" tabindex="5" title="Icon Only" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" id="dataForm:j_id605621413_2_2c601363" role="button" aria-disabled="false" onclick="PrimeFaces.bcn(this,event,[function(event){jsf.ajax.request('dataForm:j_id605621413_2_2c601363',event,{render:'dataForm:otherNameFields','javax.faces.behavior.event':'click'})},function(event){PrimeFaces.ab({s:"dataForm:j_id605621413_2_2c601363"});return false;}]);" type="submit"><span class="ui-button-icon-left ui-icon ui-c ui-icon-plus"></span><span class="ui-button-text ui-c">ui-button</span></button>
<table></table> <!-- the unwanted table element -->
<table id="dataForm:otherNameFields">
<tbody id="dataForm:otherNameFields:tbody_element">
<tr><td><input name="dataForm:otherNameFields:0:j_id605621413_2_2c6013ca" tabindex="6" id="dataForm:otherNameFields:0:j_id605621413_2_2c6013ca" onchange="jsf.ajax.request('dataForm:otherNameFields:0:j_id605621413_2_2c6013ca',event,{render:'dataForm:otherNameFields','javax.faces.behavior.event':'change'})" type="text" value=""></td><td><button name="dataForm:otherNameFields:0:j_id605621413_2_2c601007" tabindex="6" title="Icon Only" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" id="dataForm:otherNameFields:0:j_id605621413_2_2c601007" role="button" aria-disabled="false" onclick="PrimeFaces.bcn(this,event,[function(event){jsf.ajax.request('dataForm:otherNameFields:0:j_id605621413_2_2c601007',event,{render:'dataForm:otherNameFields','javax.faces.behavior.event':'click'})},function(event){PrimeFaces.ab({s:"dataForm:otherNameFields:0:j_id605621413_2_2c601007"});return false;}]);" type="submit"><span class="ui-button-icon-left ui-icon ui-c ui-icon-trash"></span><span class="ui-button-text ui-c">ui-button</span></button></td></tr>
</tbody></table>
</td>
</tr>
Any

JSF (PrimeFaces) radio button behavior when space bar used to activate

I am using PrimeFaces 5.2 to render a radio button group for gender. This is for an internal network so will only run on IE11 browsers in compatibility mode. When I select one of the radio buttons by pressing space the button is correctly activated but I don't know where the focus has gone. If I press TAB I got to the first item that I set the tabindex for, but if I press SHIFT-TAB focus does not go to the previous element that I set the tabindex on but goes to a button that I did not set the tabindex on and is also not the first or last element in the default tab order (it is not the first or last element added to the page).
The element before the gender radio buttons is included, the calendar button is what is getting selected with SHIFT-TAB The JSF (with sensitive information removed) is as follows:
<tr>
<td>
<h:outputText value="#{enr['birth.date']}"/>*:
</td>
<td>
<p:calendar id="birthDate" value="#{II.birthdate}" mode="popup" showOn="button" popupIconOnly="true" pattern="yyyy-MM-dd" mask="9999-99-99" maxdate="#{UserBean.currentDate}" yearrange="c-120" navigator="true" showButtonPanel="true" required="true" requiredMessage="#{enr['missing.required.field']}: #{enr['birth.date']}" tabindex="4"/>
<p:tooltip for="birthDate" value="yyyy-MM-dd" showEffect="fade" hideEffect="fade" />
</td>
</tr>
<tr>
<td>
<h:outputText value="#{enr['other.last.names']}"/> / <h:outputText value="#{enr['maiden.name']}"/>
</td>
<td>
<p:commandButton icon="ui-icon-plus" title="Icon Only" tabindex="5" immediate="true">
<f:ajax listener="#{II.addOtherName()}" immediate="true" render="otherNameFields"/>
</p:commandButton>
<h:dataTable id="otherNameFields" value="#{II.otherNames}" var="otherNameField">
<h:column size="30" maxlength="50">
<h:inputText value="#{otherNameField.otherName}" tabindex="6">
<f:ajax event="change" listener="#{II.populateName()}" immediate="true" render="otherNameFields" />
</h:inputText>
</h:column>
<h:column>
<p:commandButton icon="ui-icon-trash" title="Icon Only" immediate="true" tabindex="6">
<f:ajax listener="#{II.deleteOtherName(otherNameField)}" immediate="true" render="otherNameFields" />
</p:commandButton>
</h:column>
</h:dataTable>
</td>
</tr>
<tr>
<td>
<h:outputText value="#{enr['gender']}"/>*:
</td>
<td>
<p:selectOneRadio value="#{II.gender}" tabindex="7" required="true" requiredMessage="#{enr['missing.required.field']}: #{enr['gender']}" style="border-style: none !important;">
<f:selectItem itemValue="2" itemLabel="#{enr['male']}"/>
<f:selectItem itemValue="1" itemLabel="#{enr['female']}"/>
</p:selectOneRadio>
</td>
</tr>
I also did a plain non-JSF version for comparison which works as expected so I am certain the problem is in the JSF.
Plain HTML below:
<html>
<body>
<p>Non Index:<input type="text" /></p>
<p>Index #200:<input tabindex="200" type="text" /></p>
<p><input name="gender" tabindex="201" id="gender_male" type="radio" value="2" />Male</p>
<p><input name="gender" tabindex="201" id="gender_female" type="radio" value="1" />Female</p>
<p>Index #202:<input tabindex="202" type="text" /></p>
</body>
</html>
I
This can be fixed by using plain JSF version of the selectOneRadio, ie:
<h:selectOneRadio value="#{II.gender}" tabindex="7" required="true" requiredMessage="#{enr['missing.required.field']}: #{enr['gender']}" style="border-style: none !important;">
<f:selectItem itemValue="2" itemLabel="#{enr['male']}"/>
<f:selectItem itemValue="1" itemLabel="#{enr['female']}"/>
</h:selectOneRadio>
where:
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"

How to refresh inputboxes in JSF after ajax submit command?

I have an inputText and an inputTextArea in JSF (shown below) that I carry out an ajax action on. However, after my ajax event, the values remain in the input boxes. Is there a simple way to refresh these text boxes after the commandButton is involked? Thanks.
<table>
<tr>
<td><div class="white"><H2>Comments</H2></div>
<h:inputText class="inputboxes" id="username"
value="#{commentBean.comment.commentname}" size="20">
</h:inputText><br/><br/>
<h:inputTextarea rows="5" cols="55" id="comment" class="inputbox" value="#{commentBean.comment.commentText}"
size="20" /></td>
</tr>
<tr>
<td></td>
<td><h:commandButton id="update"
action="#{commentBean.addComment(searchBean.companyId)}" class="myButton2" value="Add Comment" >
<f:ajax execute="#form" render=":results"/>
</h:commandButton></td>
</tr>
</table>
</h:form>
<h:form id="results">
<table>
<tr>
<td>
<h:dataTable value="#{commentBean.viewComments(searchBean.companyId)}" var="c">
<h:column>
<div class="commentsbox">#{c.commentText}</div>
</h:column>
<h:column>
<div class="boldfont">#{c.commentdate}</div>
</h:column>
<td><h:column>
<div class="commentname">#{c.commentname}</div>
</h:column>
</td>
</h:dataTable>
</td>
</tr>
</table>
In your view :
<f:ajax execute="#this" render=":results, username, comment"/>
In your backing bean, in the addComment() method :
comment.setCommentname(null);
comment.setCommentText(null);
or simply (I guess only)
comment = null;

Resources