0
Well I’m starting now to develop with JSF and I’m with a doubt of how to do for my method edit receive the right object of the radio button , the way it is implemented it always takes the first of the table and not what is selected by the radio, what I missed ?
JSF
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
<link rel="stylesheet" href="#{resource['css:materialize.min.css']}" type="text/css"/>
<link rel="stylesheet" href="#{resource['css:style.css']}" type="text/css"/>
<title>Usuários</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</h:head>
<style>
.fixed-action-btn.horizontal ul {
top: 47%;
}
</style>
<h:body>
<script type="text/javascript" src="#{resource['js:jquery-3.2.1.min.js']}"/>
<script type="text/javascript" src="#{resource['js:materialize.min.js']}"/>
<script type="text/javascript" src="#{resource['js:scripts.js']}"/>
<nav>
<div class="nav-wrapper indigo lighten-1">
<h:graphicImage value="#{resource['imagens:logo_white.png']}" class="brand-logo center"/>
<ul id="nav-mobile" class="left">
<li> <a id="menu" href="#">
<i class="material-icons">menu</i>
</a>
</li>
</ul>
<ul id="nav-mobile" class="right hide-on-small-only">
<li> <a id="menu" href="#">Settings</a>
</li>
</ul>
</div>
</nav>
<h:messages />
<div class="divDados grey lighten-4">
<div class="divCima indigo lighten-2">
<div class="fixed-action-btn horizontal click-to-toggle right ">
<a class="btn-floating indigo lighten-1 z-depth-3">
<i class="material-icons">menu</i>
</a>
<ul>
<li><a class="btn-floating purple darken-4 z-depth-3" href="../index.jsf"><i class="material-icons">home</i></a></li>
<li><a class="btn-floating red darken-1 z-depth-3"><i class="material-icons">delete</i></a></li>
<li><h:form>
<h:commandLink id="menu" styleClass="btn-floating yellow darken-1 z-depth-3" action="#{BeanUsers.editar()}">
<i class="material-icons">mode_edit</i>
<f:setPropertyActionListener target="#{BeanUsers.selectedItem}"
value="#{usuario}" />
</h:commandLink>
</h:form></li>
<li><h:form>
<h:commandLink id="menu" styleClass="btn-floating green darken-1 z-depth-3" action="#{BeanUsers.cadastrar}">
<i class="material-icons">add</i>
</h:commandLink>
</h:form></li>
</ul>
</div>
<h4 class="white-text" style="text-align: center">Usuários</h4>
</div>
<div class=" dadosListar container">
<h:form>
<h:dataTable binding="#{BeanUsers.dataTable}" value="#{BeanUsers.listar()}" var="usuario" rules="row" cellpadding="5" styleClass="bordered striped" >
<f:facet name="header">Selecionar</f:facet>
<h:column>
<h:selectOneRadio value="#{BeanUsers.usuario}" valueChangeListener="#{BeanUsers.setSelectedItem}" onclick="selectOneRadio(this)">
<f:selectItem itemValue="#{usuario}" />
</h:selectOneRadio>
</h:column>
<h:column>
</h:column>
<h:column>
<f:facet name="header">Ativo</f:facet>
#{usuario.ativo}
</h:column>
<h:column>
<f:facet name="header">Nome</f:facet>
#{usuario.nome}
</h:column>
<h:column>
<f:facet name="header">Cargo</f:facet>
#{usuario.office}
</h:column>
<h:column>
<f:facet name="header">Telefone</f:facet>
#{usuario.celular}
</h:column>
<h:column>
<f:facet name="header">E-mail</f:facet>
#{usuario.email}
</h:column>
</h:dataTable>
</h:form>
</div>
#{usuario.id}
<div class="divBaixo indigo lighten-2"></div>
</div>
</h:body>
BEAN
@ManagedBean(name = "BeanUsers")
@RequestScoped
public class BeanUsers {
private Users usuario = new Users();
private String confirmarSenha;
private List<Users> lista;
private HtmlDataTable dataTable;
public List<Users> listar() {
if (this.lista == null) {
UsersBO usersBO = new UsersBO();
this.lista = usersBO.listar();
}
return this.lista;
}
public String users() {
return "/publico/users";
}
public String cadastrar() {
this.usuario = new Users();
this.usuario.setAtivo(true);
return "/publico/cadUsers";
}
public String salvar() {
FacesContext context = FacesContext.getCurrentInstance();
String senha = this.usuario.getSenha();
if (!senha.equals(this.confirmarSenha)) {
FacesMessage facesMessage = new FacesMessage("A senha nao foi confirmada corretamente");
context.addMessage(null, facesMessage);
return null;
}
UsersBO usersBO = new UsersBO();
usersBO.salvar(this.usuario);
return "/publico/users";
}
public Users getUsuario() {
return usuario;
}
public void setUsuario(Users usuario) {
this.usuario = usuario;
}
public String getConfirmarSenha() {
return confirmarSenha;
}
public void setConfirmarSenha(String confirmarSenha) {
this.confirmarSenha = confirmarSenha;
}
public String editar() {
this.usuario = usuario.getSelectedItem();
this.confirmarSenha = this.usuario.getSenha();
return "/publico/cadUsers";
}
public void setSelectedItem(ValueChangeEvent event) {
usuario = (Users) dataTable.getRowData();
}
public void getSelectedItem() {
ArrayList<Users> selectedDataList = new ArrayList<Users>();
selectedDataList.add(usuario);
}
;
public HtmlDataTable getDataTable() {
return dataTable;
}
public void setDataTable(HtmlDataTable dataTable) {
this.dataTable = dataTable;
}
}
JS
function selectOneRadio(radio) {
var id = radio.name.substring(radio.name.lastIndexOf(':'));
var el = radio.form.elements;
for (var i = 0; i < el.length; i++) {
if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
el[i].checked = false;
}
}
radio.checked = true;
}
I did not understand the part of JS. If it is radio button, everyone should have the same name and only 1 should be selected. What is this function then
selectOneRadio(radio)
?– Sam