3 Replies Latest reply on Dec 23, 2009 7:42 PM by Nick Belaevski

    Help with <a4j:commandButton> within <rich:dataTable>

    Mladen Stupar Newbie

      I need help with the following operation that I need to do. My <rich:dataTable> component which gets some data from the database, in a browser looks like this:

      dataTable.png

       

      I need to do next. When I click <a4j:commandButton> (1) and when button calls actionListener method in class CatalogBean, I have to get a numeric value stored in the first column in <h:outputText value="#{itemArt.id}"> (2) component, and value from the component <rich:inputNumberSpinner> (3). All this, of course, from the table's row that the button belongs.

       

      Does anyone know which classes and methods I could use to get the necessary data from a table?

        • 1. Re: Help with <a4j:commandButton> within <rich:dataTable>
          Deb Dutta Novice

          Please check the demo for editable data tabale

          http://livedemo.exadel.com/richfaces-demo/richfaces/dataTable.jsf?tab=editDataTable&cid=5364210

           

          there you can find a good example how to acess the current row and work on it.

           

           

          To assign the current row in thta aaj:commandButton have to use f:setPropertyActionListener value

          <a4j:commandLink ajaxSingle="true"   .......     />
                                  <f:setPropertyActionListener value="#{row}"
                                      target="#{dataTableScrollerBean.currentRow}" />
                              </a4j:commandLink>

           

          In the bean currentRow will have the row where you are working on, so you can get the required data from this object.

           

          To get the demo bean source check for the SVN source code

          1 of 1 people found this helpful
          • 2. Re: Help with <a4j:commandButton> within <rich:dataTable>
            Mladen Stupar Newbie

            Following my first post.
            I managed to send the ID data (first column) from the table, the row that I clicked.

            But how now to take the data from <rich:inputNumberSpinner>(3) components?

            This is part of code (xhtml page and java bean class):

             

            CatalogPortlet.xhtml

            <a4j:region>
                 <rich:dataTable id="dt1" value="#{CatalogBean.itemArtAll}"
                      var="itemArt" rowKeyVar="row"
                      onRowMouseOver="this.style.backgroundColor='#EFF7FF'"
                      onRowMouseOut="this.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'"
                      width="100%" columns="7">
            
                      ...
                      <rich:column>
                           <f:facet name="header">
                                <h:outputText value="Odaberi kolicinu" />
                           </f:facet>
                                <!--     <a4j:form ajaxSubmit="true">-->
                           <rich:inputNumberSpinner value="#{CatalogBean.odbKolicina}"
                                          minValue="1" maxValue="#{itemArt.kolicina}" />
                                <!--     </a4j:form>-->
                      </rich:column>
            
                      <rich:column>
                           <f:facet name="header">
                                <h:outputText value="Dodaj u korpu" />
                           </f:facet>
                           <a4j:commandButton id="button" value="Kupi" ajaxSingle="true"
                                     actionListener="#{CatalogBean.currentRow}" immediate="true" style="width:70px;">
                                <f:setPropertyActionListener value="#{row}"
                                     target="#{CatalogBean.currentRow}" />
                           </a4j:commandButton>
                      </rich:column>
            
                      ...
            
                 </rich:dataTable>
            </a4j:region>
            

             

            CatalogBean.java

            ...
            private int currentRow;
            private int odbKolicina;
            
            private List<itemArt> itemArtAll = new ArrayList();
            
            public List getItemArtAll() {
                 // This method the fills variable "itemArtAll" with items from the database
                 return itemArtAll;
            }
            
            public void currentRow(ActionEvent event) {
                 int idd = itemArtAll.get(getCurrentRow()).getId();
                 System.out.println("IDD = " + idd);
                 int kol = odbKolicina;
                 System.out.println("KOL = " + kol);
            }
            
            public int getCurrentRow() {
                 return currentRow;
            }
            
            public void setCurrentRow(int currentRow) {
                 this.currentRow = currentRow;
            }
            
            public int getOdbKolicina() {
                 return odbKolicina;
            }
            
            public void setOdbKolicina(int odbKolicina) {
                 this.odbKolicina = odbKolicina;
            }
            
            public class itemArt {
                 private int id; //get, set
                 private int cena;
                 private int kolicina;
                 private String nazivArtikla;
                 private String nazivKategorije;
                         
                 public itemArt(){}
                      
                 public itemArt(int id, int cena, int kolicina, String nazivArtikla, String nazivKategorije){
                      this.id = id;
                      this.cena = cena;
                      this.kolicina = kolicina;
                      this.nazivArtikla = nazivArtikla;
                      this.nazivKategorije = nazivKategorije;
                 }
            
                 ...
                 Getters and Setters
            }
            

             

            Current solution that I have in the code, for taking values from the <> doesn't work. Always returns 0 ( "KOL = 0" in the console). For IDD works correct.

            If someone has an idea what the problem is here?

            • 3. Re: Help with <a4j:commandButton> within <rich:dataTable>
              Nick Belaevski Master

              Hi,

               

              How about this:

               

               <rich:inputNumberSpinner value="#{itemArt.odbKolicina}"

               

              and also move action method to "itemArt"?

               

              Message was edited by: Nick Belaevski