[SalesForce] visualforce, adding new products to a table on same page

I'm trying to do one thing for my application, I already did object Products. Added fields, manipulated with object…

Next I overrode the Products page with a VisualForce custom page, that uses Products__c controller and ProductDataController extension, because I wanted to add some extra functionality .

Next, i added apex:pageBlockTable and i want want that when I enter a new product and click save application add me products in table, that is under that form in same page. In that grid than would also like to have buttons to delete single product.

Code of Controller with wich I extend VisualForce page:

public with sharing class ProductDataController 
{

 ApexPages.StandardController controller;

 public ProductDataController(ApexPages.StandardController controller){}

 List<Product__c> products;

 public List<Product__c> getProducts()
  {
   if(products == null) products= [select Name, Product_Name__c, Product_Type__c from       Product__c ];
   return products;
   }
}

Here is code of ProductCustom VisualForce page :

<apex:page standardController="Product__c" extensions="ProductDataController" >    
<apex:form> 
  <apex:sectionHeader title="Products" subtitle="{!Product__c.name}"/ > 
  <apex:pageBlock title="Product input id="ProductBlock" > 

  <apex:pageBlockButtons >
    <apex:commandButton value="Save" action="{!Save}"/> 
    <apex:commandButton value="Quick Save" action="{!QuickSave}"/> 
    <apex:commandButton value="Cancel" action="{!Cancel}"/> 
  </apex:pageBlockButtons> 

  <apex:pageBlockSection columns="1">
    <apex:inputField value="{!Product__c.Product_Name__c}" style="height: 15px"/>

   <apex:inputField id="xxxif1" value="{!Product__c.Product_Type__c}" required="true">
      <apex:actionSupport event="onchange" rerender="ProductBlock" />
    </apex:inputField>

   <apex:inputField value="{!Product__c.IsUser__c}"  rendered="{!(Product__c.Product_Type__c == 'User')}"/>

     </apex:pageBlockSection>
  </apex:pageBlock> 
  <apex:pageBlock> 
   <apex:pageBlockTable value="{!products}" var="product" id="theTable" rowClasses="odd,even" styleClass="tableClass">
      <apex:column >
        <apex:facet name="header">Id</apex:facet>
        <apex:outputText value="{!product.Name}"/>
      </apex:column>      
      <apex:column >
        <apex:facet name="header">Name</apex:facet>
        <apex:outputText value="{!product.Product_Name__c}"/>
      </apex:column>
      <apex:column >
          <apex:facet name="header">Product type</apex:facet>
          <apex:outputText value="{!product.Product_Type__c}"/>
      </apex:column>
      <apex:column >
       <apex:facet name="header">Action</apex:facet>
        <apex:commandButton value="Delete product" action="{!removeProduct}"                rerender="ProductTabele"/>
    </apex:column>
   </apex:pageBlockTable >     
  </apex:pageBlock>      
 </apex:form> 
</apex:page>

1) When I click save, it saves the product, but it redirects me to default Page. How can I stay in the custom Visualforce Page?

2) I added a delete column with delete buttons. I want to remove Product from database
with action="{!removeProduct}" .

How can I write the removeRroduct Functon that deletes Product from table row. I have tried to pass the Parameter to controller function. I tried few ways but no luck.

Best Answer

Answer to Question #1

You need to overwrite the standard controller Save method to return a null PageReference. Don't forget to instantiate the StandardController in your constructor:

ApexPages.StandardController controller { get; set; }
public ProductDataController(ApexPages.StandardController controller)
{
    this.controller = controller;
}

public PageReference Save()
{
    controller.save();
    return null;
}

Answer to Question #2

Instead of:

<apex:commandButton value="Delete product" action="{!removeProduct}" rerender="ProductTabele"/>

Do:

<apex:commandButton value="Delete product" action="{!removeProduct}" rerender="ProductTabele">
    <apex:param value="{!product.Id}" assignTo="{!productIdToDelete}" name="productIdToDelete" />
</apex:commandButton>

And then in your controller:

public Id productIdToDelete { get; set; }

public List<Product__c { get { return productMap.values(); } }
public Map<Id, Product__c> productMap
{
    get
    {
        if (productMap == null)
        {
            productMap == new Map<Id, Product__c> ( [SELECT Name, Product_Name__c, Product_Type__c FROM Product__c ] ); // YOU SHOULD REALLY MAKE THIS QUERY SELECTIVE
        }
        return productMap;
    }
    private set;
}
public PageReference removeProduct
{
    if (productIdToDelete != null) productMap.remove(productIdToDelete);
    return null;
}
Related Topic