h:selectOneMenu value should change onclick of p:selectBooleanCheckbox - jsf

I have a problem in my application.
I have a datatable, inside that one selectBooleancheckbox , selectonemenu with items("Confirmed", "Rejected" ,"selected") and one inputText is there. I need to write code for,
if selectbooleancheckbox is checked then the selectonemenu will get the value "Selected" and the inputText also become editable.
this is my xhtml code:
<p:dataTable id="panel1"
rendered="true"
var="step2table"
value="#{dtstep2_tab1.step2table}"
editMode="cell"
scrollable="true"
selection="#{dtstep2_tab1.selectedrows}"
rowKey="#{step2table.rs2_empname}"
rowSelectMode="checkbox"
rowStatePreserved="true">
<p:column headerText="Select" id="hSelect" style="font-size:12px;width:60px;">
<p:selectBooleanCheckbox id="Booleanchkbox" >
<p:ajax listener="#{dtstep2_tab1.UpdateStatus}" update="panel1" />
</p:selectBooleanCheckbox>
</p:column>
<p:column headerText="Status" id="hStatus" style="font-size:12px;width:100px;">
<h:selectOneMenu id="cbo" value="#{step2table.rs2_status}" >
<f:selectItem itemLabel="Confirmed" itemValue="Confirmed"></f:selectItem>
<f:selectItem itemLabel="Selected" itemValue="Selected"></f:selectItem>
<f:selectItem itemLabel="Rejected" itemValue="Rejected"></f:selectItem>
</h:selectOneMenu>
</p:column>
<p:column style="font-size:12px;width:150px;">
<f:facet name="header">
<h:outputText value="Comments"></h:outputText>
</f:facet>
<h:inputText value="#{step2table.rs2_comments}" rendered="#{dtstep2_tab1.editable}"/>
</p:column>
</p:dataTable>
and in bean I have the following code in addition with getter and setter method.
public void UpdateStatus(AjaxBehaviorEvent event) {
SelectBooleanCheckbox permit = (SelectBooleanCheckbox) event.getComponent();
boolean checked = (Boolean) permit.getValue();
if (checked) {
setRs2_status("Selected");
setEditable(true);
setSelect(true);
}
else
{
setEditable(false);
setSelect(false);
}
}
I don't know what I am doing wrong here. Please guide me on the issue.

Related

p:selectOneMenu inside selectManyMenu trouble

I have an element selectOneMenu inside element selectManyMenu (component names have been changed).
<p:selectManyMenu id="#{id}MyItemsList" converter="#{backend.myItemsConverter}"
value="#{backend.selectedItem}"
rendered="#{backend ne null}" var="item">
<f:selectItems value="#{backend.getMyItems()}"
var="varItems"
itemLabel="#{varItems.name}" itemValue="#{varItems.id}"/>
<p:column>
<h:outputText value="#{item.name}" title="#{item.title}"/>
</p:column>
<p:column>
<p:selectOneMenu value="#{item.subId}" converter="#{backend.subItemsConverter}">
<p:ajax listener="#{backend.onSubItemClick}"/>
<f:selectItems value="#{backend.subItems}" var="varSubItems"
itemLabel="#{varSubItems.name}"
itemValue="#{varSubItems.id}"/>
</p:selectOneMenu>
</p:column>
</p:selectManyMenu>
They are displayed normally, but when I change the value of the selectOneMenu in one line, it changes in all. What am I doing wrong?
example
<p:remoteCommand name="subItemChange" action="#{backend.onSubItemChange}"
process="#this"
update="#form:ButtonsPanel"/>
<p:dataTable id="#{id}MyItemsListReadOnly" converter="#{backend.myItemsConverter}"
value="#{backend.selectedItem}"
rendered="#{backend ne null}" var="item">
<p:column style="width: 50%">
<h:outputText value="#{item.name}" title="#{item.title}"/>
</p:column>
<p:column style="width: 50%">
<p:selectOneMenu value="#{item.subId}" converter="#{backend.subItemsConverter}"
onchange="subItemChange();" >
<f:selectItems value="#{backend.subItems}" var="varSubItems"
itemLabel="#{varSubItems.name}"
itemValue="#{varSubItems.id}"/>
</p:selectOneMenu>
</p:column>
</p:dataTable>
dataTable saved my time. In addition, to call onSubItemChange from it, need to use remoteCommand. Hope this helps to somebody.

Binding in JSF not working

I am trying to make a h:panelgrid visible and invisible by clicking on a button that's why i made this code:
<p:menu styleClass="ui-menubar" style="width:auto">
<p:menuitem value="khraaaaaaaaa" actionListener="#{accounts.buttonUser()}"></p:menuitem>
</p:menu>
<h:panelGrid id="naalobouk" binding="#{accounts.pg1}">
<h1>Activer/désactiver un compte étudiant</h1>
<hr/>
<h:form id="etudiants">
<p:dataTable emptyMessage="Pas d'étudiant!"
paginator="true"
paginatorPosition="bottom"
rows="4"
value="#{accounts.etudiants}"
var="stu">
<f:facet name="header">
<h:outputText value="Les comptes étudiant" />
</f:facet>
<p:column headerText="#">
<h:outputText value="#{stu.id}" />
</p:column>
<p:column headerText="nom">
<h:outputText value="#{stu.nom}" />
</p:column>
<p:column headerText="prenom">
<h:outputText value="#{stu.prenom}" />
</p:column>
<p:column headerText="email">
<h:outputText value="#{stu.email}" />
</p:column>
<p:column headerText="password">
<h:outputText value="#{stu.password}" />
</p:column>
<p:column headerText="action">
<p:commandButton rendered="#{accounts.butshow}" value="#{stu.actif?'desactiver':'activer'}" action="#{accounts.doToggleStudentState(stu)}" update=":etudiants"/>
</p:column>
</p:dataTable>
</h:form>
</h:panelGrid>
and here is my bean code
private HtmlPanelGrid pg1 = new HtmlPanelGrid();//getter and setter
public void buttonUser(){
if(pg1.isRendered()==true){
pg1.setRendered(false);;
}
if(pg1.isRendered()==false){
pg1.setRendered(true);
}
}
When i click on the button nothing happens. What's the problem here ?
I'm assuming you want the method to negate the rendered property. In that case the method is flawed, as mentioned in the comments. The simplest code would be
public void buttonUser() {
pg1.setRendered(!pg1.isRendered());
}
The main problem is that the p:menuItem by default uses ajax, which by default does'nt update anything on the page. So either update something, or don't use ajax;
<p:menuitem value="khraaaaaaaaa" actionListener="#{accounts.buttonUser()}" ajax="false">
With ajax you need to wrap the panelGrid in a component that will always be rendered, since you can't update a component that has not been rendered in the first place. Wrap the panelGrid in a panelGroup:
<h:panelGroup id="container">
and update that:
<p:menuitem value="khraaaaaaaaa" actionListener="#{accounts.buttonUser()}" update=":container">

commandLink within PickList in Primefaces passes null value to the managed bean

I am using Primefaces 5.0 and on one of my pages I have this picklist:
<h:form>
...
<p:pickList id="aspectPickList" styleClass="camsPickList" value="#{userAccountMB.aspects}" var="aspect" itemValue="#{aspect}"
itemLabel="#{aspect.aspectName}" showSourceFilter="true" showTargetFilter="true" converter="appAspectConverter">
<f:facet name="targetCaption">#{msg['createUser.userAspects.assignedAspects']}</f:facet>
<f:facet name="sourceCaption">#{msg['createUser.userAspects.availableAspects']}</f:facet>
<p:column>
<h:outputText id="aspectName" value="#{aspect.aspectName}" />
<p:tooltip id="toolTipAspectDescription" for="aspectName" value="#{aspect.description}" />
</p:column>
<p:column style="width:30px;">
#{aspect}
<p:commandLink process="#this" oncomplete="PF('aspectDetailsDialog').show();" actionListener="#{userAccountMB.setSelectedAspect(aspect)}">
<p:graphicImage value="#{resource['images:questionmark_icon.jpg']}" />
</p:commandLink>
</p:column>
</p:pickList>
...
</h:form>
Managed bean setSelectedAspect method looks like this:
public void setSelectedAspect(AppAspect selectedAspect) {
this.selectedAspect = selectedAspect;
}
The Scope of the Managed Bean is view.
When I click on the command link with icon, the setSelectedAspect method is invoked, but the selectedAspect parameter is null.
What I need to achieve is to show AspectDetails dialog after clicking the commandLink. The AspectDetails dialog contains details about the previously selected aspect.
if I update the code this way:
<p:column style="width:30px;">
#{aspect}
<p:commandLink process="#this" oncomplete="PF('aspectDetailsDialog').show();" actionListener="#{userAccountMB.selectAspect(aspect)}">
<p:graphicImage value="#{resource['images:questionmark_icon.jpg']}" />
</p:commandLink>
</p:column>
the #{aspect} directive correctly displays the object signature: com.xyz.app.domain.AppAspect#69e1f7aa.
What am I doing wrong? Do I miss something?
I had the same error, try this:
<p:pickList ...>
<p:ajax event="select" listener="#{userAccountMB.selectAspectEvent}">
<f:attribute name="aspectParam" value="#{aspect}" />
</p:ajax>
<f:facet ...>
<f:facet ...>
<p:column>...</p:column>
<p:column style="width:30px;">
<p:commandLink process="#this" oncomplete="PF('aspectDetailsDialog').show();">
<p:graphicImage value="#{resource['images:questionmark_icon.jpg']}" />
</p:commandLink>
</p:column>
</p:pickList>
And the controller:
public void selectAspectEvent(SelectEvent event) {
AppAspect aspectParam = (AppAspect) event.getObject();
if(aspectParam!=null){
this.selectedAspect = aspectParam;
}
}
Regards

PrimeFaces PickList component, how to get all target values to one InputText component?

I created xhtml page with one inputText and one commandButton component. When I clicked on button is displaying overlayPanel with pickList and one commandButton under list. I want to get all target values and put it into inputText (this component must be only one). Can you help me how to do it?
Below is xhtml code that I wrote for now:
<h:panelGrid id="displayPlayers" columns="3">
<ui:repeat value="#{pickListBean.players.target}" var="player">
<p:inputText id="input2" value="#{player.name}" />
</ui:repeat>
<p:commandButton id="overPanBtn2" value="overPanBtn2" type="button" />
</h:panelGrid>
<p:overlayPanel id="chartPanel2" for="overPanBtn2" hideEffect="fade">
<p:pickList id="pickList4" value="#{pickListBean.players}" var="player" itemLabel="#{player}" itemValue="#{player}" converter="playerConverter">
<p:column style="width:75%;">
#{player.name}
</p:column>
<p:column style="width:75%;">
#{player.number}
</p:column>
</p:pickList>
<p:commandButton id="playerSubmit4" value="Ok" update="displayPlayers" style="margin-top:5px" />
</p:overlayPanel>
I will be grateful for help.
In your case it should be something like that, you only have to follow the showcase example:
XHTML
<p:pickList id="pickList4" value="#{pickListBean.players}" var="player" itemLabel="#{player}" itemValue="#{player}" converter="playerConverter">
<p:ajax event="transfer" listener="#{pickListBean.onTransfer}" update="msg" />
<p:column style="width:75%;">
#{player.name}
</p:column>
<p:column style="width:75%;">
#{player.number}
</p:column>
</p:pickList>
BACKING BEAN
String selectedPlayerNames;
//GETTER
public void onTransfer(TransferEvent event) {
selectedPlayerNames = "";
for (Player p : players.getTarget()){
selectedPlayerNames += p.getName() + " ";
}
}
Just call the event when transfer happens, and save the targeted values into the String. Then bind this String property to a p:inputText.

p:commandLink actionListener is processed for each row of p:dataTable while rendering the page

I have a dataTable that shows data stored in a database. One of the column contains a commandLink (p:commandLink) to edit selected row which is fine.
I have a problem during the rendering of my xhtml page. It seems that the method of backingBean in the actionListener attribute of commandLink is processed for every row in the table, but the actionListener should be processed only when link is clicked.
Here is my xhtml page (part of):
<h:form id="formElenco">
<p:dataTable id="dt1" value="#{myBean.itemList}" var="item">
<f:facet name="header">
<h:outputText value="header" />
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="value" />
</f:facet>
<h:outputText value="#{item.value}"/>
</p:column>
<p:column>
<p:commandLink id="lnkItemUpdate" value="Edit"
onstart="document.getElementById('divUpdateItem').style.display = 'block';"
actionListener="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />
</p:column>
</p:dataTable>
</h:form>
<div id="divUpdateItem" style="display:none" >
<h:form id="formUpdateItem">
Nome <h:inputText value="#{myBean.name}" /><br/>
Met <h:inputText value="#{myBean.value}" /><br/>
<h:inputHidden value="#{myBean.id}" />
<h:commandButton action="#{myBean.updateItemAction}" value="Save" />
</h:form>
</div>
Here are myBean's method (myBean is requestScoped):
public String updateItemAction(Entity item){
this.setId(item.getId());
this.setName(item.getName());
this.setValue(item.getValue());
return null;
}
public String updateItemAction() {
Entity entity = new Entity();
entity.setId(this.getId());
entity.setName(this.getName());
entity.setVAlue(this.getValue());
updateEntityQueryMethod(entity);
return null;
}
This isn't a valid method signature for an actionListener, so it's treated as a value expression by the <p:commandLink>.
You should be using action instead.
<p:commandLink id="lnkItemUpdate" value="Edit"
onstart="document.getElementById('divUpdateItem').style.display = 'block';"
action="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />
Note that you can also just return void instead of a null String.
public void updateItemAction(Entity item) {
this.setId(item.getId());
this.setName(item.getName());
this.setValue(item.getValue());
}
A valid actionListener method signature would be a void method taking javax.faces.event.ActionEvent argument:
public void actionListener(ActionEvent event) {
// ...
}
See also:
Differences between action and actionListener

Resources