Search code examples
salesforceapex-codevisualforce

Creating an array of SelectOption Lists


Im trying to set up an online test, using a visualforce page that pulls data from 3 objects in salesforce COPE_Tests__C, COPE_Questions__C, and COPE_Options__c. Once the user selects the specific test, I thought I would be able to make a call like this to get all the other data:

questions = [select id, name, question_body__c, 
(select id, name, option_body__c from COPE_options__r order by name ASC)
from COPE_questions__c where COPE_test__c = :tid];

And then use apex:repeat and apex:selectRadio/apex:selectOption to generate the actual test form. But for some reason it would not render the radioboxes. So it would seem I need to create selectOption lists and then use apex:selectOptions. But im not sure how to set this up . How can I have it create a public list<selectOption> automatically for each question?

Is there a way to set up an array of list<selectOption>?


Solution

  • I don't know about creating it automatically but going over your question object in a loop should be pretty easy, something over the lines of

    List<List<SelectOption> options = new List<List<SelectOption>;
    for(COPE_Questions__C q : questions){
        List<SelectOption> list = new List<SelectOption>();
        for(COPE_options__r op : q.COPE_options__r){
             list.add(new SelectOption(op.id, op.option_body__c);
        }
        options.add(list);
    }
    

    Hope it helps.