Boa Tarde, por favor se alguém puder me ajudar, preciso inserir e remover linhas na tabela datatable:
na tabela existe 2 colunas uma com autoCoplete e outra com o inputText. na linha existe dois botões um Adicionar linha e outro exclir linha.
A minha vontade era inserir linha automaticamente assim que tirar o foco da segunda coluna do inputText, ou então apertar o botão Adicionar, que estará numa coluna na própria linha e adicionar uma linha abaixo. E se eu quiser excluir alguma linha vou no botão Excluir da linha e aciono. Me ajudem, por favor, já tentei bastante, sei que tem algo errado.
Meu Bean
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import br.com.ehs.dao.ItensDAO;
import br.com.ehs.modelo.Itens;
@ManagedBean(name="itensBean")
@SessionScoped
public class ItensBean {
Itens itens = new Itens();
private String text;
List<String> result ;
List<Integer> linhas;
ItensDAO itensdao = new ItensDAO();
private int codigo;
private String descricao;
private String status;
public List<Integer> getLinhas() {
return linhas;
}
public List<String> complete(String p){
result = itensdao.listarItens();
return result;
}
public String adicionarLinha(){
linhas.add(1);
return "";
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
DAO
@SuppressWarnings("unchecked")
public List<String> listarItens() {
List<String> itens = null;
try {
Criteria cri = session.createCriteria(Itens.class);
cri.setProjection(Projections.property("iteNome"));
itens = cri.list();
} catch (HibernateException e) {
e.printStackTrace();
}finally{
//session.close();
}
return itens;
}
orçamento.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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">
<ui:composition template="template.xhtml">
<ui:define name="conteudo">
<div id="form" align="center">
<f:view>
<h:form id="form1">
<p:growl id="messages" showDetail="false" > </p:growl>
<p:panel>
<div align="left">
<h:panelGrid columns ="4">
<h:panelGroup>
<tr>
<td align="right"><h:outputLabel value="Número : " for="numero" /> </td>
<td align="left"><h:inputText id="numero" size="10" required="true"/> </td>
</tr>
<tr>
<td align="right"><h:outputText value="Data do Orçamento : " /> </td>
<td align="left"><p:calendar locale="pt" showButtonPanel="true" navigator="true" id="dataorcamento" pattern="dd/MM/yyyy" requiredMessage="Selecione a data do Orçamento!" required="true"/></td>
</tr>
<tr>
<td align="right"><h:outputLabel value="Cliente : " for="cliente" /> </td>
<td align="left"><p:selectOneMenu id="cliente" size="40" requiredMessage="Selecione o cliente !" required="true" effect="fold">
<f:selectItems value="#{clientesBean.list}"/>
</p:selectOneMenu> </td>
</tr>
</h:panelGroup>
</h:panelGrid>
</div>
<p:dataTable id="itens" var="item" value="{itensBean.result}" >
<f:facet name="header">Tabela de Itens</f:facet>
<p:column headerText="Itens" style="text-align: center; width: 100px" >
<p:autoComplete size="32" queryDelay="1000" effect="blind" value="#{itensBean.text}" completeMethod="#{itensBean.complete}"/>
</p:column>
<p:column headerText="Descrição" style="text-align: center; width: 100px">
<p:inputText size="32" />
</p:column>
<p:column headerText="Adcionar Item" style="text-align: center; width: 100px">
<h:commandButton value="Adicionar" action="#{itensBean.adicionarLinha}" />
</p:column>
<p:column headerText="Apagar Item" style="text-align: center; width: 100px">
<h:commandButton value="Delete" />
</p:column>
</p:dataTable>
</p:panel>
<p:separator/>
<ui:include src="/WEB-INF/orcamento/botoesorcamento.xhtml"></ui:include>
<p:separator/>
</h:form>
</f:view>
</div>
</ui:define>
</ui:composition>
</html>