+ Start a Discussion
WEN JIEWEN JIE 

How to add a fixed value in a dynamic datatable

Hi,

 

I have a problem about visual force table tag.

I use <apex:datatable> in my visualforce page, and I set a dynamic data set for this table. But now I want to set a fixed value at the first row in this table ,then display other dynamic data.

 

Does anyone know how to achieve this?

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
HusseyHussey

Hi JIE,

 

you can modify your code as follows.

 

public class dataTableCon12 {
  List<Account> accounts; 
   public List<Account> getAccounts(){
    if(accounts == null)
       accounts = new List<Account>();
      accounts = [select name, Type ,phone from account limit 10];
      Account acct = new Account( name='Test', Type=' Customer - Channel' ,phone='123456789');
      accounts.add(acct);
     return accounts;
   }

}

 

/****************** Page ************************/

 

<apex:page controller="dataTableCon12" id="thePage">
<apex:pageBlock >
<apex:dataTable value="{!Accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column width="20%">
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}"/>
</apex:column>
<apex:column width="20%">
<apex:facet name="header">Phone</apex:facet>
<apex:outputText value="{!account.phone}"/>
</apex:column>
<apex:column width="20%">
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.type}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:page>

 

The row that you want to set with constant values will always be at last in the table.

 

All Answers

HusseyHussey

Hey JIE, 

 

you can do this. See the below code for reference.

 

 

public class dataTableCon12 {
  List<Account> accounts;
  public List<Account> getAccounts() {
   if(accounts == null)
    accounts = [select name, Type ,phone from account limit 10];
    return accounts;
  }
}

 

Page

--------------------------

 

<apex:page controller="dataTableCon12" id="thePage">
<apex:pageBlock >
<apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column width="20%">
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}"/>
</apex:column>
<apex:column width="20%">
<apex:facet name="header">Phone</apex:facet>
<apex:outputText value="123456789"/>
</apex:column>
<apex:column width="20%">
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.type}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:page>

WEN JIEWEN JIE

Hi Hussey,

 

Thanks for you answer.

As I know base on your code, I can set a fixed value in a column (Phone), but I want to add a fixed value in a row. 

So do you have any suggestion about this?

 

Thank you!

HusseyHussey

Hi JIE,

 

you can modify your code as follows.

 

public class dataTableCon12 {
  List<Account> accounts; 
   public List<Account> getAccounts(){
    if(accounts == null)
       accounts = new List<Account>();
      accounts = [select name, Type ,phone from account limit 10];
      Account acct = new Account( name='Test', Type=' Customer - Channel' ,phone='123456789');
      accounts.add(acct);
     return accounts;
   }

}

 

/****************** Page ************************/

 

<apex:page controller="dataTableCon12" id="thePage">
<apex:pageBlock >
<apex:dataTable value="{!Accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column width="20%">
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}"/>
</apex:column>
<apex:column width="20%">
<apex:facet name="header">Phone</apex:facet>
<apex:outputText value="{!account.phone}"/>
</apex:column>
<apex:column width="20%">
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.type}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:page>

 

The row that you want to set with constant values will always be at last in the table.

 

This was selected as the best answer
WEN JIEWEN JIE

Hi Hussey,

 

Thanks for you help! I think this is a way to achieve my target.

 

 

HusseyHussey

glad to help you...