+ Start a Discussion
unidhaunidha 

Share list variable between two controller (page and component)

Hello all,

 

I want to ask if it possible or not to share/pass a list variable between two controller.Scenario is something like this:

 

I have one page named Page1 which is using StandardController Account and extension  named Extension1.

 

In this Extension1, I defined one list variable that contain all element which I used to contruct tab in Page1.

 

Then I created one component named Component1 with controller named Controller1.In this component, I put all the content that I want to display for every tab that constructed in Page1, to do this I just pass one list element as string .

 

So I have Page1 and inside it contain Component1.Now, where the issue come out. In the Controller1, I need to know all the element in the list variable that I defined in Extension1 in order to do some processing/ mathematical calculation. In order word, I need to retrieve the list from Extension1 in Controller1.

 

Is it possible to do this? Any Idea?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Yes, this is possible. It works like this:

 

public class ext1 {
  public string[] data { get; set; }
  public ext1(apexpages.standardcontroller controller) {
    data = new string[0];
  }
}

public class comp1 {
  public string[] data { get; set; }
}

<!-- component -->
<apex:component controller="comp1">
  <apex:attribute name="datavalue" assignto="{!data}" type="String[]" description="Data to pass" required="true"/>
</apex:component>

<!-- page -->
<apex:page standardcontroller="account" extensions="ext1">
  <c:datacomp datavalue="{!data}"/>
</apex:page>

Of course, this code doesn't DO anything, but when you look at the view state, component's "data" value will show "reference to data", meaning the extension's value. Altering the value in either case will result in both elements seeing the data changes. As you can imagine, this mechanism can allow components to share data across instances of the component on the same page.

All Answers

sfdcfoxsfdcfox

Yes, this is possible. It works like this:

 

public class ext1 {
  public string[] data { get; set; }
  public ext1(apexpages.standardcontroller controller) {
    data = new string[0];
  }
}

public class comp1 {
  public string[] data { get; set; }
}

<!-- component -->
<apex:component controller="comp1">
  <apex:attribute name="datavalue" assignto="{!data}" type="String[]" description="Data to pass" required="true"/>
</apex:component>

<!-- page -->
<apex:page standardcontroller="account" extensions="ext1">
  <c:datacomp datavalue="{!data}"/>
</apex:page>

Of course, this code doesn't DO anything, but when you look at the view state, component's "data" value will show "reference to data", meaning the extension's value. Altering the value in either case will result in both elements seeing the data changes. As you can imagine, this mechanism can allow components to share data across instances of the component on the same page.

This was selected as the best answer
unidhaunidha
Thanks , it works.
unidhaunidha
Hi,

But I have one doubt here, why it throw me Attempt to de-reference a null object . We already define get set in Comp1 and set the data in Ext1?
sfdcfoxsfdcfox
Could you post the relevant code that's causing the problem? I've used this successfully on many different pages, so I know it works.