0

I'm trying to create a table that will show the data from two different unrelated objects. There are two multiselect fields in page. Lets say one is A and other is B. So when user select 3 options - xy,ab,mn from A and 2 options - 123, 456 from B then in the table it should show all the 6 combination.

A B

XY 123

XY 456

AB 123

AB 456

MN 123

MN 456

This is the sample code that I'm using to create the table in VF Page is:

<table>
  <apex:repeat value="{!A}" var="a">
    <tr>
      <td>a</td>
      <apex:repeat value="{!B"} var="b">
        <td>b</td>
      </apex:repeat>
    </tr>
  </apex:repeat>
</table>

It is showing output as but I need to show all 6 combination in table:

A B

XY 123

AB 456

MN

1 Answer 1

4

I think this is a job for our old friend the wrapper class. If you create a wrapper class that contains two values, you can iterate the selections and create an instance of the wrapper class for each combination:

In the sample code below I've assumed you have access to the selected values via a pair of String arrays (i.e. that you are using an apex:selectlist component) - if these are input fields, you'll need to turn them into String arrays by splitting on the ';' separator from the field value:

public class MyController
{
   public List<String> selected1 {get; set;}
   public List<String> selected2 {get; set;}

   public List<Tuple> getCombinations()
   {
       List<Tuple> results=new List<Tuple>();
       for (String sel1 : selected1)
       {
          for (String sel2 : selected2)
          {
             Tuple tup=new Tuple(sel1, sel2);
             results.add(tup);
          }
       }
   }

   public class Tuple
   {
      public String value1 {get; set;}
      public String value2 {get; set;}

      public Tuple(String val1, String val2)
      {
          value1=val1;
          value2=val2;
      }
   }
}

and in the page:

<table>
  <tr>
     <th>A</th>
     <th>B</th>
  </tr>
  <apex:repeat value="{!combinations}" var="tuple">
    <tr>
      <td>{!tuple.value1}</td>
      <td>{!tuple.value2}</td>
    </tr>
  </apex:repeat>
</table>

You must log in to answer this question.

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