How to set values into a selectradiobutton from a managed bean? - jsf

I´ve a datatable that has a column with selectoneradio, so I´m supposed to turn on the radios if they are on in my database. So I recover my data but I can´t figured how to tell my selectoneradio buttons to be on when my users consult the page with the table. Here is the code from my table:
<p:dataTable id="secciones" var="secciones" value="#{seccionesMB.seccionesList}" style="width: 60%;">
<p:column>
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<h:outputText value="#{secciones.codigo}" />
<f:facet name="header" >
<h:outputText value="Seccion" />
</f:facet>
<h:outputText value="#{secciones.seccion}" />
</p:column>
<p:column>
<f:facet name="header">
<div align="left"> <h:outputText value="Alta Baja Consulta Mod N/A" /></div>
</f:facet>
<p:selectOneRadio id="options" value="#{secciones.nivel}" disabled="true">
<f:selectItem itemLabel="10000" itemValue="10000" />
<f:selectItem itemLabel="01000" itemValue="01000" />
<f:selectItem itemLabel="00100" itemValue="00100" />
<f:selectItem itemLabel="00010" itemValue="00010" />
<f:selectItem itemLabel="00001" itemValue="00001" />
</p:selectOneRadio>
</p:column>
</p:dataTable>
and here is the method from my backing bean that feeds my datatable:
public String consultarPerfil(int id, String perfil) {
this.setId(id);
this.setPerfil(perfil);
try {
List<Perfiles_Secciones> list = getPerfiles_SeccionesService().getPerfiles_Seccion(id);
int i = 0;
for(SeccionesDTO seccionDTO : seccionesList) {
for(Perfiles_Secciones ps : list) {
if (seccionDTO.getId() == ps.getId_seccion()) {
System.out.println(seccionDTO.getId() + " - " + ps.getId_seccion());
seccionDTO.setNivel(ps.getNivel());
}
i++;
}
i = 0;
}
for(SeccionesDTO seccionDTO : seccionesList) {
System.out.println(seccionDTO.getNivel());
}
} catch (Exception ex) {
Logger.getLogger(PerfilesManagedBean.class.getName()).log(Level.SEVERE, null, ex);
}
return CONSULTAR_PERFIL;
}
What do I need to do in order to feed my selectoneradio from my managed bean??

Related

How to dynamically disable/enable commandbutton in datatable in JSF/PrimeFaces

I am displaying list of some objects in primefaces datatable. Apart from object related columns, I have added two columns that contain commandbutton. Each of these commandbutton make ajax calls to desired methods in bean. I want to disable both commandbuttons in that row when any of the button is clicked and enable them again once action is completed successfully. The method execution is simulated by ajaxStatus which is working fine.
Following is selected code of xhtml
<h:form id="form1" >
<p:remoteCommand name="onload" action="#{controlBean.init}" autoRun="true" />
<h:outputtext>Sample Project</h:outputtext>
<p:ajaxStatus style="display:block;margin-bottom:2em;height:24px;">
<f:facet name="prestart">
<h:outputText value="Starting..." /> </f:facet>
<f:facet name="error"> <h:outputText value="Error" />
</f:facet>
<f:facet name="success"> <h:outputText value="Success" />
</f:facet>
<f:facet name="default"> <h:outputText value="Idle" />
</f:facet>
<f:facet name="start"> <h:outputText value="Please Wait" />
<p:graphicImage name="/images/ajaxloadingbar.gif" />
</f:facet>
</p:ajaxStatus>
<p:dataTable var="appl" id="tbl1" rowIndexVar="rowIndx" value="#{controlBean.applsList}">
<p:column headerText="Name">
<h:outputText value="#{appl.applName}" />
</p:column>
<p:column headerText="Type">
<h:outputText value="#{appl.applType}" />
</p:column>
<p:column headerText="Desc" rendered="true">
<h:outputText value="#{appl.applDesc}" />
</p:column>
<p:column headerText="Start Appl" id="col4">
<p:commandButton value="Submit" ajax="true"
process="#this"
widgetVar="startButtonVar"
id="startBtn" update="msgs, col4, startBtn"
action="#{controlBean.startAction}" style="margin-right:20px;"
styleClass="ui-priority-primary"
disabled="#{controlBean.startBtnDisabled}">
<f:setPropertyActionListener value="#{appl}" target="#{controlBean.selectedAppl}"/>
</p:commandButton>
</p:column>
<p:column headerText="Stop Appl" id="col5">
<p:commandButton value="Submit" ajax="true"
process="#this"
widgetVar="stopButtonVar"
id="stopBtn" update="msgs, col5"
action="#{controlBean.stopAction}" style="margin-right:20px;"
styleClass="ui-priority-primary"
disabled="#{controlBean.btnDisabled}">
<f:setPropertyActionListener value="#{appl}" target="#{controlBean.selectedAppl}"/>
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
backing bean methods are as under:
public void startAction()
{
System.out.println("In start action method");
this.btnDisabled = true;
for(int i=0;i<100000;i++)
{
if(i%10000 == 0)
{
System.out.println(i);
}
for(int j=0;j<10000;j++)
{
for(int k=0;k<1000;k++)
{
}
}
}
MessageUtils.info(getSelectedAppl().getApplName()+" has been started");
this.btnDisabled = false;
}
public void stopAction()
{
System.out.println("In stop action method");
this.btnDisabled = true;
for(int i=0;i<100000;i++)
{
if(i%10000 == 0)
{
System.out.println(i);
}
for(int j=0;j<10000;j++)
{
for(int k=0;k<1000;k++)
{
}
}
}
MessageUtils.info(getSelectedAppl().getApplName()+" has been stopped");
this.btnDisabled = false;
}
Everything is working fine except disabling/enabling of commandbuttons. Any help in this regard will be highly appreciated.
Regards
You could disable them on the onclick javascript handler:
<p:commandButton id="startBtn"
widgetVar="startButtonVar"
onclick="PF('startButtonVar').disable();"
.....
The onclick will be executed BEFORE the AJAX call. On return, as the button is updated, it will be reenabled.

Filtered jsf outputtext

I would like to know how to filter so that only the patents of vehicles that are available appear.
<p:column headerText="Patent car">
<h:outputText value="#{v.fkcar.patent}"/>
</p:column>
In the database there is a table called State with 2 income
id_state 1 ---- name_state Available and id_state 2 ---- name_state Not available
What I need is that only the patents of the vehicles that have the State in Available are shown
The State table has its respective Bean
I hope you can help me: D
Entry code Rent
<h:head>
<title>Realizar Alquiler</title>
<h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
<h2>Realizar Alquiler</h2>
<h:form id="fv">
<h:panelGrid columns="1" styleClass="panelGridCenter">
<h:outputText value="Precio"/>
<p:inputText value="#{alquilerBean.precio}" required="true"/>
<p:outputLabel for="datetime" value="Fecha" />
<p:calendar id="datetime" value="#{alquilerBean.fecha3}" pattern="MM/dd/yyyy HH:mm:ss" required="true"/>
Cliente
<p:selectOneMenu value="#{alquilerBean.cliente.idCliente}">
<f:selectItems value="#{clienteBean.cliente}" var="e" itemLabel="#{e.rut}" itemValue="#{e.idCliente}"/>
</p:selectOneMenu>
Patente
<p:selectOneMenu value="#{alquilerBean.vehiculo.idVehiculo}">
<f:selectItems value="#{vehiculoBean.vehiculos}" var="o" itemLabel="#{o.patente}" itemValue="#{o.idVehiculo}"/>
</p:selectOneMenu>
¿Entregado?
<p:selectOneMenu value="#{alquilerBean.entregado.idEntregado}">
<f:selectItems value="#{entregadoBean.entregado}" var="o" itemLabel="#{o.nombreEntregado}" itemValue="#{o.idEntregado}"/>
</p:selectOneMenu>
<br/>
<h:outputText value=""/>
<p:commandButton actionListener="#{alquilerBean.crear()}" value="Agregar Alquiler" update="fv"/>
</h:panelGrid>
<br/>
<p:dataTable value="#{alquilerBean.alquiler}" var="v" emptyMessage="No hay registros" >
<p:column headerText="Fecha">
<h:outputText value="#{v.fecha}"/>
</p:column>
<p:column headerText="Precio">
<h:outputText value="#{v.precio}"/>
</p:column>
<p:column headerText="Entregado">
<h:outputText value="#{v.fkEntregado.nombreEntregado}"/>
</p:column>
<p:column headerText="Rut Cliente">
<h:outputText value="#{v.fkCliente.rut}"/>
</p:column>
<p:column headerText="Patente Auto"
<h:outputText value="#{v.fkVehiculo.patente}"/>
</p:column>
</p:column>
<f:facet name="footer">
Se han alquilado #{alquilerBean.alquiler.size()} Vehiculos.
</f:facet>
</p:dataTable>
</h:form>
<br/>
<h:form>
<h:commandLink action="index">
<h:graphicImage value="resources/images/atras2.png" />
</h:commandLink>
</h:form>
</h:body> </html>
Here is the state bean code.
private int id_entregado;
private String nombre_entregado;
public EntregadoBean() {
}
public String crear(){
Entregado e = new Entregado();
e.setNombreEntregado(nombre_entregado);
entregadoFacade.create(e);
return "#";
}
public List<Entregado> getEntregado(){
return entregadoFacade.findAll();
}
public EntregadoFacadeLocal getEntregadoFacade() {
return entregadoFacade;
}
public void setEntregadoFacade(EntregadoFacadeLocal entregadoFacade) {
this.entregadoFacade = entregadoFacade;
}
public int getId_entregado() {
return id_entregado;
}
public void setId_entregado(int id_entregado) {
this.id_entregado = id_entregado;
}
public String getNombre_entregado() {
return nombre_entregado;
}
public void setNombre_entregado(String nombre_entregado) {
this.nombre_entregado = nombre_entregado;
}
}

how to display dynamic column names in UI using p:columns tag?

I am using Hibernate and jsf with primefaces.I got list of data and it is displayed on console. but i am unable to display data throgh UI using <p:columns> Tag. My Xhtml page having one drop-down box and check-box. My datacolumn should be display based on check-box. If checkbox value is true then display all columns otherwise should be display selected columns dynamically.
XHTML Page:
<p:dataTable id="tbl" var="dept" rendered="#{deptBean.visible}"
value="#{deptBean.getDepusers1()}"
scrollRows="20" scrollable="false" liveScroll="false"
scrollHeight="340" mode="inline"
styleClass="report-table"
rowClasses="report-table-odd-row, report-table-even-row">
<f:facet name="header">
<h:outputText value="Save as " class="reportSubHeadingFont"/>
<h:commandLink>
<p:graphicImage value="resources/images/csv.png" />
<p:dataExporter type="csv" target="tbl" fileName="Department_Users" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="resources/images/excel.png" />
<p:dataExporter type="xls" target="tbl" fileName="Department_Users" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="resources/images/xml.png" />
<p:dataExporter type="xml" target="tbl" fileName="Department_Users" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="resources/images/pdf.png" />
<p:dataExporter type="pdf" target="tbl" fileName="Department_Users" />
</h:commandLink>
</f:facet>
<p:columns value="#{deptBean.dataTableColumns}" var="column" columnIndexVar="colIndex" sortBy="#{dept[column.property]}">
<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>
<h:outputText value="#{dept[column.property]}" />
</p:columns>
</p:dataTable>
Bean Class:
public List executeQuery() {
try
{
dataTableColumns = new ArrayList<DataTableColumn>();
setVisible(true);
log = Logger.getLogger(this.getClass());
System.out.println("selectedcheckbox" + checkboxvalue);
System.out.println("selectd dep user " + selectddep_user);
int deptID = Integer.parseInt(selectddep_user);
DepartmentDao dedao=new DepartmentDao();
HttpSession httpsession= Util.getSession();
String role= (String)httpsession.getAttribute("role");
if (checkboxvalue.equals("true")) {
depusersDetailGrid = new UserDAO().getUsersWithDetailsByDeptId(deptID);
System.out.println("depusersDetailGrid generated");
for (DepartmentUsersDetailedGrid ss : depusersDetailGrid) {
System.out.println(ss.getFirstName());
System.out.println(ss.getLoginId());
System.out.println(ss.getEmail());
System.out.println(ss.getMobileNo());
System.out.println(ss.getRoleId());
}
} else {
depusersGrid = new UserDAO().getAllUsers_Department();
for (DepartmentUsersGrid ad : depusersGrid) {
System.out.println(ad.getDeptName());
System.out.println(ad.getNoOfUsers());
}
}
depusers1.add(depusersDetailGrid);
depusers1.add(depusersGrid);
}catch(Exception e)
{
e.printStackTrace();
log.error("Exception occured in ",e);
}
return depusers1;
}
}

Getting duplicate rows with row edit event in primefaces

I am Using Prime faces 3.5.8,I am getting duplicate rows with row edit event.I am getting my row count as fine but one record is showing twice instead of other.This I am getting when call my clintList to load.When I close dialog and open it it is fine,Even I checked my list It is getting right data,Thanks in Advance..
My BeanCode for edit,
public void onClientEdit(RowEditEvent event) {
try {
Client edtClient = (Client) event.getObject();
if (null == edtClient.getClientName()
|| edtClient.getClientName() == "") {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
CommonStringUtil.getUIString("clintNameEmpty"),
CommonStringUtil.getUIString("clintNameEmpty")));
getAgencyClients();
return;
}
HibernateUtil.beginTransaction();
Agency tempAgency = agencyHome.findById(agency.getAgencyId());
Hibernate.initialize(tempAgency.getClients());
HibernateUtil.commitTransaction();
for (Client cl : (Set<Client>) tempAgency.getClients()) {
if (cl.getClientName().equalsIgnoreCase(
edtClient.getClientName().trim())) {
log.debug("Client Name Already exists");
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
CommonStringUtil
.getUIString("clientNameExists"),
CommonStringUtil
.getUIString("clientNameExists")));
getAgencyClients();
return;
}
}
HibernateUtil.beginTransaction();
Client eClient = clientHome.findById(edtClient.getClientId());
eClient.setClientName(edtClient.getClientName());
clientHome.attachDirty(eClient);
clientName = null;
agencyHome.attachDirty(agency);
HibernateUtil.commitTransaction();
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO, CommonStringUtil
.getUIString("clientNameEditSucc"), CommonStringUtil
.getUIString("clientNameEditSucc")));
} catch (Exception e) {
if (HibernateUtil.isTransactionActive())
HibernateUtil.rollbackTransaction();
log.error("Error Updating Client Name", e);
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, CommonStringUtil
.getUIString("clientNameEditFail"), CommonStringUtil
.getUIString("clientNameEditFail")));
}
}
My View Part
<p:dialog widgetVar="clientDialogue" modal="true" id="clientdilogId"
showHeader="false" style="padding:0;">
<h:form id="clientDlgForm">
<p:toolbar style="height:38px;">
<p:toolbarGroup align="left">
<h:outputText
value="Edit Client Messages: #{adminAgencyBean.agencyName}"
styleClass="dialog-header-Text" style="line-height:38px"></h:outputText>
</p:toolbarGroup>
<p:toolbarGroup align="right" style="margin-left:50px;">
<p:inputText value="#{adminAgencyBean.clientName}" id="clientName"
maxlength="45" />
<p:spacer width="5"></p:spacer>
<p:commandButton action="#{adminAgencyBean.createClient}"
icon="ui-icon-custom-arrow" iconPos="right" value="Create"
style="margin-left:5px;margin-right:20px;" update="#form" />
<p:commandLink value="X" onclick="clientDialogue.hide()"
style="text-decoration:none;margin-right:10px;" process="#none"></p:commandLink>
</p:toolbarGroup>
</p:toolbar>
<p:messages id="clientMsgs" />
<p:scrollPanel style="width:980px;height:400px;width:100%;"
mode="native">
<p:dataTable value="#{adminAgencyBean.clientList}" var="client"
sortBy="#{client.clientName}" sortOrder="ascending" editable="true"
id="ClientTable">
<p:ajax event="rowEdit" listener="#{adminAgencyBean.onClientEdit}"
update=":clientDlgForm:clientMsgs"/>
<p:ajax event="rowEditCancel" />
<p:column headerText="Client Name" sortBy="#{client.clientName}"
style="white-space:pre-line;width:130px;">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{client.clientName}" />
</f:facet>
<f:facet name="input">
<p:inputText id="editClientName" value="#{client.clientName}"
maxlength="45" style="width:90px"></p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit Client"
style="white-space:pre-line;width:80px;">
<p:rowEditor
rendered="#{adminAgencyBean.clientTransactionMap[client.clientId] lt 1}" />
<h:outputText value="#{commonmessages.NoEdit}"
rendered="#{adminAgencyBean.clientTransactionMap[client.clientId] ge 1}" />
</p:column>
<p:column headerText="Remove Client"
style="width:85px;white-space:pre-line">
<p:commandButton value="Remove"
rendered="#{adminAgencyBean.clientTransactionMap[client.clientId] lt 1}"
action="#{adminAgencyBean.deleteClient}" update="#form"
process="#none" styleClass="blue_button">
<f:setPropertyActionListener target="#{adminAgencyBean.clientId}"
value="#{client.clientId}"></f:setPropertyActionListener>
</p:commandButton>
<h:outputText value="#{commonmessages.nodelete}"
rendered="#{adminAgencyBean.clientTransactionMap[client.clientId] ge 1}" />
</p:column>
</p:dataTable>
</p:scrollPanel>
</h:form>
</p:dialog>

selecting item from primeface's selectOneMenu not working

I'm having problem with getting the selected item from a selectOneMenu.Here is my JSF code:
<h:form id="mainfrm">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Basic Usage: " />
<p:selectOneMenu id="domaine" value="#{projet.currentDomaines}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{projet.initDomaines()}" var="d" itemValue="#{d}" itemLabel="#{d.libelleDomaine}" />
<p:ajax update="formEquipe" process="mainfrm" event="change" />
</p:selectOneMenu>
</h:panelGrid>
</h:form>
<h:form id="formEquipe">
<h:panelGrid id="display" columns="2" cellpadding="4">
<f:facet name="header">
<p:graphicImage value="/images/cars/xxxx.jpg"/>
</f:facet>
<h:outputText value="Domaine name :" />
<h:outputText value="#{projet.currentDomaines.libelleDomaine}"/>
<h:outputText value="Director :" />
<h:outputText value="#{projet.currentDomaines.nomDirecteur}" />
</h:panelGrid>
</h:form>
it seems like everything is right but i must be missing something... so i tested by changing the currentDomaines (object type Domaines) by text (String) and it worked, and here is the code :
<h:form id="mainfrm">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Basic Usage: " />
<p:selectOneMenu id="domaine" value="#{projet.text}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{projet.initDomaines()}" var="d" itemValue="#{d.libelleDomaine}" itemLabel="#{d.libelleDomaine}" />
<p:ajax update="formEquipe" process="mainfrm" event="change" />
</p:selectOneMenu>
</h:panelGrid>
</h:form>
<h:form id="formEquipe">
<h:panelGrid id="display" columns="2" cellpadding="4">
<f:facet name="header">
<p:graphicImage value="/images/cars/xxxx.jpg"/>
</f:facet>
<h:outputText value="Domaine name :" />
<h:outputText value="#{projet.text/>
</h:panelGrid>
</h:form>
and here is my backing bean:
public class ProjetsBean implements Serializable {
private DomainesService domainesService;
private Domaines currentDomaines;
private String text;
/////////////// setters & getters \\\\\\\\\\\\\\\\\\\
public void setCurrentDomaines(Domaines currentDomaines) {
this.currentDomaines=currentDomaines;
}
public Domaines getCurrentDomaines() {
return currentDomaines;
}
public void setText(String text) {
this.text=text;
}
public Integer getText() {
return text;
}
///////////////// Méthodes \\\\\\\\\\\\\\\
#PostConstruct
public List<Domaines> initDomaines() {
return domainesService.getAllDomaines();
}
}
The selection from a html selectbox will always be returned to the server as string. If you want to use objects in h:selectOneMenu you need a converter.
There is a comprehensive tutorial on that topic: "Objects in h:selectOneMenu".
For the most cases you can do it without an converter, but this is not wrong.
Here is an example:
<h:selectOneMenu value="#{bean.selectedObject.id}">
<f:selectItems value="#{bean.listOfObjects}" var="item" itemLabel="#{item.title}" itemValue="#{item.id}" />
</h:selectOneMenu>
Another idea is to use the hashcode instead of the ID (but in both: itemValue and value).

Resources