function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NiknitNiknit 

I want some easy solution for my hard codes on the Visual page and the Controller class

VF Page
<apex:page controller="productslistpage" showHeader="false">

  <Apex:form >


  <apex:outputText value="{!usermode}  {!Name}" style="float:right;font-weight:bold;color:black;font-size:16px;text-align:left;" />

        <br/><p style="font-size:19px;font-weight:bold;"> Products available today </p><br/>



            <apex:CommandLink action="{!prod1button}" ><br/>    
  <apex:image url="{!$Resource.Testprod1}" width="100" height="180"/><br/>
            <h3>Logo Shirt (Gray)</h3> 
            </apex:commandlink>
            <p>$20.00</p>

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

Controller
 
public Class productslistpage{

    public list<inventory__c> prodlist{get; set;} 
    public string PID{get; set;}
    public string name{get; set;}
    public string usermode{get; set;}

public productslistpage(){

    this.name = ApexPages.currentPage().getParameters().get('Name');
    this.usermode = ApexPages.currentPage().getParameters().get('usermode');
    prodlist = [Select ButtonName__c,Name, Price__c, Stocks__c,productid__c from Inventory__c];
}     

      PageReference pg = new PageReference('/apex/testprodpage');
public pagereference prod1button(){


      PID = '10004';    

      pg.getParameters().put('PID', PID);

      return pg; 
     }
}


The Vf page contains commandbutton for productone as well as name and other details of product, button is linked to pagereference prod1button which passes the product id to the other page.

What I need help with is I don't want to write each block of code similar to one eanother in the VF and the controller class.Is there a way I can generalise the code with anything, because i don't know how I can,I thought of Repeat but i don't think it helps with the methods in the class, as each method will be required , If i am not wrong. Please any help is appreciated.

 

Thank you,

Niknit

Varun SinghVarun Singh
hi @niknit

use  in tsetprod  vf page
 
<apex:page controller="productslistpage" >
    <apex:form >
       <apex:pageblock id="product" title="Product Details(Standard Object)" >
            <apex:pageblockTable value="{!prodlist}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.price__c}"/>
                <apex:column value="{!a.productid__c}"/>
                <apex:column value="{!a.Stocks__c}"/>
            </apex:pageblockTable>
        </apex:pageblock>
        
    </apex:form>
</apex:page>

 
NiknitNiknit

but How do I display the commandbutton for each product

User-added image

as you see the product name and image is a commandbutton, clicking it leads to another page, with your code how do i place the commandbutton?

I am sorry i am confused

 

Varun SinghVarun Singh
Hi Update your page
productslistpage vf page
<apex:page controller="productslistpage" showHeader="false">

  <Apex:form >

<apex:pageBlock>
  <apex:outputText value="{!usermode}  {!Name}" style="float:right;font-weight:bold;color:black;font-size:16px;text-align:left;" />

        <br/><p style="font-size:19px;font-weight:bold;"> Products available today </p><br/>

            <apex:pageBlockTable value="{!prodlist}" var="a">
            <apex:column>
            <apex:CommandLink action="{!prod1button}" ><br/>    
            <apex:image url="{!$Resource.Testprod1}" width="100" height="180"/><br/>
            <h3>Logo Shirt (Gray)</h3> 
            </apex:commandlink>
            <p>$20.00</p>
                        </apex:column>

</apex:pageBlockTable>
</apex:pageBlock>
    </apex:form>
</apex:page>

 
Varun SinghVarun Singh
Hi
I have   updated

 
<apex:page controller="productslistpage" showHeader="false">

  <Apex:form >

<apex:pageBlock>
  <apex:outputText value="{!usermode}  {!Name}" style="float:right;font-weight:bold;color:black;font-size:16px;text-align:left;" />

        <br/><p style="font-size:19px;font-weight:bold;"> Products available today </p><br/>

            <apex:pageBlockTable value="{!prodlist}" var="a">
            <apex:column>
            <apex:CommandLink action="{!prod1button}"   ><br/>    
            <apex:image url="{!$Resource.Testprod1}" width="100" height="180"/><br/>
            <h3>{!a.ButtonName__c}</h3> 
            </apex:commandlink>
            <p>{!a.price__c}</p>
                        </apex:column>

</apex:pageBlockTable>
</apex:pageBlock>
    </apex:form>
</apex:page>

 
NiknitNiknit

Thanks Varun,

 

But the thing is , I went with this before, i even went with repeat, the problem is how do I change the image and the methodname() [for pagereference], because right now it all leads to same product page and displays same pic for all products, I tried adding a column in the table for the method name and pic name, but when i try to add in the vf page between {! } it gives an error, eg - object name- inventory, column name for method name - Prodbutton , Now  if i try to call it ,             <apex:CommandLink action="{!a.prodbutton}"   ><br/>  , this gives an error as it searches for variable name "a.prodbutton" as in whole instead of querying inventory__c.prodbutton.

NiknitNiknit
Inventory_c *
Varun SinghVarun Singh
you have to  use  satatic resource name as  $Resource.Testprod1,$Resource.Testprod2,$Resource.Testprod3.......$Resource.Testprod10
use repeat 
i will do  someting  like that  

 
NiknitNiknit
Can you please show in code
Varun SinghVarun Singh
Hi @niknit

you have  to upload  images in documnets
you have custome field button name put the  url of image in  buttonname  fild

Stored images

these are images we store in document

and we can put the in button name filed  or  (in any other filed  you can also  hide it  from layout)

get id from images

then save  path of image in text type fild
User-added image

User-added image

apex code
 
public Class productslistpage{

    public list<inventory__c> prodlist{get; set;} 
    public string PID{get; set;}
    public string name{get; set;}
    public string usermode{get; set;}
    public string ImageUrl{get;set;}
public productslistpage(){

    this.name = ApexPages.currentPage().getParameters().get('Name');
    this.usermode = ApexPages.currentPage().getParameters().get('usermode');
    prodlist = [Select Name,ButtonName__c, Price__c, Stocks__c,productid__c from Inventory__c];
    imageURL='/servlet/servlet.FileDownload?file=';
    List< document > documentList=[select name from document where 
                                    Name='img1'];
   
    if(documentList.size()>0)
    {
      imageURL=imageURL+documentList[0].id;
    }
  
    
}     

      PageReference pg = new PageReference('/apex/testprodpage');
public pagereference prod1button(){


      PID = '10004';    

      pg.getParameters().put('PID', PID);

      return pg; 
     }
}

Page
 
<apex:page controller="productslistpage" showHeader="false">

  <Apex:form >

<apex:pageBlock >
  <apex:outputText value="{!usermode}  {!Name}" style="float:right;font-weight:bold;color:black;font-size:16px;text-align:left;" />

        <br/><p style="font-size:19px;font-weight:bold;"> Products available today </p><br/>
            <apex:variable var="count" value="{!0}"/>
            <apex:pageBlockTable value="{!prodlist}" var="a">
            <apex:column >
             <apex:variable var="count" value="{!count + 1}"/>
                          <apex:variable var="path" value="/servlet/servlet.FileDownload?file=img{!count}"/>

           {!imageurl}img1
            <apex:CommandLink action="{!prod1button}"   ><br/>    
      <apex:image value="{!a.ButtonName__c}"/>            <h3>{!a.ButtonName__c}</h3> 
            </apex:commandlink>
            <p>{!a.Price__c}</p>
                        </apex:column>

</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

output

User-added image

when you click on image button pass id of production to  controller and get detail of  same  product  you clicked  in neext page.

I  Hope  this is  helpful and best answer from my side.