How to open a Panel with Drag and Drop of an image? - html

I would like to open a new Panel in the Center LayoutUnit by drag and drop an image from the west LayoutUnit to the Center LayoutUnit.
The problems that I can't solve are:
I have several images and I would like to differentiate the panel that is going to be open according to the image that is dropped.
The element that I drop is a graphicImage and there is only onclick for the event, ondrop is not possible.
The panels don't change even if I use the onclick.
Please find below the code, could someone help me please?
<h:body>
<p:layout fullPage="true" id="allLayout">
<p:layoutUnit position="north" size="200" resizable="false" closable="true" collapsible="true" >
<h:panelGrid id="monGrid1" columns="1" style="text-align: left;" >
<p:graphicImage value="resources/images/logo_transparent.png" />
<h:outputLabel value="Découverte des Animaux" style="font-size:15px;font-weight:bold;color:#000000;face:Calibri;"/>
</h:panelGrid>
</p:layoutUnit>
<p:layoutUnit position="west" size="290" header="OBJECTS" collapsible="true" resizable="false" style="font-size:12px" >
<h:form>
<h:panelGrid columns="3">
<p:inputText id="keyword" required="true" style="width: 195px"/>
<p:watermark for="keyword" value="..." />
<p:commandButton value="Search"/>
</h:panelGrid>
<p:fieldset legend="Oiseaux" toggleable="true" toggleSpeed="500" collapsed="true" >
<h:panelGrid columns="2" cellpadding="5">
<p:graphicImage id="pic1" value="resources/images/oiseau1.png" onclick="#{menu.setSelectedOption(menu.OPT2)}"/>
<p:graphicImage id="pic2" value="resources/images/oiseau2.png" onclick="#{menu.setSelectedOption(menu.OPT3)}"/>
<p:graphicImage id="pic3" value="resources/images/oiseau3.png" onclick="#{menu.setSelectedOption(menu.OPT4)}"/>
<p:draggable for="pic1;pic2;pic3" helper="clone" revert="true" />
</h:panelGrid>
</p:fieldset>
<p:fieldset legend="Chats" toggleable="true" toggleSpeed="500" collapsed="true" >
<h:panelGrid columns="5" >
</h:panelGrid>
</p:fieldset>
<p:fieldset legend="Chiens" toggleable="true" toggleSpeed="500" collapsed="true">
<h:panelGrid columns="2" cellpadding="5">
</h:panelGrid>
</p:fieldset>
<p:fieldset legend="Chevaux" toggleable="true" toggleSpeed="500" collapsed="true">
<h:panelGrid columns="2" cellpadding="5">
</h:panelGrid>
</p:fieldset>
<p:fieldset legend="Serpents" toggleable="true" toggleSpeed="500" collapsed="true">
<h:panelGrid columns="2" cellpadding="5">
</h:panelGrid>
</p:fieldset>
<p:fieldset legend="Araignées" toggleable="true" toggleSpeed="500" collapsed="true">
<h:panelGrid columns="2" cellpadding="5">
</h:panelGrid>
</p:fieldset>
<p:fieldset legend="Singes" toggleable="true" toggleSpeed="500" collapsed="true">
<h:panelGrid columns="2" cellpadding="5">
</h:panelGrid>
</p:fieldset>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center" style="background: transparent ">
<h:form>
<h:panelGroup id="opt1Panel" layout="block" style="height:150px;width:300px;" rendered="#{menu.selectedOption == menu.OPT1}">
<p class="ui-widget-header" style="margin: 0; padding: 5px;">Drop here</p>
<p:droppable onDrop="handleDrop" />
</h:panelGroup>
<p:panel id="opt2Panel" rendered="#{menu.selectedOption == menu.OPT2}">
<p:outputLabel value="Ceci est le panel 2." />
</p:panel>
<p:panel id="opt3Panel" rendered="#{menu.selectedOption == menu.OPT3}">
<p:outputLabel value="Ceci est le panel 3." />
</p:panel>
<p:panel id="opt4Panel" rendered="#{menu.selectedOption == menu.OPT4}">
<p:outputLabel value="Ceci est le panel 4." />
</p:panel>
</h:form>
</p:layoutUnit>
</p:layout>
<script>
function handleDrop(event, ui) {
$(event.target).addClass("ui-state-highlight").find("p").html("Dropped!");
}
</script>
</h:body>
#ManagedBean(name="menu")
#SessionScoped
public class Menu {
private final int OPT1 = 1;
private final int OPT2 = 2;
private final int OPT3 = 3;
private final int OPT4 = 4;
private int selectedOption;
public Menu() {
selectedOption = OPT1;
}
public int getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(int selectedOption) {
this.selectedOption = selectedOption;
}
public int getOPT1() {
return OPT1;
}
public int getOPT2() {
return OPT2;
}
public int getOPT3() {
return OPT3;
}
public int getOPT4(){
return OPT4;
}
}

You should nest a p:ajax into your p:droppable in the target panel.
To identify the given image is a bit tricky. If you have only a few, fix images, you can bind them to Menu bean maually and later compare their clientId (see example below).
An image example:
<p:graphicImage id="pic1" value="pathToYourImage" binding="#{menu.img1}"/>
The droppable target panel:
<h:panelGroup id="opt1Panel" layout="block" style="height:150px;width:300px;" rendered="#{menu.selectedOption == menu.OPT1}">
<p class="ui-widget-header" style="margin: 0; padding: 5px;">Drop here</p>
<p:droppable>
<p:ajax listener="#{menu.actionImageDropped}" update="#form" partialSubmit="true" process="#this" />
</p:droppable>
</h:panelGroup>
Menu bean modifications:
private GraphicImage img1;
public GraphicImage getImg1() {
return img1;
}
public void setImg1(GraphicImage img1) {
this.img1 = img1;
}
If you have a various number of images, the binding is not a solution for you. In that case, you should try to find the given component from the ViewRoot.
This could be helpful: Link
Menu callback:
public void actionImageDropped(DragDropEvent event){
String dragId = event.getDragId();
if(img1.getClientId().equals(dragId)){
selectedOption = OPT1
}else if(...){
...
}
}

Related

Update property doesn't seem to work

I have a datatable with dialog forms nested in a tabView tag....
each row of the datatable has buttons for editing and displaying data in Dialog form...but when i click on the edit or display button, it doesn't fill the form...And i don't know what's wrong with the update property....
I tried also to update the datatable after a create and update process by setting an update property in the forms commanButton but it seems that something is missing...
I also visited this link...update-form-on-hcommandbutton-action
and it doesn't seem to work in my case...maybe because update property is not set corectly.
when i delete the tabView, layout and LayoutUnit tags, it works correctly
Here is my index.xhtml code:
<h:body>
<ui:composition template="/template.xhtml">
<ui:define name="content">
<f:view>
<p:tabView id="tout">
<p:tab title="Identite" id="test">
<p:layout style="min-width:400px;min-height:450px;" id="change">
<p:layoutUnit position="center" id="tester">
/* this Dialog form is for creating a new Antecedent*/
<p:dialog showEffect="drop" hideEffect="drop" id="creation" widgetVar="creation" header="Enregistrer locataire">
<h:form id="creer">
<center>
<p:panelGrid columns="4">
<p:outputLabel value="DateDiagnostic:" for="dateDiagnostic" />
<p:inputText id="dateDiagnostic" value="#{antecedentMBean.antecedent.dateDiagnostic}" title="DateDiagnostic" >
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</p:inputText>
<p:outputLabel value="DateGuerison:" for="dateGuerison" />
<p:inputText id="dateGuerison" value="#{antecedentMBean.antecedent.dateGuerison}" title="DateGuerison" >
<f:convertDateTime pattern="MM/dd/yyyy" />
</p:inputText>
<p:outputLabel value="Allergies:" for="allergies" />
<p:inputText id="allergies" value="#{antecedentMBean.antecedent.allergies}" title="Allergies" />
<p:outputLabel value="Therapie:" for="therapie" />
<p:inputText id="therapie" value="#{antecedentMBean.antecedent.therapie}" title="Therapie" />
</p:panelGrid>
<p:panelGrid columns="1">
<p:outputLabel value="Diagnostique:" for="diagnostique" />
<p:inputTextarea id="diagnostique" value="#{antecedentMBean.antecedent.diagnostique}" title="Diagnostique" autoResize="false" rows="6" cols="83"/>
</p:panelGrid>
<hr/>
</center>
<center>
<p:button icon="fa fa-close" value="Fermer" onclick="PF('creation').hide();
return false" />
<p:commandButton icon="fa fa-trash" id="effacer" value="Effacer" type="reset"/>
<p:commandButton icon="fa fa-save" value="Enregistrer" action="#{antecedentMBean.createAntecedent()}"
update=":formulaire:tbl" oncomplete="PF('creation').hide()" />
</center>
</h:form>
</p:dialog>
<h:form id="formulaire">
/* this Dialog form is for etiting */
<p:dialog showEffect="size" id="modif" hideEffect="size" widgetVar="modif" header="Modification">
<p:outputPanel id="repere" style="text-align:center;">
<center>
<p:panelGrid columns="4">
<p:outputLabel value="DateDiagnostic:" for="dateDiagnostic" />
<p:inputText id="dateDiagnostic" value="#{antecedentMBean.antecedent.dateDiagnostic}" title="DateDiagnostic" >
<f:convertDateTime pattern="MM/dd/yyyy" />
</p:inputText>
<p:outputLabel value="DateGuerison:" for="dateGuerison" />
<p:inputText id="dateGuerison" value="#{antecedentMBean.antecedent.dateGuerison}" title="DateGuerison" >
<f:convertDateTime pattern="MM/dd/yyyy" />
</p:inputText>
<p:outputLabel value="Allergies:" for="allergies" />
<p:inputText id="allergies" value="#{antecedentMBean.antecedent.allergies}" title="Allergies" />
<p:outputLabel value="Therapie:" for="therapie" />
<p:inputText id="therapie" value="#{antecedentMBean.antecedent.therapie}" title="Therapie" />
</p:panelGrid>
<p:panelGrid columns="1">
<p:outputLabel value="Diagnostique:" for="diagnostique" />
<p:inputTextarea id="diagnostique" value="#{antecedentMBean.antecedent.diagnostique}" title="Diagnostique" autoResize="false" rows="6" cols="83"/>
</p:panelGrid>
<hr/>
</center>
<center>
<p:button icon="fa fa-close" value="Fermer" onclick="PF('modif').hide();
return false" />
<p:commandButton icon="fa fa-trash" id="effacer" value="Effacer" type="reset"/>
<p:commandButton icon="fa fa-pencil" value="Modifier" action="#{antecedentMBean.updateAntecedent()}"
update=":formulaire:tbl" onclick="PF('modif').hide()" />
</center>
</p:outputPanel>
</p:dialog>
/* this Dialog form is for displaying a row details */
<p:dialog showEffect="size" hideEffect="size" id="voir" widgetVar="voir" header="Modification">
<p:outputPanel id="view" style="text-align:center;">
<center>
<p:panelGrid columns="4">
<p:outputLabel value="IdAntecedent:"/>
<h:outputText value="#{antecedentMBean.antecedent.idAntecedent}" title="IdAntecedent"/>
<p:outputLabel value="DateDiagnostic:"/>
<h:outputText value="#{antecedentMBean.antecedent.dateDiagnostic}" title="DateDiagnostic">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
<p:outputLabel value="DateGuerison:"/>
<h:outputText value="#{antecedentMBean.antecedent.dateGuerison}" title="DateGuerison">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
<p:outputLabel value="Allergies:"/>
<h:outputText value="#{antecedentMBean.antecedent.allergies}" title="Allergies"/>
<p:outputLabel value="Therapie:"/>
<h:outputText value="#{antecedentMBean.antecedent.therapie}" title="Therapie"/>
<p:outputLabel value="Diagnostique:"/>
<h:outputText value="#{antecedentMBean.antecedent.diagnostique}" title="Diagnostique"/>
<p:outputLabel value="CreerPar:"/>
<h:outputText value="#{antecedentMBean.antecedent.creerPar}" title="CreerPar"/>
<p:outputLabel
<p:outputLabel value="DateCreer:"/>
<h:outputText value="#{antecedentMBean.antecedent.dateCreer}" title="DateCreer">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
<p:outputLabel value="MajPar:"/>
<h:outputText value="#{antecedentMBean.antecedent.majPar}" title="MajPar"/>
<p:outputLabel value="DateMaj:"/>
<h:outputText value="#{antecedentMBean.antecedent.dateMaj}" title="DateMaj">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
<p:outputLabel value="IdPatient:"/>
<h:outputText value="#{antecedentMBean.antecedent.idPatient.prenom}" title="IdPatient"/>
</p:panelGrid>
<hr/>
</center>
<p:button icon="fa fa-close" value="Fermer" onclick="PF('voir').hide();
return false" />
</p:outputPanel>
</p:dialog>
/* this commandButton display the create form */
<p:commandButton icon="fa fa-plus" value="Creer"
onclick="PF('creation').show()"/>
/* Here is my dataTable */
<p:dataTable id="tbl" value="#{antecedentMBean.listAllAntecedents()}" var="item"
emptyMessage="Aucun Antecedent trouvee" widgetVar="AntecedentTable"
paginator="true" paginatorTemplate="{CurrentPageReport}
{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink}
{LastPageLink} {RowsPerPageDropdown} " paginatorPosition="top" rowsPerPageTemplate="5,10"
>
<f:facet name="header">
Liste des Patients
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="IdAntecedent"/>
</f:facet>
<h:outputText value="#{item.idAntecedent}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="DateDiagnostic"/>
</f:facet>
<h:outputText value="#{item.dateDiagnostic}">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="DateGuerison"/>
</f:facet>
<h:outputText value="#{item.dateGuerison}">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Allergies"/>
</f:facet>
<h:outputText value="#{item.allergies}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Therapie"/>
</f:facet>
<h:outputText value="#{item.therapie}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Diagnostique"/>
</f:facet>
<h:outputText value="#{item.diagnostique}"/>
</p:column>
<p:column headerText="Action" style="width:18%" exportable="false">
<div class="ui-g">
<div class="ui-g-4">
/* Here are the buttons for editing*/
<p:commandButton icon="fa fa-pencil" update=":formulaire:repere" oncomplete="PF('modif').show()">
<f:setPropertyActionListener value="#{item}" target="#{antecedentMBean.antecedent}" />
</p:commandButton>
</div>
<div class="ui-g-4">
/* Here are the buttons for deleting*/
<p:commandButton icon="fa fa-trash" style="background-color: red" type="button" onclick="PF('cd').show()" />
<p:confirmDialog message="Voulez vous vraiment supprimer cet enregistrer?"
header="Suppression en cours" severity="alert"
widgetVar="cd" global="true" showEffect="bounce" hideEffect="bounce">
<p:commandButton value="Oui" action="#{antecedentMBean.deleteAntecedent()}"
oncomplete="PF('cd').hide()" icon="ui-icon-check">
<f:setPropertyActionListener target="#{antecedentMBean.antecedent}" value="#{item}"/>
</p:commandButton>
<p:commandButton value="Non" onclick="PF('cd').hide();" type="button" icon="ui-icon-close"/>
</p:confirmDialog>
</div>
<div class="ui-g-4">
/* Here are the buttons for displaying*/
<p:commandButton icon="fa fa-binoculars" update=":formulaire:view" oncomplete="PF('voir').show()">
<f:setPropertyActionListener value="#{item}" target="#{antecedentMBean.antecedent}" />
</p:commandButton>
</div>
</div>
</p:column>
</p:dataTable>
</h:form>
</p:layoutUnit>
</p:layout>
</p:tab>
</p:tabView>
</f:view>
</ui:define>
</ui:composition>
</h:body>
An here is my managedbean:
package managedbeans;
import entities.Operation;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import sessions.OperationFacadeLocal;
Named(value = "operationMBean")
#ViewScoped
public class OperationMBean implements Serializable{
#EJB
private OperationFacadeLocal operationFacade;
private List<Operation> operationList;
private Operation operation;
private Date date = new Date();
public OperationFacadeLocal getOperationFacade() {
return operationFacade;
}
public void setOperationFacade(OperationFacadeLocal operationFacade) {
this.operationFacade = operationFacade;
}
public List<Operation> getOperationList() {
return operationList;
}
public void setOperationList(List<Operation> operationList) {
this.operationList = operationList;
}
public Operation getOperation() {
return operation;
}
public void setOperation(Operation operation) {
this.operation = operation;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public List<Operation> createOperation(){
operation.setDateCree(getDate());
operation.setDateMaj(getDate());
operationFacade.create(operation);
operationList = operationFacade.findAll();
return operationList;
}
public List<Operation> updateOperation(){
operation.setDateMaj(getDate());
operationFacade.edit(operation);
operationList = operationFacade.findAll();
return operationList;
}
public List<Operation> deleteOperation(){
operationFacade.remove(operation);
operationList = operationFacade.findAll();
return operationList;
}
/**
* Creates a new instance of OperationMBean
*/
public OperationMBean() {
}
}
I'm using PrimeFaces 6.1 Netbeans 8.2 Glassfish 4.1
Hope i've been clear in my question
Thanks in advance!!!
I solve this problem by using WidgetVar instead of id in the update property like this:
<p:commandButton update="widgetVar(element_widgetVar)" />
Thanks to Stackoverflow

primefaces dialog as a tooltip like facebook show people details

I want to show the details of a person with a dialog as shown on Facebook when the mouse is placed on a person's name.
I am using dialog framework in primefaces.
The dialog should display data that must be loaded when the mouse is placed on the person.
The problem is the dialog position:
<p:dataTable filterDelay="700" reflow="true" emptyMessage="No se encontraron elementos" widgetVar="tblist"
id="dataTableList" var="item" paginator="true" paginatorPosition="bottom"
rows="10" rowKey="#{item.idPersona}" value="#{listadoPersonasMB.listPersonas}" filteredValue="#{listadoPersonasMB.filterlistPersonas}">
<f:facet name="header">
Resultado de la búsqueda #{listadoPersonasMB.cantPerFilter}
</f:facet>
<p:ajax event="filter" oncomplete="handleLoadStart();"/>
<p:column style="width: 50px">
<p:graphicImage style="border-radius: 50%" alt="image" id="fotoPer" value="#{listadoPersonasMB.fotoPersonaTabla}" cache="false" width="100%">
<f:param name="personId" value="#{item.idPersona}" />
</p:graphicImage>
</p:column>
<p:column width="100%" headerText="Nombre y apellidos" sortBy="#{item.nombre}" style="text-align: left" filterBy="#{item.nombre} #{item.nombre2} #{item.apellidos} #{item.apellidos2}" filterMatchMode="contains">
<p:commandLink ajax="false" id="pict" action="#{mBDetallesPersona.detallesPersona(item.idPersona)}" onmouseover="showPersonDetail();">
<h:outputText value="#{item.nombre} #{item.nombre2} #{item.apellidos} #{item.apellidos2}"/>
</p:commandLink>
</p:column>
</p:dataTable>
<p:remoteCommand id="rcomperson" name="showPersonDetail" process="#this"
actionListener="#{listadoPersonasMB.showPersonDetail}"
update=":form2:perDeta" oncomplete="PF('carOP').show('#{component.clientId}')"
/>
<p:overlayPanel widgetVar="carOP" showEffect="fade" hideEffect="fade">
<p:outputPanel id="perDeta">
<ui:include src="perDialog.xhtml" />
</p:outputPanel>
</p:overlayPanel>
I finally found the solution with overlayPanel, remoteCommand and some javascript code
xhtml
<p:dataTable filterDelay="700" reflow="true" emptyMessage="No se encontraron elementos" widgetVar="tblist"
id="dataTableList" var="item" paginator="true" paginatorPosition="bottom"
rows="10" rowKey="#{item.idPersona}" value="#{listadoPersonasMB.listPersonas}" filteredValue="#{listadoPersonasMB.filterlistPersonas}">
<f:facet name="header">
Resultado de la búsqueda #{listadoPersonasMB.cantPerFilter}
</f:facet>
<p:column style="width: 50px">
<p:graphicImage style="border-radius: 50%" alt="image" id="fotoPer" value="#{listadoPersonasMB.fotoPersonaTabla}" cache="false" width="100%">
<f:param name="personId" value="#{item.idPersona}" />
</p:graphicImage>
</p:column>
<p:column width="100%" headerText="Nombre y apellidos" sortBy="#{item.nombre}" style="text-align: left" filterBy="#{item.nombre} #{item.nombre2} #{item.apellidos} #{item.apellidos2}" filterMatchMode="contains">
<p:commandLink ajax="false" action="#{mBDetallesPersona.detallesPersona(item.idPersona)}" onmouseover="loadPersonDetalles('#{component.clientId}', #{item.idPersona});">
<h:outputText value="#{item.nombre} #{item.nombre2} #{item.apellidos} #{item.apellidos2}"/>
</p:commandLink>
</p:column>
<p:column headerText="Edad" sortBy="#{item.calculaEdad()}" style="text-align: right; width: 60px" sortFunction="#{utilMB.ordenarEnteros}">
<h:outputText value="#{item.calculaEdad()}"/>
</p:column>
<p:column filterBy="#{item.miembro eq true ? 'Sí' : 'No'}" headerText="Membresía" filterMatchMode="contains" style="width: 220px">
<h:outputText value="#{item.miembro eq true ? 'Sí' : 'No'}" />
<span> <p:commandButton rendered="#{mBLogin.siTieneFuncion('AdminPersonas')}" icon="fa fa-refresh Fs16 white" title="Cambiar"
actionListener="#{listadoPersonasMB.selectToSetMember(item)}" oncomplete="PF('newDialog').show()"/> </span>
</p:column>
<p:column style="text-align: center; width: 40px" rendered="#{mBLogin.siTieneFuncion('AdminPersonas')}">
<p:commandButton icon="fa fa-pencil Fs16 white" title="Modificar"
action="#{listadoPersonasMB.cargaPersonaModi(item.idPersona)}"/>
</p:column>
<p:column style="text-align: center; width: 40px" rendered="#{mBLogin.siTieneFuncion('AdminPersonas')}">
<p:commandButton icon="fa fa-trash Fs16 white" title="Dar Baja"
action="#{mBDeletePersona.detallesPersona(item.idPersona)}"/>
</p:column>
</p:dataTable>
<p:remoteCommand name="showPersonDetail" process="#this"
actionListener="#{listadoPersonasMB.showPersonDetail}"
update=":form2:perDeta" oncomplete="loadPanel();"/>
<p:overlayPanel widgetVar="carOP" my="right bottom" showEffect="fade" hideEffect="fade"
dismissable="true" hideEvent="onmouseout"
dynamic="true"
>
<p:outputPanel id="perDeta">
<ui:include src="perDialog.xhtml" />
</p:outputPanel>
</p:overlayPanel>
javascript code, send param to remoteCommand
<h:outputScript id="waypointScript" target="body">
var idcom = 0;
function loadPersonDetalles(idC, idPer) {
showPersonDetail([{name:'x', value:idPer}, {name:'y', value:20}]);
idcom = idC;
}
function loadPanel() {
PF('carOP').show(idcom);
}
</h:outputScript>
backingbean
public void showPersonDetail() {
try {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
this.idPerSeledtToDialog = Integer.valueOf(params.get("x"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Error: filterListener() " + e.getMessage());
}
}

Wrong row selected for edit after applying filter in prime faces 5.2

i have problem in row selection after applying default filter using primefaces 5.2 and jsf 2.2
Primefaces default filter work fine but when i select row to edit i got a wrong row .
My code is attached below :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="../template/template.xhtml">
<ui:define name="container clearfix">
<ui:insert>
<h:form id="Type_Utilisateur_Form">
<p:commandLink action="#{typeUtilisateurBean.create()}"
oncomplete="PF('popAddTypeUtilisateur').show();"
style="font-size: 15px;">
Add user type
</p:commandLink>
<p:outputPanel id="Type_Utilisateur_List">
<p:dataTable var="typeUtilisateur"
value="#{typeUtilisateurBean.listTypeUtilisateur}"
widgetVar="Type_Utilisateur_Table" rows="3" paginator="true"
rendered="true" emptyMessage="No records found with given criteria"
>
<f:facet name="header">
<p:outputPanel >
<p:inputText id="globalFilter"
onkeyup="PF('Type_Utilisateur_Table').filter()"
style="width:150px" placeholder="Search fields" />
</p:outputPanel>
</f:facet>
<p:column headerText="User Type designation"
sortBy="#{typeUtilisateur.designationTypeUtilisateur}"
filterBy="#{typeUtilisateur.designationTypeUtilisateur}"
filterStyle="display:none">
<h:outputText
value="#{typeUtilisateur.designationTypeUtilisateur}" />
</p:column>
<p:column headerText="Actions" style="width:120px">
<p:outputPanel>
<p:commandLink action="#{typeUtilisateurBean.getRow()}"
oncomplete="PF('popEditTypeUtilisateur').show();">
<p:graphicImage value="../template/images/file_edit.png"
width="48" height="48"></p:graphicImage>
</p:commandLink>
<p:commandLink action="#{typeUtilisateurBean.delete}"
update=":Type_Utilisateur_Form:Type_Utilisateur_List"
style="float: right;">
<p:graphicImage value="../template/images/file_delete.png"
width="48" height="48"></p:graphicImage>
</p:commandLink>
</p:outputPanel>
</p:column>
</p:dataTable>
</p:outputPanel>
</h:form>
<p:dialog widgetVar="popEditTypeUtilisateur" modal="true"
header="EDIT">
<p:ajax event="close"
update=":Type_Utilisateur_Form:Type_Utilisateur_List" />
<p:outputPanel autoUpdate="true">
<h:form>
<p:panelGrid columns="2" layout="grid">
<h:outputLabel value="User Type designation :" />
<p:inputText id="designationTypeUtilisateur"
value="#{typeUtilisateurBean.typeUtilisateur.designationTypeUtilisateur}" />
</p:panelGrid>
<p:commandButton value="Save" action="#{typeUtilisateurBean.save}"
oncomplete="if (!args.validationFailed) PF('popEditTypeUtilisateur').hide();" />
<p:commandButton value="Cancel"
action="#{typeUtilisateurBean.init}"
oncomplete="PF('popEditTypeUtilisateur').hide();" />
</h:form>
</p:outputPanel>
</p:dialog>
<p:dialog widgetVar="popAddTypeUtilisateur" modal="true" header="Add">
<p:ajax event="close"
update=":Type_Utilisateur_Form:Type_Utilisateur_List" />
<p:outputPanel autoUpdate="true">
<h:form>
<p:panelGrid columns="2" layout="grid">
<h:outputLabel value=" User
Type designation :" />
<p:inputText id="designationTypeUtilisateur"
value="#{typeUtilisateurBean.typeUtilisateur.designationTypeUtilisateur}" />
</p:panelGrid>
<p:commandButton value="Save" action="#{typeUtilisateurBean.save}"
oncomplete="if (!args.validationFailed) PF('popAddTypeUtilisateur').hide();" />
<p:commandButton value="Cancel"
action="#{typeUtilisateurBean.init}"
oncomplete="PF('popAddTypeUtilisateur').hide();" />
</h:form>
</p:outputPanel>
</p:dialog>
</ui:insert>
</ui:define>
</ui:composition>
Here we go with primefaces example:
What is important:
in this code set editable="true"
p:ajax event="rowEdit" and p:ajax event="rowEditCancel" both listeners
f:facet name="output" and f:facet name="input" for display, and
edit a column with p:rowEditor
CODE .xhtml:
<p:dataTable id="cars1" var="car" value="#{dtEditView.cars1}" editable="true" style="margin-bottom:20px">
<p:ajax event="rowEdit" listener="#{dtEditView.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{dtEditView.onRowCancel}" update=":form:msgs" />
<p:column headerText="Id">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.id}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{car.id}" style="width:100%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Year">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.year}" /></f:facet>
<f:facet name="input"><p:inputText value="#{car.year}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Brand">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.brand}" /></f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{car.brand}" style="width:100%">
<f:selectItems value="#{dtEditView.brands}" var="man" itemLabel="#{man}" itemValue="#{man}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Color">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.color}" /></f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{car.color}" style="width:100%">
<f:selectItems value="#{dtEditView.colors}" var="color" itemLabel="#{color}" itemValue="#{color}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
in my table i have column named actions that contains two icons one for update row and the other for delete row.I like to work like that.I have also iput text in order to filter my table and it's work.but if the function PF('carsTable').filter() is called i cannot get the right row selected !
IistUsers.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="../template/template.xhtml">
<ui:define name="container clearfix">
<ui:insert>
<h:form id="Type_Utilisateur_Form">
<p:commandLink action="#{typeUtilisateurBean.create()}"
oncomplete="PF('popAddTypeUtilisateur').show();"
style="font-size: 15px;">
Add user type
</p:commandLink>
<p:outputPanel id="Type_Utilisateur_List" rendered="true">
<p:dataTable var="typeUtilisateur"
value="#{typeUtilisateurBean.listTypeUtilisateur}"
widgetVar="Type_Utilisateur_Table" rows="3" paginator="true"
rendered="true" emptyMessage="No records found with given criteria"
filteredValue="#{typeUtilisateurBean.filteredTypeUtilisateur}"
id="Type_Utilisateur_Table"
>
<f:facet name="header">
<p:outputPanel autoUpdate="true">
<p:inputText id="globalFilter"
onkeyup="PF('Type_Utilisateur_Table').filter()"
style="width:150px" placeholder="Search fields" />
</p:outputPanel>
</f:facet>
<p:column headerText="User Type designation"
sortBy="#{typeUtilisateur.designationTypeUtilisateur}"
filterBy="#{typeUtilisateur.designationTypeUtilisateur}"
filterStyle="display:none" filterMatchMode="contains">
<h:outputText
value="#{typeUtilisateur.designationTypeUtilisateur}" />
</p:column>
<p:column headerText="Actions" style="width:120px">
<p:outputPanel>
<p:commandLink action="#{typeUtilisateurBean.getRow()}"
oncomplete="PF('popEditTypeUtilisateur').show();"
>
<p:graphicImage value="../template/images/file_edit.png"
width="48" height="48"></p:graphicImage>
</p:commandLink>
<p:commandLink action="#{typeUtilisateurBean.delete}"
update=":Type_Utilisateur_Form:Type_Utilisateur_List"
style="float: right;">
<p:graphicImage value="../template/images/file_delete.png"
width="48" height="48"></p:graphicImage>
</p:commandLink>
</p:outputPanel>
</p:column>
</p:dataTable>
</p:outputPanel>
</h:form>
<p:dialog widgetVar="popEditTypeUtilisateur" modal="true"
header="EDIT">
<p:ajax event="close"
update=":Type_Utilisateur_Form:Type_Utilisateur_List" />
<p:outputPanel autoUpdate="true">
<h:form>
<p:panelGrid columns="2" layout="grid">
<h:outputLabel value="User Type designation :" />
<p:inputText id="designationTypeUtilisateur"
value="#{typeUtilisateurBean.typeUtilisateur.designationTypeUtilisateur}" />
</p:panelGrid>
<p:commandButton value="Save" action="#{typeUtilisateurBean.save}"
oncomplete="if (!args.validationFailed) PF('popEditTypeUtilisateur').hide();" />
<p:commandButton value="Cancel"
action="#{typeUtilisateurBean.init}"
oncomplete="PF('popEditTypeUtilisateur').hide();" />
</h:form>
</p:outputPanel>
</p:dialog>
<p:dialog widgetVar="popAddTypeUtilisateur" modal="true" header="Add">
<p:ajax event="close"
update=":Type_Utilisateur_Form:Type_Utilisateur_List" />
<p:outputPanel autoUpdate="true">
<h:form>
<p:panelGrid columns="2" layout="grid">
<h:outputLabel value=" User
Type designation :" />
<p:inputText id="designationTypeUtilisateur"
value="#{typeUtilisateurBean.typeUtilisateur.designationTypeUtilisateur}" />
</p:panelGrid>
<p:commandButton value="Save" action="#{typeUtilisateurBean.save}"
oncomplete="if (!args.validationFailed) PF('popAddTypeUtilisateur').hide();" />
<p:commandButton value="Cancel"
action="#{typeUtilisateurBean.init}"
oncomplete="PF('popAddTypeUtilisateur').hide();" />
</h:form>
</p:outputPanel>
</p:dialog>
</ui:insert>
</ui:define>
</ui:composition>
TypeUtilisateurBean:
package tn.com.bj.presentation;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.model.ListDataModel;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import tn.com.bj.entities.TypeUtilisateur;
import tn.com.bj.service.ITypeUtilisateurService;
#Controller("typeUtilisateurBean")
#Scope("session")
#ViewScoped
public class TypeUtilisateurBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Autowired
#Qualifier("typeutilisateurService")
private ITypeUtilisateurService typeUtilisateurService;
private TypeUtilisateur typeUtilisateur = new TypeUtilisateur();
private ListDataModel listTypeUtilisateur;
private List<TypeUtilisateur> filteredTypeUtilisateur;
#PostConstruct
public void init() {
listTypeUtilisateur = new ListDataModel();
listTypeUtilisateur.setWrappedData(typeUtilisateurService
.findAll(TypeUtilisateur.class));
}
public void create() {
typeUtilisateur = new TypeUtilisateur();
}
public void getRow() {
typeUtilisateur = (TypeUtilisateur) listTypeUtilisateur.getRowData();
}
public void delete() {
getRow();
typeUtilisateurService.deleteService(typeUtilisateur);
listTypeUtilisateur.setWrappedData(typeUtilisateurService
.findAll(TypeUtilisateur.class));
}
public void save() {
typeUtilisateur = typeUtilisateurService
.saveOrUpdateService(typeUtilisateur);
init();
}
public ITypeUtilisateurService getTypeUtilisateurService() {
return typeUtilisateurService;
}
public void setTypeUtilisateurService(
ITypeUtilisateurService typeUtilisateurService) {
this.typeUtilisateurService = typeUtilisateurService;
}
public TypeUtilisateur getTypeUtilisateur() {
return typeUtilisateur;
}
public void setTypeUtilisateur(TypeUtilisateur typeUtilisateur) {
this.typeUtilisateur = typeUtilisateur;
}
public ListDataModel getListTypeUtilisateur() {
return listTypeUtilisateur;
}
public void setListTypeUtilisateur(ListDataModel listTypeUtilisateur) {
this.listTypeUtilisateur = listTypeUtilisateur;
}
public List<TypeUtilisateur> getFilteredTypeUtilisateur() {
return filteredTypeUtilisateur;
}
public void setFilteredTypeUtilisateur(
List<TypeUtilisateur> filteredTypeUtilisateur) {
this.filteredTypeUtilisateur = filteredTypeUtilisateur;
}
}

setting a value in a form with a default value in primefaces

I am working with Primefaces 5.1, I have a form in which there are columns with values #{circuitController.newCircuit.project}.
For this column, I want to set a default value from #{projectController.selectedProject} without asking the user to write a text.
How can I do that ?
<h:form id="newUserForm">
<p:panelGrid id="displayNewUser" columns="2" cellpadding="4"
style="margin:0 auto;">
<f:facet name="header">
selected project : #{projectController.selectedProject.designation}
</f:facet>
<h:outputText value="Designation :" styleClass="title-text" />
<p:inputText id="newCircuitDesignation"
value="#{circuitController.newCircuit.designation}" required="true" />
<h:outputText value="Description :" styleClass="title-text" />
<p:inputText id="newCircuitDescription"
value="#{circuitController.newCircuit.description}" required="true" />
<h:outputText value="Start date :"
styleClass="title-text" />
<p:calendar id="newCircuitend"
value="#{circuitController.newCircuit.date_deb}"
enableManualInput="true" />
<h:outputText value="End Date:" styleClass="title-text" />
<p:calendar id="newCircuitstart"
value="#{circuitController.newCircuit.date_fin}"
enableManualInput="true" />
<h:outputText value="Circuit designer:" />
<p:selectOneMenu id="user" required="true" effect="fold"
editable="true" value="#{circuitController.newCircuit.user}" var="user"
itemValue="#{user}"
converter="project.management.converter.UserConverter">
<f:attribute name="collectionType" value="java.util.ArrayList" />
<f:selectItems value="#{designersSelectItems.values}" />
<p:column>
<h:outputText value="#{user.identity}" rendered="true"
styleClass="data-text" />
</p:column>
</p:selectOneMenu>
<h:outputText value="project :" />
<p:inputText id="newCircuitpro"
value="#{circuitController.newCircuit.project}" var="project" converter="project.management.converter.ProjectConverter"/>
<f:facet name="footer">
<p:commandButton value="Submit" update=":form1:circuit1, :growl"
oncomplete=" handleSubmitRequest(xhr, status, args, 'newUserDlg','newUserForm');"
actionListener="#{circuitController.addCircuit()}" icon="ui-save" />
<p:commandButton type="reset" value="Reset" icon="ui-reset">
</p:commandButton>
</f:facet>
</p:panelGrid>
</h:form>
You can use a #PostConstruct method to set the value before to render the view.
public class CircuitController {
#ManagedProperty(value="#{projectController}")
private ProjectController projectController;
/* Getter and setter for projectController */
/* And other properties of CircuitController */
#PostConstruct
public void init() {
newCircuit.setProject(projectController.getSelectedProject());
}
}

Primefaces and liferay: Single select not working

I'm following the example given at http://www.primefaces.org/showcase/ui/datatableRowSelectionSingle.jsf
I'm using this as a portlet in Liferay. Here is my code:
The HTML:
<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:p="http://primefaces.org/ui">
<h:form id="form">
<h:panelGrid columns="1" cellpadding="10" width="100%">
<p:dataTable id="campaignTable" var="data"
value="#{campaign.allCampaigns}" selectionMode="single"
rowKey="#{data.campaignId}" selection="#{campaign.selectedCampaign}">
<f:facet name="header">
<p:commandButton type="button" value="Create Campaign" icon="ui-icon-document" update=":form:createForm" onclick="createDialog.show()" />
</f:facet>
<p:column sortBy="#{data.campaignId}">
<f:facet name="header">ID</f:facet>
<h:outputText value="#{data.campaignId}" />
</p:column>
<p:column sortBy="#{data.carrier}">
<f:facet name="header">Carrier</f:facet>
<h:outputText value="#{data.carrier}" />
</p:column>
<p:column sortBy="#{data.shortCode}">
<f:facet name="header">Short Code</f:facet>
<h:outputText value="#{data.shortCode}" />
</p:column>
<p:column sortBy="#{data.sendType}">
<f:facet name="header">Send Type</f:facet>
<h:outputText value="#{data.sendType}" />
</p:column>
<p:column sortBy="#{data.startDate}">
<f:facet name="header">Start Date</f:facet>
<h:outputText value="#{data.startDate.time}">
<f:convertDateTime pattern="MM/dd/yyyy hh:mm a" />
</h:outputText>
</p:column>
<p:column sortBy="#{data.endDate}">
<f:facet name="header">End Date</f:facet>
<h:outputText value="#{data.endDate.time}">
<f:convertDateTime pattern="MM/dd/yyyy hh:mm a" />
</h:outputText>
</p:column>
<p:column sortBy="#{data.messageText}">
<f:facet name="header">Text Message</f:facet>
<h:outputText value="#{data.messageText}" />
</p:column>
<p:column sortBy="#{data.serviceId}">
<f:facet name="header">Service ID</f:facet>
<h:outputText value="#{data.serviceId}" />
</p:column>
<p:column sortBy="#{data.responseKeyword}">
<f:facet name="header">Response Keyword</f:facet>
<h:outputText value="#{data.responseKeyword}" />
</p:column>
</p:dataTable>
</h:panelGrid>
<p:dialog id="createDialog" header="Create Campaign" widgetVar="createDialog">
<h:panelGrid id="createForm" columns="2" cellpadding="5">
<h:outputLabel for="campaignId" value="Campaign ID:" />
<p:inputText value="#{campaign.selectedCampaign.campaignId}" id="campaignId" required="true" label="Campaign ID" />
<h:outputLabel for="startDate" value="Start Date:" />
<p:inputText value="#{campaign.selectedCampaign.startDate}" id="startDate" required="true" label="Start Date" />
<f:facet name="footer">
<p:commandButton id="createCampaignButton" value="Create" update=":growl" actionListener="#{campaign.createCampaign()}" />
</f:facet>
</h:panelGrid>
</p:dialog>
</h:form>
</ui:composition>
And the backing bean:
#ManagedBean
#ViewScoped
public class Campaign implements Serializable {
private List<CampaignBean> allCampaigns;
private CampaignBean selectedCampaign;
public Campaign() {
MyProxy proxy = new MyProxy();
try {
CampaignsResponse response = proxy.getAllCampaigns();
if (response != null) {
setAllCampaigns(new ArrayList<CampaignBean>(Arrays
.asList(response.getCampaigns())));
}
} catch (RemoteException e) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Error",
"Problem listing campaigns"));
}
}
public void createCampaign() {
}
/* Getters and Setters */
public List<CampaignBean> getAllCampaigns() {
return allCampaigns;
}
public void setAllCampaigns(List<CampaignBean> allCampaigns) {
this.allCampaigns = allCampaigns;
}
public CampaignBean getSelectedCampaign() {
return selectedCampaign;
}
public void setSelectedCampaign(CampaignBean selectedCampaign) {
this.selectedCampaign = selectedCampaign;
}
}
The form is not populated with the selected row values!
I finally figured out what the problem was. The entire table was inside a "TabView" component. I was using the wrong selector i.e. instead of using update=":form:createForm" I should have used update=":tabView:form:createForm" and remove the type="button" declaration.