4 Replies Latest reply on Oct 11, 2005 5:51 AM by Gavin King

    SelectableItem problem

    Andrew Newbie

      Hi everyone!

      i have some strange problem (on example DVDstore such code working)

      i have list of SelectableItems

      public class SelectableItem<SiteProduct> implements Serializable {
       SiteProduct item;
       boolean selected = false;
      
       public SelectableItem(SiteProduct item) { this.item = item; }
      
       public SiteProduct getItem() { return item; }
      
       public void setItem(SiteProduct item) { this.item = item; }
      
       public boolean getSelected() { System.err.println("getSelected() " + selected); return selected; }
      
       public void setSelected(boolean val) { this.selected=val; System.err.println("setSelected() " + val);}
      
       public String toString() {
       return super.toString() + "[selected=" + selected + "]";
       }
      }


      it exposes on form

      @Stateful
      @Name("productOrdering")
      @Interceptor(SeamInterceptor.class)
      @Conversational(ifNotBegunOutcome="main")
      @LoggedIn
      public class ProductOrderingAction implements ProductOrdering, Serializable {
       private static final Logger log = Logger.getLogger(ProductOrdering.class);
      
       @PersistenceContext(type=EXTENDED)
       private EntityManager em;
      
       private String searchString;
      
       @Out
       public List<SelectableItem<SiteProduct>> products;
      
      ......
      
      public List getProducts() {
       return products;
      }
      public String addToCart() {
       log.info("Started adding to cart");
       if (products == null) {
       log.info("products null");
       return null;
       }
       for (SelectableItem<SiteProduct> item: products) {
       log.info("flag " + item.getSelected());
       if (item.getSelected()) {
       item.setSelected(false);
       log.info("????????? ? ??????? " + item.getItem().getName());
       cartService.addProduct(item.getItem(), new Double(1));
      
       }
       }
       return null;
      }
      
      }


      .xhtml code:

      <ui:define name="content">
      <div class="section">
       <h1>Search Products</h1>
       <h:form>
       <fieldset>
       <h:inputText value="#{productOrdering.searchString}" style="width: 165px;" />
       <h:commandButton value="Find Products" action="#{productOrdering.find}" styleClass="button" />
       </fieldset>
       </h:form>
      </div>
      <div class="section">
       <h:outputText value="No Products Found" rendered="#{productOrdering.products != null and empty productOrdering.products}"/>
       <h:form rendered="#{not empty productOrdering.products}">
       <h:dataTable value="#{productOrdering.products}" var="prod">
       <h:column>
       <f:facet name="header">V</f:facet>
       <h:selectBooleanCheckbox rendered="#{!empty(prod.item.price)}" value="#{prod.selected}"/>
       </h:column>
       <h:column>
       <f:facet name="header">Id</f:facet>
       #{prod.item.markingOfGoods}
       </h:column>
       <h:column>
       <f:facet name="header">Name</f:facet>
       #{prod.item.name}
       </h:column>
       <h:column>
       <f:facet name="header">Unit</f:facet>
       #{prod.item.unit}
       </h:column>
       <h:column>
       <f:facet name="header">Price</f:facet>
       #{prod.item.price}
       </h:column>
       <h:column>
       <f:facet name="header">Action</f:facet>
       <h:commandLink action="#{productOrdering.selectProduct}">View Product</h:commandLink>
       </h:column>
       </h:dataTable>
       <fieldset>
       <h:commandButton value="Add selected to cart" action="#{productOrdering.addToCart}" styleClass="button" />
       </fieldset>
       </h:form>
      </div>


      but when i select some products and trying to "add them to cart" seems like no one is selected

      help me please, suffer all day

      --Andrew