[SalesForce] Dynamically add rows to vf page

Can you please help me how to add rows dynamically in vf page based on record getting in query in controller.
Below is the code i am using, in this currently adding one row, but need to add rows based on the records getting in SQL query.

VF page:

<apex:page standardController="Opportunity" extensions="ListsControllernew">
    <apex:form>   
        <apex:pageBlock title="Files">
            <apex:pageBlockButtons location="top" >
                <apex:commandButton value="Upload Files" />
            </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!oppname}" var="acc">
                <apex:column headerValue="Action" value="{!oppname}" />
                <apex:column headerValue="Title" value="{!title}" />
                <apex:column headerValue="Last Modified by" value="{!Modified}" />
                <apex:column headerValue="Is Locked" value="{!Lockvalue}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class ListsControllernew  {

    private final Opportunity opp;
    public string title { get; set; }
    public String Modified { get; set; }
    public String Lockvalue { get; set; }
    public String oppname { get; set; }

    public ListsControllernew(ApexPages.StandardController stdController) {
        this.opp = (Opportunity) stdController.getRecord();
        system.debug('id:'+opp.id);

        //List<Opportunity> customObjects =[select name from Opportunity where Id=:opp.id];
        //SELECT ContentDocumentId, FROM ContentDocumentLink WHERE LinkedEntityId = '[RECORD ID]' 

        integer count = [SELECT count() FROM ContentDocumentLink 
            WHERE LinkedEntityId =: opp.id];
        system.debug('Count:'+count);

        List<ContentDocumentLink> customObjects = [SELECT Id, ContentDocumentId, 
            ContentDocument.LatestPublishedVersionId, ContentDocument.Title, 
            ContentDocument.CreatedById, ContentDocument.LastModifiedDate FROM 
            ContentDocumentLink WHERE LinkedEntityId =: opp.id];

        for(ContentDocumentLink rec: customObjects) {
            oppname = rec.ContentDocument.Title;
            title = rec.ContentDocument.Title;
            Modified = rec.ContentDocument.CreatedById;
            Lockvalue = rec.ContentDocument.CreatedById;
        }
    } 
}

Thanks.

Best Answer

Page block tables can be used with Collections, I've put together a quick example for you below:

Visualforce:

<apex:pageBlockTable value="{!documentLinks}" var="documentLink">
      <apex:column value="{!documentLink.ContentDocument.Title}" />
      <apex:column value="{!documentLink.ContentDocument.CreatedById}" />
</apex:pageBlockTable>     

Controller:

public class ObjectController {
     private final Opportunity opportunity;
     public List<ContentDocumentLink> documentLinks {get;set;}


     public ObjectController(ApexPages.StandardController stdController) {
          opportunity = (Opportunity)stdController.getRecord();
          records = [SELECT ContentDocument.Title, ContentDocument.CreatedById FROM ContentDocumentLink WHERE LinkedEntityId =: opportunity.id];
     }
}  

Note: if you don't specify a header value for an SObject field, it should display the field's label.

Related Topic