You need to sign in to do that
Don't have an account?
Anupama@28
How to access the controllers List in javascript?
Hi,
I have a requirement where I need to access the controller's List into javascript.
Below is my partial code which I have tried with.
<apex:component controller="MyCompController">
---
---
<script>
Var arr1 = new Array(); // OR var arr1 = [];
<apex:repeat value="{!AccountIdList}" var="accId">
arr1.push('{!accId}');
</apex:repeat >
alert('#### elements :' +arr1);
</script>
---
---
</apex:component>
controller
=====================
public class MyComponentController
{
public List<String> AccountIdList {get; set;}
public MyComponentController()
{
List<Account> acc = [Select name, id from account];
for(Account a : acc)
{
AccountIdList.add(a.id);
}
}
}
Is anything wrong I have done? Please help.
I have a requirement where I need to access the controller's List into javascript.
Below is my partial code which I have tried with.
<apex:component controller="MyCompController">
---
---
<script>
Var arr1 = new Array(); // OR var arr1 = [];
<apex:repeat value="{!AccountIdList}" var="accId">
arr1.push('{!accId}');
</apex:repeat >
alert('#### elements :' +arr1);
</script>
---
---
</apex:component>
controller
=====================
public class MyComponentController
{
public List<String> AccountIdList {get; set;}
public MyComponentController()
{
List<Account> acc = [Select name, id from account];
for(Account a : acc)
{
AccountIdList.add(a.id);
}
}
}
Is anything wrong I have done? Please help.
JS remoting works fine too, but Anupama@28 was so close to the solution, it would be a shame not to continue in that way.
Try these files (I tried, it works) :
MyCompController : VF component "testcmp": VF Page (you need this to view your component) "testcmppage" : when you type the url : https://INSTANCE.salesforce.com/apex/testcmppage, you should see an alert box showing a list of account ids.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
All Answers
I think you have a "Null pointer exception" message, no ?
You did not initialize the AccountIdList list before fill it with ids : There is nothing wrong with the javascript part of your code.
If this do not work, post your error message here.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
How can you use the visualforce component like <apex:repeat> inside the javascript directly and if anyhow it is working then i will be wonder to see this. I also tried to use the visualforce coponent inside the javascript but it is not working.
In my view for your probolem you should use the visualforce remoting , This can return back the data from controller class to javascript .
I am giving you a simple example that is returning a single value in the same way you can get the list of value from cotroller also.
Try to declare one Array inside the javascript and assign value in that .
Thanks
Rishav
JS remoting works fine too, but Anupama@28 was so close to the solution, it would be a shame not to continue in that way.
Try these files (I tried, it works) :
MyCompController : VF component "testcmp": VF Page (you need this to view your component) "testcmppage" : when you type the url : https://INSTANCE.salesforce.com/apex/testcmppage, you should see an alert box showing a list of account ids.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !