4

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.

  • 1
    Please post some code if you would like help. – Adrian Larson Sep 17 '13 at 20:40
  • Have you got correct code for this query? if any one of you got it kindly mail it to me on admn.vinayurkude@gmail.com Thanks! :-) – user3891 Sep 24 '13 at 18:22
2

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;
}
| improve this answer | |
  • thanks but nothing of that works, maybe only i do some thingsbad. 1) When i want to save, it throws an exception: System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!Save}' in page productscustom Class.ProductDataController.save 2)Compile errors here: public List<Product__c { get { return productMap.value(); } } ------- I aded > listMap() ---- than compiling go ahead, but next error is Method does not exist or incorrect signature: [MAP<Id,Student__c>].value() – Mark Bilbar Sep 19 '13 at 18:46
  • values(). I updated my answer. – Adrian Larson Sep 19 '13 at 18:56
  • Uh...you've got to instantiate the standardcontroller too... – Adrian Larson Sep 19 '13 at 19:03
  • Sorry but i dont realy understand. I did this... ApexPages.StandardController controller; correct? – Mark Bilbar Sep 19 '13 at 19:13
  • See my most recent edit. – Adrian Larson Sep 19 '13 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.