1
I’m developing a prototype of Ecommerce being that I’m still trying to learn the logic of shopping cart. By that link or this other of videos on Youtube it makes the following steps:
It creates a method that can capture the record according to the selected line.
After that it update the tables via Ajax by the identifier address (id) of the table that is the record for the table that will be added to the new record applying shopping cart logic, but not forgetting to point out that this way is only possible because the two tables are on the same page.
My goal is to at least be able to capture the selected line and play the record to another page, but I can not imagine a way to do this, I can even play the identifier address of the selected line using :param
as in the code below, however I cannot load the other page even having the identifier address.
<p:button outcome="/noticias/CarrinhoCompras.xhtml"
icon="ui-icon-cart " title="Carrinho">
'
<f:param name="noticia" value="#{noticia.id}" />
</p:button>
This was my last attempt to put this snippet of code:
<p:commandButton value="adicionar"
action="#{carrinhoComprasBean.adicionar(noticia)}" />
And this is my Bean class that lists the table:
@Named
@ViewScoped
public class PesquisaNoticiasBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Noticias noticias;
private NoticiaFilter filtro;
private List<Noticia> noticiasFiltrados;
private Noticia noticiaSelecionada;
public PesquisaNoticiasBean() {
filtro = new NoticiaFilter();
noticiasFiltrados = new ArrayList<>();
}
public void pesquisar() {
noticiasFiltrados = noticias.filtrados(filtro);
}
public void inicializar() {
noticiasFiltrados = noticias.raizes();
}
public void excluir(){
noticias.remover(noticiaSelecionada);
noticiasFiltrados.remove(noticiaSelecionada);
FacesUtil.addInfoMessage("Noticia " + noticiaSelecionada.getTitulo_noticia() + "excluída com sucesso");
}
public List<Noticia> getNoticiasFiltrados() {
return noticiasFiltrados;
}
public NoticiaFilter getFiltro() {
return filtro;
}
public Noticias getNoticias() {
return noticias;
}
public Noticia getNoticiaSelecionada() {
return noticiaSelecionada;
}
public void setNoticiaSelecionada(Noticia noticiaSelecionada) {
this.noticiaSelecionada = noticiaSelecionada;
}
}
And this is the Bean class that works trying to transfer the selected record to the other table on the other page:
@Named
@ViewScoped
public class CarrinhoComprasBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Noticias noticias;
private NoticiaFilter filtro;
private List<Noticia> noticiasFiltrados;
private List<Item> listaItens = new ArrayList<>();
private Noticia noticiaSelecionada;
public CarrinhoComprasBean() {
filtro = new NoticiaFilter();
noticiasFiltrados = new ArrayList<>();
}
public void pesquisar() {
noticiasFiltrados = noticias.filtrados(filtro);
}
public String adicionar(Noticia noticia){
Item item = new Item();
item.setNoticia(noticia);
item.setQuantidade(1);
item.setValor(noticia.getPreco());
//System.out.println("lista de itens " +item);
listaItens.add(item);
return "CarrinhoCompras";
}
// public String addcart(Noticia n) {
// for (Item item : cart) {
// if (item.getNoticia().getId() == n.getId()) {
// item.setQuantidade(item.getQuantidade()+1);
// return "CarrinhoCompras";
// }
// }
// Item i = new Item();
// i.setQuantidade(1);
// i.setNoticia(n);
// cart.add(i);
// return "CarrinhoCompras";
// }
public void inicializar() {
noticiasFiltrados = noticias.raizes();
}
public void excluir() {
noticias.remover(noticiaSelecionada);
noticiasFiltrados.remove(noticiaSelecionada);
FacesUtil.addInfoMessage("Noticia " + noticiaSelecionada.getTitulo_noticia() + "excluída com sucesso");
}
public List<Noticia> getNoticiasFiltrados() {
return noticiasFiltrados;
}
public NoticiaFilter getFiltro() {
return filtro;
}
public Noticias getNoticias() {
return noticias;
}
public Noticia getNoticiaSelecionada() {
return noticiaSelecionada;
}
public void setNoticiaSelecionada(Noticia noticiaSelecionada) {
this.noticiaSelecionada = noticiaSelecionada;
}
public List<Item> getListaItens() {
return listaItens;
}
public void setListaItens(List<Item> listaItens) {
this.listaItens = listaItens;
}
}
That is the method in question:
public String adicionar(Noticia noticia){
Item item = new Item();
item.setNoticia(noticia);
item.setQuantidade(1);
item.setValor(noticia.getPreco());
//System.out.println("lista de itens " +item);
listaItens.add(item);
return "CarrinhoCompras";
}
For those who want to have access to my complete code is here my project on Github.
I really need help.
Following the proposed suggestion the method in the Carrinhocomprabean class has been modified;
public String adicionar(Noticia noticia){
Item item = new Item();
item.setNoticia(noticia);
item.setQuantidade(1);
item.setValor(noticia.getPreco());
// está aqui o código para imprimir
System.out.println("lista de itens " +item);
listaItens.add(item);
return "CarrinhoCompras";
}
This was the message that came out on the eclipse consoles on the first list item in the datatable
lista de itens br.com.vendelancha.model.Item@6b10199b
In the second item of the datatable list came out this
lista de itens br.com.vendelancha.model.Item@53404bd1
and that’s how the add button
<p:commandButton value="adicionar"
action="#{carrinhoComprasBean.adicionar(noticia)}">
<f:setPropertyActionListener target="#{carrinhoComprasBean.noticiaSelecionada}"
value="#{noticia}" />
</p:commandButton>
When it was clicked happened the facts described above and then was directed to the other page, but could not load the datable with the clicked item, because that’s the goal, click on the item and this item is added in the other table on the other page.
He’s on this screen
and went to this screen, not having resulted as you can see.
That button is inside a column?
– DiegoAugusto
positive, if you observe you will see that you are inside a column.
– wladyband
Observe where, because the code is not very clear rsrs. Anyway, try to use the
<f:setPropertyActionListener>
passing your selected object– DiegoAugusto
I have tried it, but I did it without asking for help, but I will put it again as I have done and request that you may persist in helping me. soon I will update my post.
– wladyband
When update the post let me know that put a possible solution to your problem.
– DiegoAugusto
I just updated the post.
– wladyband
Already tried using the rowSelect <p:datatable event> ?
– Rafael
I don’t know, I’ll see what it’s about.
– wladyband
I just saw, I tried, but I didn’t succeed.
– wladyband
Ready, can you get the right object? you want to move this object to another page now?
– DiegoAugusto
Yes, of course I do
– wladyband
Come on part, your code is too big and still not clear. You want to pass an object or a list to the other page?
– DiegoAugusto
I want to do similar to video >>>>>> https://www.youtube.com/watch?v=IH5TeMObs2s
– wladyband
what will be the difference of passing the object or passing the list?
– wladyband
I cannot watch the video at the moment if you pass the Object is just one item, if you pass a list are several items
– DiegoAugusto