+ Start a Discussion
Tyler HarrisTyler Harris 

Pass value from Visualforce to Controller

I wanted to grab the row Id below and pass it to the "rowz" variable. In my debug log the value is never passed from my method. Any ideas on how to adjust?

I'm using rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel'); to try and grab the row ID, but it keeps giving me a null.
Apex
public class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;

    public Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
    
            
        }  
         
    }

    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
             
          		

            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
 
                }
       
        return cart;
    }

    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public Integer rowNumber { get; set; }
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}

Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
   
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart">
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form>    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
         	<apex:param name='rowDel' value="{!cart[carti].merchandise.Id}" />
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
         <apex:column headerValue="Remove?">
             <apex:commandButton action="{!Remove}" value='Remove' reRender="cart"/>
         </apex:column>
                     

      </apex:dataTable>
    </apex:form>
    
  
</apex:page>

 
Best Answer chosen by Tyler Harris
Nitin PaliwalNitin Paliwal
HI,
Try this vf page code,

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
  
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
         
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
  
      </apex:dataTable>
     
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart">
     
      </apex:commandButton>
  </apex:form>
 
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>   
    <h1>Your Basket</h1>
   
<apex:form>   
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
   
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
          <apex:param name='rowDel' value="{!cart[carti].merchandise.Id}" />
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
           
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
         <apex:column headerValue="Remove?">
             <apex:commandButton action="{!Remove}" value='Remove' reRender="cart">
                   <Apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
             </apex:commandButton>
         </apex:column>
                    

      </apex:dataTable>
    </apex:form>
   
 
</apex:page>

<!---------------->
Use this remove method:

public void remove(){
     //rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);

        }  
 
    }

I hope this solves your problem.

Thanks
Nitin