5 Replies Latest reply on Dec 22, 2010 4:49 AM by eswaramoorthy1985

    Is possible : javascript access c:forEach tag value?

    eswaramoorthy1985

      Hi,

       

            i have displyed values using c:forEach. (The values got from backing bean).

       

      Then I want to write javascript for read data from c:forEach values. Is possible?

       

       

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
      
      <f:view>
          <html>
              <head>
                  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                                
                  <script type="text/javascript">
                                   
                      function getEachValue()
                      {                    
                         //Here i want to write a code for, access the c:forEach tag (displayed) value
                      }
      
                  </script>
      
              </head>
              <body>
                  <h:form id="sampleForm" >                
      
                      <c:forEach items="${bean.pageHits}" var="pageHit">
                          <c:out value="${pageHit.period} : "/>
                          <c:out value="${pageHit.hits}"/></br>
                      </c:forEach>
      
                      <a4j:commandButton id="getvalue" value="GetValue" oncomplete="getEachVaue()"/>
      
                  </h:form>
              </body>
          </html>
      </f:view>
      

       

       

      If I click GetValue button, then the javascript read all value in c:forEach.

       

      Bean.java

       

       

      import java.util.ArrayList;
      import java.util.List;
      public class Bean
      {
         private List<PageHit> pageHits = new ArrayList<PageHit>();
      
          public Bean()
          {
              pageHits = loadItSomehow();
          }    
          public List<PageHit> getPageHits(){
              return pageHits;
          }
          
          public void setPageHits(List<PageHit> pageHits){
              this.pageHits = pageHits;
          }
      
          private List<PageHit> loadItSomehow()
          {
              List<PageHit> localpageHits = new ArrayList<PageHit>();
      
              localpageHits.add(new PageHit("Jan 2010",60));
              localpageHits.add(new PageHit("Feb 2010",30));
              localpageHits.add(new PageHit("Mar 2010",90));
      
              return localpageHits;
          }
      }
      

       

      and

      PageHit.java

       

      public class PageHit {
          private String period;
          private Integer hits;
      
          public PageHit(String period, Integer hits){
              this.period = period;
              this.hits = hits;
          }    
          public String getPeriod(){
              return period;
          }    
          public void setPeriod(String period){
              this.period = period;
          }   
          public Integer getHits(){
              return hits;
          }    
          public void setHits(Integer hits){
              this.hits = hits;
          }
      }
      

       

       

      Help me.

      Thanks in advance.

        • 1. Re: Is possible : javascript access c:forEach tag value?
          nbelaevski

          Hi,

           

          Use 'data' attribute of a4j:commandLink to pass data to the client.

          1 of 1 people found this helpful
          • 2. Re: Is possible : javascript access c:forEach tag value?
            eswaramoorthy1985

            Thanks for your reply.

            But i can't get any idea from your reply.

             

            Please give me any example related my question or give me any reference.

            • 3. Re: Is possible : javascript access c:forEach tag value?
              liuliu

              hi,

               

              i think he means

              <a4j:commandButton id="getvalue" value="GetValue" data="#{bean.pageHits}" oncomplete="getEachVaue(data)"/>
              

              you need read developper guide for more detail.

              1 of 1 people found this helpful
              • 4. Re: Is possible : javascript access c:forEach tag value?
                eswaramoorthy1985

                My problem is : how i extract values from data

                 

                Here just i got the object. The data has indirectly the following value like   "Jan 2010 :  60   Feb 2010 :  30   Mar 2010 :   90"

                 

                How to show these value (one by one) in javascript. (alert message)?

                 

                 

                <a4j:commandButton id="getvalue" value="GetValue" data="#{bean.pageHits}" oncomplete="getEachVaue(data)"/>
                

                And script

                 

                 

                <script type="text/javascript">
                
                      function getEachValue(data)
                      {
                            alert("data : " + data);                      //Here just i got object. 
                
                            // Here, how i iterate data. i want to Show one by one  
                            // here i want to add the value (60 + 30 + 90) using javascript code.
                      }      
                
                </script>
                

                 

                Then i don't know, get the individual value, like i want to show alert message in javascript.

                 

                Give me any reference to   extract or iterate the 'data'.

                 

                 

                please help me.

                Thanks liumin hu  for your reply and well effort.

                • 5. Re: Is possible : javascript access c:forEach tag value?
                  eswaramoorthy1985

                  Hi,

                   

                         I got solution. Thanks Nick Belaevski and liumin hu for your support.

                   

                   

                  <a4j:commandButton id="getvalue1" value="GetValue1" data="#{bean.pageHits}" oncomplete="getEachVaue(data)"/>
                  

                   

                  My javascript is :

                   

                  function getEachVaue(data)
                   {                    
                       for(var i=0; i<data.length; i++)
                       {
                            alert("Month : " + data[i].period);
                            alert("Value : " + data[i].hits);
                       }
                  }