6 Replies Latest reply on Mar 2, 2009 10:29 PM by sarfaraz Mandal

    Richfaces datagrid having selectmanycheckbox and a4j command

    sarfaraz Mandal Newbie

      Our application requires results to be displayed in grid as provided by rich: datagrid. Further each element in datagrid has dynamically generated checkboxes and a commandbutton to add the selected checkbox.

      Following is my code:

       <rich:dataGrid id="resultsAtOnce" value="#{searchResultsForAtOnce}" var="res" columns="4" elements="20" width="600px" >
       <rich:panel bodyClass="pbody" onmouseover="this.className='highlight'" onmouseout="this.className='normal'">
       <f:facet name="header">
       <h:outputText value="#{res.catalogId}" />
       <ui:include src="popup.xhtml" />
       <h:outputLink value="#" id="link">
       <rich:componentControl for="panel" attachTo="link" operation="show" event="onclick"/>
       <h:panelGrid columns="2" >
       <h:outputText value="Style Number" styleClass="label"></h:outputText>
       <h:outputText value="#{res.styleNbr}" />
       <h:outputText value="Color " styleClass="label"></h:outputText>
       <h:outputText value="#{res.colorNbr}" />
       <h:outputText value="Style Name " styleClass="label"></h:outputText>
       <h:outputText value="#{res.styleName}"/>
       <h:outputText value="Price " styleClass="label"></h:outputText>
       <h:outputText value="#{res.price}" />
       <h:selectManyCheckbox value="#{cartAction.cc}" id="check" >
       <f:selectItems value="#{res.colorCodes}" />
       <a4j:commandButton id="addToCart" value="Add to Cart" actionListener="#{cartAction.addToCart}" >
       <f:facet name="footer">
       <rich:datascroller renderIfSinglePage="false" ></rich:datascroller>

      There are few technical aspects that I am stuck in:
      1. Ability to identify the selected element in the datagrid.
      2. Only the last element in each page is working normally as expected. That is on selection of checkboxes in elements in datagrid other than the last in the page, and subsequent clicking on the Add to Cart Button; the checkbox selections are not read.

      I am using richfaces, seam.

      Following is the seam component code.
      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Map;
      import javax.faces.component.UISelectItems;
      import javax.faces.context.FacesContext;
      import javax.faces.event.ActionEvent;
      import javax.faces.model.SelectItem;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.AutoCreate;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import com.nike.model.ProductBean;
      public class CartActionImpl implements CartAction {
       private ArrayList<ProductBean> productInCart;
       private ArrayList<ProductBean> displayedResults;
       private String productId;
       private ProductBean productInGrid;
       private String currentCartType="";
       private String previousCartType="";
       private UISelectItems uiColorCodes;
       private SelectItem[] colorCodes;
       private List<String> cc;
       public CartActionImpl(){
       this.cc= new ArrayList<String>();
       public List<String> getCc() {
       return cc;
       public void setCc(List<String> cc) {
       this.cc = cc;
       public UISelectItems getUiColorCodes() {
       return uiColorCodes;
       public void setUiColorCodes(UISelectItems uiColorCodes) {
       this.uiColorCodes = uiColorCodes;
       public SelectItem[] getColorCodes() {
       return colorCodes;
       public void setColorCodes(SelectItem[] colorCodes) {
       this.colorCodes = colorCodes;
       public ArrayList<ProductBean> getDisplayedResults() {
       return displayedResults;
       public void setDisplayedResults(ArrayList<ProductBean> displayedResults) {
       this.displayedResults = displayedResults;
       public String getProductId() {
       return productId;
       public void setProductId(String productId) {
       this.productId = productId;
       public String getCurrentCartType() {
       return currentCartType;
       public void setCurrentCartType(String currentCartType) {
       this.currentCartType = currentCartType;
       public String getPreviousCartType() {
       return previousCartType;
       public void setPreviousCartType(String previousCartType) {
       this.previousCartType = previousCartType;
       public ProductBean getProductInGrid() {
       return productInGrid;
       public void setProductInGrid(ProductBean productInGrid) {
       this.productInGrid = productInGrid;
       public ArrayList<ProductBean> getProductInCart() {
       return productInCart;
       public void setProductInCart(ArrayList<ProductBean> productInCart) {
       this.productInCart = productInCart;
       public void addToCart(ActionEvent event){
       System.out.println("Selected Products :" + cc);
       private ProductBean searchinListforProduct(String productId2) {
       Iterator<ProductBean> iterator = displayedResults.iterator();
       ProductBean pb=new ProductBean();
       while (iterator.hasNext()){
       pb= iterator.next();
       return pb;

      Please let me know your observations.
      Advise me the steps that I need to correct.