0

I have a Linkedlist with 3 products, I would like to display in the input text the values of one of the products according to the button that I clicked. I am using Primefaces. I don't know how to create my the void to do so.

class Product :

public class Product {

private String idp;
private String brand;
private String price;


public Product() {

}

public Product(String idp, String brand, String price) {
    this.idp =  idp;
    this.brand = brand;
    this.price = price;
}

public String getIdp() {
    return idp;
}

public void setIdp(String idp) {
    this.idp = idp;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

 }

class Shop :

public class Shop {


 private LinkedList<Product> listprod;


public Shop() {
    listprod = new LinkedList<>();
    Product product1 = new Product("01","brand1","15");
    Product product2 = new Product("02","brand2","30");
    Product product3 = new Product("03","brand3","60");
    listprod.add(product1);
    listprod.add(product2);
    listprod.add(product3);
}

public LinkedList<Product> getListprod() {
    return listprod;
}

public void setListprod(LinkedList<Product> listprod) {
    this.listprod = listprod;
}

 }

view index :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">


 <h:head>
    <meta charset="utf-8" />
    <title>Products</title>   
</h:head>


<h:body id="mybody">

 <h:form id="myform">
    <ui:repeat var="temp" value="#{mybean.shop.listprod}" >
    <h:panelGrid columns="2" cellpadding="5" id="mygrid">

        <h:outputLabel for="idp" value="Id :"/>
        <p:inputText  id="idp" value="#{temp.idp}"/>


        <h:outputLabel for="brand" value="Brand :"/>
        <p:inputText  id="brand" value="#{temp.brand}"/>

        <h:outputLabel for="price" value="Price :"/>
        <p:inputText id="price" value="#{temp.price}"/>

        <p:commandButton value="Product 1" actionListener="#{mybean.showdata()}" />
        <p:commandButton value="Product 2" actionListener="#{mybean.showdata()}" />
        <p:commandButton value="Product 3" actionListener="#{mybean.showdata()}" />

    </h:panelGrid>
  </ui:repeat>

    </h:form>

</h:body>

</html>

My the bean :

@SessionScoped
@ManagedBean(name="mybean")
public class Mybean {

private Product product;
private Shop shop;


public void showdata(){



}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public Shop getShop() {
    return shop;
}

public void setShop(Shop shop) {
    this.shop = shop;
}


}
Benusko
  • 97
  • 1
  • 13
  • Just iterate over the `listprod` element using a `ui:repeat` tag. – Aritz Nov 10 '14 at 09:38
  • I have included a ui repeat tag in index, is it correct ? and how to do a void ? I am not even sure if the way I declare the Linkedlist is correct. – Benusko Nov 10 '14 at 09:57

2 Answers2

1

Iterate it using the ui:repeat tag.

<ui:repeat var="temp" value="#{mybean.listprod}">
<tr>
    <td>#{temp.idp}</td>
    <td>#{temp.brand}</td>
    <td>#{temp.price}</td>
</tr>
</ui:repeat>

Similar question How to use <ui:repeat> to iterate over a nested list?

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • I have updated my code in index view, and now everything that is in the ui:repeat is not displayed on screen (no label, no input text, no button) – Benusko Nov 10 '14 at 13:04
  • Have a look to the DOM tree (browser's f12 usually). Find out what's being rendered. – Aritz Nov 12 '14 at 10:38
0

You are using primefaces and want to display a list, why don't you use http://www.primefaces.org/showcase/ui/data/dataList.xhtml ?

<p:dataList value="#{mybean.listprod}" var="product" type="ordered">
    <f:facet name="header">
        Products
    </f:facet>
    #{product.idp}, #{product.brand}, #{product.price}
</p:dataList>

or (editable) datatable http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml ?

<p:dataTable var="product" value="#{mybean.listprod}" editable="true">
   <f:facet name="header">
       Products
   </f:facet>
   <p:column headerText="idp">
       <p:cellEditor>
           <f:facet name="output"><h:outputText value="#{product.idp}" /></f:facet>
           <f:facet name="input"><p:inputText value="#{product.idp}" style="width:100%"/></f:facet>
       </p:cellEditor>
   </p:column>
   <p:column headerText="brand">
       <p:cellEditor>
           <f:facet name="output"><h:outputText value="#{product.brand}" /></f:facet>
           <f:facet name="input"><p:inputText value="#{product.brand}" style="width:100%"/></f:facet>
       </p:cellEditor>
   </p:column>
   <p:column headerText="price">
       <p:cellEditor>
           <f:facet name="output"><h:outputText value="#{product.price}" /></f:facet>
           <f:facet name="input"><p:inputText value="#{product.price}" style="width:100%"/></f:facet>
       </p:cellEditor>
   </p:column>
</p:dataTable>
Ishkafel
  • 333
  • 1
  • 10