I am trying to create a table from apex according to the no of rows and columns entered on the Vf page the apex code will generate a table. This much is working fine. Now i need to add a select option in every columns. I wrote the code for it but the table is getting generated not the select option, is it possible to create select option from apex. please guide me. My code

    createtable = '<table border="1" size="width:100">';

    for(integer i =1; i<= No_Row; i++){// Looping through row
         createtable = createtable + '<tr>';
        for(integer j=1; j<= No_column ; j++){// Looping through column
            createtable = createtable + '<td><apex:selectList value="{!getSelected}" ><apex:selectOptions value="{!items}" /></apex:selectList></td>';
        createtable = createtable + '</tr>';

    createtable = createtable +'</table>';
    system.debug('table view '+ createtable );

debug log:

<table border="1" size="width:100"><tr><td><apex:selectList value="{!getSelected}" >   <apex:selectOptions value="{!items}" /></apex:selectList></td></tr></table>
  • when i coped the debug log, and put on the Vf page it worked as i need. But it is not working from apex.
    – AnuRaj
    Aug 8, 2013 at 10:21
  • 2
    Is this apex being invoked from a Visualforce page? If so, is there a reason you're generating raw HTML and not using the dynamic Visualforce generation classes? Also, what is invoking this code? Constructor? Method called by apex:page action binding? Other binding?
    – pchittum
    Aug 8, 2013 at 10:56
  • Yes this apex is invoked by VF page. If you do have any opinion for change. I am open to your opinion. It get invoked on button click after entering the numbers of rows and columns.
    – AnuRaj
    Aug 8, 2013 at 10:58
  • 1
    you can achieve this easily through actionFunction Aug 8, 2013 at 11:13
  • can you please demo it for me.
    – AnuRaj
    Aug 8, 2013 at 11:29

2 Answers 2


If it really must be so crazy... :)


public String createtable { get; set; }
public Integer No_Row { get; set; }
public Integer No_column { get; set; }
public String getSelected { get; set; }

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('1','Value 1'));
    options.add(new SelectOption('2','Value 2'));
    return options;

public PageReference createTable()
    createtable = '<table border="1" size="width:100">';

    for(integer i =1; i<= No_Row; i++){
        createtable = createtable + '<tr>';

        for(integer j=1; j<= No_column ; j++){
            createtable += '<td>';
            createtable += '<select onchange="takeValue(this.options[this.selectedIndex].value);">';
            for(SelectOption option : getItems())
                createtable += '<option value="' + option.getValue() + '">' + option.getLabel() + '</option>';
            createtable += '</select>';
        createtable = createtable + '</tr>';

    createtable = createtable +'</table>';

    return null;


<apex:page controller="test1" action="{!createTable}">


    You have selected: <apex:outputText value="{!getSelected}" id="output"/>

    <apex:actionFunction name="takeValue" reRender="output">
        <apex:param name="p1" assignTo="{!getSelected}" value=""/>

    <apex:outputText value="{!createtable}" escape="false" id="table"/>


    Rows: <apex:inputText value="{!No_Row}" style="width:20px;"/>
    Cols: <apex:inputText value="{!No_column}" style="width:20px;"/>

    <apex:commandButton action="{!createTable}" reRender="table" value="Ok"/>



And the result:

enter image description here


I would guess that whatever method that calls this is being done after the VF page renderer has finished generating the HTML, and so the apex:selectList tag cannot be evaluated. For instance if you have bound this to your page in a getter method, the visualforce renderer will have long since been done with its work by the time your string is placed into the page.

Dynamic visualforce allows you to generate visualforce tags from Apex. You can essentially build your entire VF component tree in code, then bind that to a visualforce page. Because it is bound to the page as a dynamic component, the renderer will be aware of it when it builds the page for display at runtime.

There are some write-ups in developerforce. Start with this wiki page and then go from there.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .