Set focus on input text with PrimeFaces - primefaces

I currently have an .xhtml that shows a pop up depending on some events in JAVA, now i need that if the pop up shows put the focus on a expecific textfield.
JAVA
public void validate(){
if(List.size()>0){
String [] codes = {"1","97","119","606","695","700","738","730"};
List<String> codesList = Arrays.asList(codes);
String coomentsString ="THIS IS A STRING";
if(codesList.contains(List.get(0).getCode())){
MessageUtil.addInfoMessage(LabelKeys.DOCBR_INFO,coomentsString);
PrimefacesUtil.showMessage();
}
}
}
XHTML
<p:commandButton id="loadPage" action="#CLASS.validate}"/>
<p:inputText id="comments"/>
<script type="text/javascript">
$( document ).ready(function() {
var name= document.getElementById('containerForm:loadPage');
name.click();
});
</script>
XHTML POP UP
<p:dialog id="growl" widgetVar="messagePopupWidget" resizable="false"
modal="true" width="270px" styleClass="messagedialog">
<p:ajax event="close" update="growl"
listener="#{messageDialog.handleCloseDialogMessageEvent}" />
<p:panelGrid style="width:100%;">
<p:row>
<p:column style="text-align:left;">
<p:messages showSummary="true" showDetail="true"
autoUpdate="false" escape="false"/>
</p:column>
</p:row>
<p:row>
<p:column style="text-align:center;">
<p:commandButton id="messageDialogButtonId" value="#{label['messageDialog.btn.ok']}" onclick="messagePopupWidget.hide();" update="growl" type="button"/>
</p:column>
</p:row>
</p:panelGrid>
</p:dialog>
now, i need that after loadPage makes the click on javascript and shows the pop up from validate(), put the cursor on "comments"

Two ways to achieve this with primefaces:
1. Include this in your dialog to set explicit focus
<p:focus for="commentsID"/>
There is an onShow attribute on the dialog, you can use that to set the focus manually
<p:dialog id="loadPage"
onShow="document.getElementById("commentsID").focus();"/>

Related

Primefaces DataTable Selection's Attribute is Null if not rendered in Dialog

I'm not sure if this is a limitation or a coding error on my part. I have a Primefaces DataTable that allows the user to select it (single select radio) and then click on a CommandButton to edit the data in a Dialog.
In the HTML code where I display the selected object, I omitted the ID, Lat, and Long attributes because a human user shouldn't be touching these attributes. The Dialog is showing the selected attributes correctly, however, when I tried to find the existing record using the selected object's ID for update, it thrown a no result error. When I checked the selected object's ID attribute, it has a value of NULL.
I tinker around with the HTML code and found that in order to have the any of the attributes copied to the selected object, the attributes must be shown in the XHTML. When I include the attribute as with Render = False, the attribute will have a value of NULL. Any suggestions on hiding attributes that human users shouldn't modify beside using Disable tag attribute?
Class Property{
Int ID;
String Address;
Long Lat;
Long Long;
String Desc;
}
public void modifyCurrentProject() {
HashMap<String, Object> params = new HashMap();
params.put("id", currentProject.getId());
//fails to find Project by ID as currentProject.getID == null
Project temp = (Project) dbu.findByNamedQuery("project.findID", params);
//Omit statements to overwrite attached object's values before writing back to database
dbu.modifyEntity(); //dbu is another object to control database access
currentProject = null;
}
<h:body>
<p:growl id="projectMsg" showDetail="true"/>
<h:form id="form">
<p:dataTable id="projectTable" var="project" value="#{projectBacking.builder.projects}" selection="#{projectBacking.currentProject}" rowKey="#{project.id}">
<f:facet name="header">My Project List
<p:commandButton value="Add" type="button" onclick="PF('projectAdd').show()"/>
<p:commandButton value="Edit" update=":editForm" oncomplete="PF('projectEdit').show()"/>
</f:facet>
<p:column selectionMode="single"/>
<p:column headerText="ID">
<h:outputText value="#{project.id}"/>
<p:column headerText="Description">
<h:outputText value="#{project.property.description}"/>
</p:column>
<p:column headerText="Street">
<h:outputText value="#{project.street}"/>
</p:column>
<p:column headerText="City">
<h:outputText value="#{project.city}"/>
</p:column>
<p:column headerText="Province">
<h:outputText value="#{project.state}"/>
</p:column>
<p:column headerText="Latitude">
<h:outputText value="#{project.latitude}"/>
</p:column>
<p:column headerText="Longitude">
<h:outputText value="#{project.longitude}"/>
</p:column>
<p:column headerText="Status">
<h:outputText value="#{project.status}"/>
</p:column>
</p:dataTable>
</h:form>
<!-- Omitted the Add Project Dialog box -->
<p:dialog header="Edit Project" modal="true" height="400" width="80%" widgetVar="projectEdit">
<h:form id="editForm">
<p:panel id="editProject" header="Edit Project" toggleable="true" closable="false">
<!--<h:inputText id="id" value="#{projectBacking.currentProject.id}"/>-->
<h:inputText id="country" value="#{projectBacking.currentProject.country}"/>
<h:inputText id="lat" rendered="true" value="#{projectBacking.currentProject.latitude}"/>
<h:inputText id="long" rendered="true" value="#{projectBacking.currentProject.longitude}"/>
<h:outputLabel for="desc" value="Description" />
<h:inputText id="desc" value="#{projectBacking.currentProject.description}"/>
<h:outputLabel for="street" value="Street" />
<h:inputText id="street" value="#{projectBacking.currentProject.street}"/>
<h:message for="street"/>
<h:outputLabel for="city" value="City:" />
<h:inputText id="city" required="false" value="#{projectBacking.currentProject.city}"/>
<h:message for="city" />
<h:outputLabel for="state" value="Province" />
<h:inputText id="state" required="true" value="#{projectBacking.currentProject.state}"/>
<h:commandButton value="Save Project" type="submit" action="#{projectBacking.modifyCurrentProject()}">
</h:commandButton>
</p:panel>
</h:form>
</p:dialog>
</h:body>
After placing some more breakpoints in the code, I discovered that upon saving the changes in the Dialog, it's creating a new backing bean. If the is set to Render=false, Disabled=true, or ReadOnly=true then new backing bean's object's value will not be set and retain the initial value as specified in the object's constructor.
Basically, the cause is incorrect scope. I was using Request scope when the problem occurred. However, changing to Session scope resolved it.

How to update value in p:dialog and display in the p:dialog

I have a datatable, and at the last column I will have an edit action, it will trigger the dialog box. However, I found the value in the dialog box is not updated. Can anybody to help on this?
My code as below. In fact the bean.currentItem.name and bean.currentItem.age should base on my selection from the table. I check the bean.currentItem is not null and is refer to my selection from preEdit method, but the value never show up in p:dialog.
<p:dataTable ....>
<p:column...
<p:column...
<p:column>
<f:facet name="header">
<h:outputLabel value="Update" />
</f:facet>
<p:remoteCommand name="preEdit" action="#{bean.preEdit}"
process="#this" update="#this #form:dlg">
<f:setPropertyActionListener target="#{bean.currentItem}"
value="#{thisItem}" />
</p:remoteCommand>
<p:commandLink styleClass="no-decor"
oncomplete="preEdit();PF('dlg').show();" value="Edit"/>
</p:column>
</p:dataTable>
<p:dialog header="#{lbl.tt_cat_upd}" widgetVar="dlg" id="dlg"
resizable="false" >
<h:outputLabel value="#{bean.currentItem.name}" />
<h:outputLabel value="#{bean.currentItem.age}" />
</p:dialog>
How about removing your p:remoteCommand and let your p:commandLink do all the work for you?
Assuming you have a h:form surrounding both your dataTable and your dialog
<p:commandLink action="#{bean.preEdit}" process="#this" update="dlg" styleClass="no-decor" oncomplete="PF('dlg').show()" value="Edit">
<f:setPropertyActionListener target="#{bean.currentItem}"
value="#{thisItem}" />
</p:commandLink>
If your dataTable is not in the same form with your dialog, you might replace update="dlg" by update=":formIdContainingDialog:dlg"

Primefaces dialog fields not filled after click in CommandLink

I am using Primefaces 3.5 with Weblogic 11. I have a page where a dialog to edit a 'profile' entity ('perfil' in Portuguese) which is called from two places. First from a commandButton to insert a new profile. The second from a dataTable to edit a specific profile. Below, I show the code:
Snippet of XHTML page
<p:fieldset legend="Pesquisa de Perfil">
<p:panelGrid columns="2">
<f:facet name="footer">
<p:commandButton id="btnPesquisar"
actionListener="#{perfilAcessoMB.pesquisar}" value="Pesquisar"
immediate="true" update="pnlPerfis" styleClass="ui-icon-search" />
<p:spacer width="20"></p:spacer>
<!-- OPEN DIALOG TO CREATE A NEW PROFILE -->
<p:commandButton id="btnIncluir" value="Incluir"
update="dlgPerfil" immediate="true"
actionListener="#{perfilAcessoMB.abrirDialogoEdicao(null)}"
oncomplete="dlgPerfil.show();">
</p:commandButton>
</f:facet>
</p:panelGrid>
</p:fieldset>
<br />
<p:outputPanel id="pnlPerfis" layout="block">
<p:fieldset id="resultadoPesquisa" legend="Resultado da Pesquisa"
rendered="#{not empty perfilAcessoMB.perfis}">
<p:dataTable id="tblPerfis" value="#{perfilAcessoMB.perfis}"
var="perfil" emptyMessage="Nenhum perfil encontrado.">
<p:column headerText="Nome do Perfil">
<h:outputText value="#{perfil.nome}"></h:outputText>
</p:column>
<p:column headerText="Descrição do Perfil">
<h:outputText value="#{perfil.descricao}"></h:outputText>
</p:column>
<p:column headerText="Situação do Perfil"
style="text-align: center; width: 100px;">
<h:outputText value="Ativo" rendered="#{perfil.situacao}" />
<h:outputText value="Inativo" rendered="#{not perfil.situacao}" />
</p:column>
<p:column headerText="Editar"
style="text-align: center; width: 50px;">
<!-- OPEN DIALOG TO EDIT A NEW PROFILE -->
<p:commandLink id="lnkEditar" immediate="true"
title="Editar Perfil" update=":formPrincipal:dlgPerfil :formPrincipal:pnlPerfilEdicao"
actionListener="#{perfilAcessoMB.abrirDialogoEdicao(perfil)}"
oncomplete="dlgPerfil.show();">
<h:outputText value="Editar" />
</p:commandLink>
</p:column>
<p:column headerText="Excluir"
style="text-align: center; width: 50px;">
</p:column>
</p:dataTable>
</p:fieldset>
</p:outputPanel>
<p:dialog id="dlgPerfil" widgetVar="dlgPerfil" resizable="false"
closable="true" modal="true" closeOnEscape="true"
header="#{(empty perfilAcessoMB.perfil)? 'Incluir': 'Editar'} Perfil">
<h:outputText value="#{perfilAcessoMB.perfil}"></h:outputText>
<h:panelGroup id="pnlPerfilEdicao" layout="block">
<p:fieldset legend="Dados do Perfil">
<p:panelGrid columns="2">
<h:outputLabel id="lblNomePerfilEdicao" value="Nome do Perfil"
for="txtNomePerfilEdicao" />
<p:inputText id="txtNomePerfilEdicao" required="true"
requiredMessage="É obrigatório preencher o campo Nome do Perfil."
value="#{perfilAcessoMB.perfil.nome}" maxlength="20" size="20"></p:inputText>
<h:outputLabel id="lblDescricaoPerfilEdicao"
value="Descrição do Perfil" for="txtDescricaoPerfilEdicao" />
<p:inputText id="txtDescricaoPerfilEdicao" required="true"
requiredMessage="É obrigatório preencher o campo Descrição do Perfil."
value="#{perfilAcessoMB.perfil.descricao}" maxlength="20"
size="20"></p:inputText>
<h:outputLabel id="lblSituacaoPerfilEdicao" value="Situação"
for="selSituacaoPerfilEdicao" />
<p:selectOneMenu id="selSituacaoPerfilEdicao"
value="#{perfilAcessoMB.perfil.situacao}">
<f:selectItems value="#{perfilAcessoMB.situacoesEdicao}" />
</p:selectOneMenu>
</p:panelGrid>
</p:fieldset>
<br />
<p:fieldset legend="Permissões">
<p:pickList id="pickFuncoes" value="#{perfilAcessoMB.funcoes}"
var="funcao" itemValue="#{funcao}"
itemLabel="#{funcao.descricao}" required="true"
requiredMessage="É obrigatório associar ao menos uma funcionalidade ao perfil."
converter="funcaoConverter" />
<p:column>#{funcao.descricao}</p:column>
</p:fieldset>
<p:panelGrid columns="2">
<p:commandButton id="btnSalvarPerfil" value="Salvar"
actionListener="#{perfilAcessoMB.salvar}"
oncomplete="if(args && !args.validationFailed) dlgPerfil.hide();" />
<p:commandButton id="btnCancelarPerfil" value="Cancelar"
immediate="true" onclick="dlgPerfil.hide();" />
</p:panelGrid>
</h:panelGroup>
</p:dialog>
MangedBean (perfilAcessoMB)
#ManagedBean
#ViewScoped
public class PerfilAcessoMB extends BaseMB {
private PerfilAcessoORM perfil;
private List<PerfilAcessoORM> perfis;
private String nomePerfil;
private Boolean situacao;
private DualListModel<FuncaoORM> funcoes;
private SelectItem[] situacoesPesquisa = new SelectItem[] {
new SelectItem(null, "Todos"), SELECT_ITEM_ATIVO,
SELECT_ITEM_INATIVO };
private SelectItem[] situacoesEdicao = new SelectItem[] {
SELECT_ITEM_ATIVO, SELECT_ITEM_INATIVO };
private FuncaoORM funcaoA = new FuncaoORM(1L, "FUNÇÃO A"), //
funcaoB = new FuncaoORM(2L, "B Function"), //
funcaoC = new FuncaoORM(10L, "Se Funssaum");
public void abrirDialogoEdicao(PerfilAcessoORM p) {
this.perfil = (p == null)? new PerfilAcessoORM(): p;
System.out.println("PerfilAcessoMB.onAbrirDialogoEdicao(): "
+ this.perfil);
List<FuncaoORM> funcoesDisponiveis = new ArrayList<FuncaoORM>(
Arrays.asList(funcaoA, funcaoB, funcaoC));
// Remove from funcoesDisponiveis those whose are present in perfil.
if (this.perfil.getFuncoes() == null) {
this.funcoes = new DualListModel<FuncaoORM>(funcoesDisponiveis,
Lists.<FuncaoORM> newArrayList());
} else {
funcoesDisponiveis.removeAll(this.perfil.getFuncoes());
this.funcoes = new DualListModel<FuncaoORM>(funcoesDisponiveis,
this.perfil.getFuncoes());
}
}
// Getters & Setters
}
PerfilAcessoORM (Profile) Entity
public class PerfilAcessoORM {
private Long id;
private String nome;
private String descricao;
private Boolean situacao = null;
private List<FuncaoORM> funcoes;
// Getters & Setters
}
FuncaoORM Entity:
public class FuncaoORM {
private Long id;
public String descricao;
// Getters & Setters
}
What is my problem? Apparently, when I want insert a new Profile, all seems OK. However, when I click to edit an existent profile to open the dialog, although the outputText and pickList are correctly filled, the inputText's and selectOneMenu are not.
Looking for solutions, I found here a suggestion to use appendToBody="true" in dialog. When I tried, the inputText's and selectOneMenu are correctly filled. However, the validation is not working as waited. When I click to save it is showed a message indicating that pickList is not filled, even if it is really filled. Actually, even if inputText's are not filled, there is no message about these inputTexts although they are required.
Another alternative would be use <f:setPropertyActionListener> inside commandLink:
<!-- OPEN DIALOG TO CREATE A NEW PROFILE -->
<p:commandButton id="btnIncluir" value="Incluir"
update="dlgPerfil" immediate="true"
actionListener="#{perfilAcessoMB.abrirDialogoEdicao}"
oncomplete="dlgPerfil.show();">
<f:setPropertyActionListener target="#{perfilAcessoMB.perfil}" value="#{null}"/>
</p:commandButton>
<!-- OPEN DIALOG TO EDIT A NEW PROFILE -->
<p:commandLink id="lnkEditar" immediate="true"
title="Editar Perfil" update=":formPrincipal:dlgPerfil :formPrincipal:pnlPerfilEdicao"
actionListener="#{perfilAcessoMB.abrirDialogoEdicao}"
oncomplete="dlgPerfil.show();">
<f:setPropertyActionListener target="#{perfilAcessoMB.perfil}" value="#{perfil}"/>
<h:outputText value="Editar" />
</p:commandLink>
The actionListener method would changed to:
public void abrirDialogoEdicao() {
if(this.perfil == null) {
this.perfil = new PerfilAcessoORM();
}
System.out.println("PerfilAcessoMB.onAbrirDialogoEdicao(): "
+ this.perfil);
// The rest remains the same ...
}
Now, when I click to create a new perfil, although in actionListener method the perfil attribute is filled, I got a NullPointerException when the dialog is renderized. It is like first is called perfilAcessoMB.setPerfil(), after the dialog is opened and finally actionListener method. On the other hand, if I click to edit an existent profile, I return to initial situation.
Therefore, I am lost, without idea how to solve this issue.
Thanks,
Rafael Afonso
The solution was incredible simple: just added process="#this" to both commandButtons.
<!-- OPEN DIALOG TO EDIT A NEW PROFILE -->
<p:commandLink id="lnkEditar" immediate="true"
title="#{msg['titulo.edicao']}" process="#this"
update=":formPrincipal:dlgPerfil"
actionListener="#{perfilAcessoMB.abrirDialogoEdicao(perfil)}"
oncomplete="dlgPerfil.show();">
<h:outputText value="#{msg['titulo.edicao']}" />
</p:commandLink>

How to Update outputpanel from commandbutton which is inside the datatable using primefaces

Commandbutton with id("myButtonId2") works fine. I mean it updates "myOutputPanel"
but commandbutton(myButtonId) which is inside datatable doesn't update outputPanel. I found the suggestions and tried with the following,
update=":myForm:myOutputPanel"
update="#myForm"
update=":myOutputPanel"
But none of them worked...
Here is the snippet...
<h:form id="myForm" prependId="false">
<p:panel id="myPanel">
<p:dataTable id="myDatatable">
<p:column style="width:4%">
<p:commandButton id="myButtonId" actionListener="#{bean.showPanel}" update="myOutputPanel"/>
</p:column>
</p:dataTable>
<p:commandButton id="myButtonId2" update="myOutputPanel"/>
</p:panel>
<p:outputPanel id="myOutputPanel" rendered="#{bean.show}">
//some stuff
</p:outputPanel>
Try to put your <p:outputPanel /> inside a <p:panel />
and add a "widgetVar" attribute
For example : <p:panel widgetVar="var" [..]>
and just update the panel directly with update="#widgetVar(var)"

Change DataTable row color on checkbox selection

I have a <p:dataTable> , with a checkbox on the last column. I want to color the rows of the table based on the status of the checkbox.
<p:dataTable var="acs" value="#{myBean.listaRevogaveis}"
emptyMessage="#{rotulo.mensagemSemDados}" paginator="true"
rows="10" id="tableacs"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
<p:column headerText="Nome" sortBy="#{acs.nome}"
filterBy="#{acs.nome}">
<h:outputText value="#{acs.nome}" />
</p:column>
<p:column headerText="Address" sortBy="#{acs.address}" filterMatchMode="contains"
filterBy="#{acs.address}" filterMaxLength="8">
<h:outputText value="#{acs.address}" />
</p:column>
<p:column headerText="Perfil" sortBy="#{acs.cdPerfil}"
filterBy="#{acs.cdPerfil}" filterMaxLength="2">
<h:outputText value="#{acs.cdPerfil}" />
</p:column>
<p:column headerText="Cadastramento">
<h:outputText value="#{acs.tsSolicitacao}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</p:column>
<p:column headerText="Concedido">
<h:outputText value="#{acs.concedidoPor}" />
</p:column>
<p:column headerText="Revogar">
<p:selectBooleanCheckbox value="#{acs.ativo}" >
<p:ajax event="valueChange" oncomplete="toggleColor(this, #{acs.ativo}" listener="#{myBean.checkBox}" update="#form"/>
</p:selectBooleanCheckbox>
</p:column>
</p:dataTable>
So on the toggling of #{acs.ativo} I want that row to receive a different background color.
Following the answer of this question I tried to add this to my xhtml:
<style type="text/css">
.linhaDestacada { background-color: red !important;}
</style>
<script type="text/javascript">
function toggleColor(col, status) {
var linha = jQuery(col).parent();
if(status) {
linha.removeClass('linhaDestacada');
} else {
linha.addClass('linhaDestacada');
}
}
</script>
But that's of no use. I put some alerts to see if the function was being called, it is. However, trying to see the tagName, or any property of the linha variable returns null.
There is another interesting point, the callback is being called with the previous value of the checkbox. When box is checked, javascript toggleColor() is receiving false on the status, when its unchecked it receives true.
How can I make the row background toggle together with the checkbox toggle?
Ok, there were a TON of bugs and problems in that one.
First:
primefaces sets the background of the rows to a blank image (depending on the skin). you have to override the url() of the background, so the CSS should look like:
.linhaDestacada { background: url('') red !important;}
Second:
Calling the javascript from the ajax tag was causing the jQuery to find nulls (dunno why). move the callback to the checkbox tag, using its onchange attribute.
The final javascript fragment was:
<script type="text/javascript">
function toggleColor(col, status) {
var linha = jQuery(col).parent().parent().parent().parent().parent();
if(status) {
linha.removeClass('linhaDestacada');
//alert("remove " + linha.html());
} else {
linha.addClass('linhaDestacada');
//alert("add " + linha.html());
}
}
</script>
Third:
Updating the WHOLE form is a no-no (the css class you added with javascript was being erased)... you should update only what really matters (in this case, the checkbox and the command button.
<p:selectBooleanCheckbox value="#{acs.ativo}" onchange="toggleColor(this, #{acs.ativo})">
<p:ajax event="valueChange" listener="#{myBean.checkBox}" update="#this :formAcesso:btnRevogaNuke" />
</p:selectBooleanCheckbox>
Fourth:
the commandButton was not rendering. It would be updated on the ajax response, but the browser did not display it. Found this answer, and moved the id btnRevogaNuke on the encapsulating element:
<h:panelGroup id="btnRevogaNuke" layout="block">
<p:commandButton icon="ui-icon-alert" value="Confirmar rendered="#{myBean.confirmar()}" style="margin-left:600px;" action="#{acessoBean.revogaTodos()}" process="#this #form" update="#form" />
</h:panelGroup>