4 Replies Latest reply on Oct 11, 2005 5:51 AM by gavin.king

    SelectableItem problem

      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

        • 1. Re: SelectableItem problem
          lcoetzee

          Hi Andrew

          based on my previous learning I think you should declare a getter/setter for those elements associated with selects (selectBooleanCheckbox) that you want to access in the view, and not use the @Out tag ...

          e.g.
          //No bijection stuff
          public List<SelectableItem> products;

          and in your interface (ProductOrdering) declare the getProducts/setProducts with the implementation in ProductOrderingAction.

          Regards

          Louis

          • 2. Re: SelectableItem problem

            I recommend using @DataModel instead of @Out. Then just reference #{products} in your datamodel. I've updated the dvd store to use @DataModel now.

            • 3. Re: SelectableItem problem

              Thanks Norman

              i changes my @Out to @DataModel
              Everything fine now. But @DataModel vas serious limitation - i can use only one @DataModel in every bean.
              Can i ask, when this limitation will be removed?

              Using more then one @DataModel in bean will be nice :-)

              --Andrew

              • 4. Re: SelectableItem problem
                gavin.king

                Yes, there is an existing feature request in JIRA to support >1 @DataModel