You need to sign in to do that
Don't have an account?
emb
VisualForce: how do I pass data from controller to javascript?
Hello,
My request is on the title. It seems to be simple, but I don't find a simple explanation to do that:
How do I pass data, from controller to javascript?
The situation is:
I have a controller which get some data from the database :
public with sharing class recup_donnees_test_Controller {
Public LIST<Calendrier__c> GetCalendriers(){
return Calendriers_Interne;
}
Public void SetCalendriers(LIST<Calendrier__c>Calendriers){
Calendriers_Interne = Calendriers;
}
Private LIST<Calendrier__c>Calendriers_Interne = new LIST<Calendrier__c>();
Public recup_donnees_test_Controller (){
Calendriers_Interne = [Select c.nom__c, c.id__c, c.Tache__c, c.SystemModstamp, c.Ressource__c, c.OwnerId, c.Name, c.LastModifiedDate, c.LastModifiedById, c.LastActivityDate, c.IsDeleted, c.Id, c.Heure_Fin__c, c.Heure_Debut__c, c.Date__c, c.CreatedDate, c.CreatedById, c.Categorie__c From Calendrier__c c WHERE c.Ressource__c = 'FRANCO-IDRISS Béatrice'];
}
}
And then i have a Visual Force page, in which would like to get the data
Visual force page:
<apex:page Controller="recup_donnees_test_Controller" > <apex:pageBlock title="Calendrier"> <apex:pageBlockTable value="{!Calendriers}" var="c"> <apex:column headerValue="nom" value="{!c.nom__c}"/> <apex:column headerValue="Tâche" value="{!c.Tache__c}"/> <apex:column headerValue="Ressource" value="{!c.Ressource__c}"/> <apex:column headerValue="Date" value="{!c.Date__c}"/> <apex:column headerValue="Heure Début" value="{!c.Heure_Debut__c}"/> <apex:column headerValue="Heure Fin" value="{!c.Heure_Fin__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
if someone could help me?
Thank you
Emmanuel
Use hidden variables. Map those to properties in controller.
Using documment.getelementbyid in JavaScript() you can access value in hidden variable.
Regards,
Pankaj Raijade,
Create new property in controller and access that in javascript using {!property_name}
let me know if you find it difficult
Hello
Thank you!
Yes it's dificult for me.
Could you please give me a simple example ? So i can use it in my development?
Do i have to create a compoment?
I try to understand how to create a property now...
Emmanuel
Define below as property
and access it's value in visual force page as
How to define "public Boolean ErrorOcc{get; set;} " as property ?
In your example put
between class definition so it wil be
please mark it as solution if you find it useful
I test your script.
I get an alert "false".
How do i "put" my data (for example "c.Date__c" , and where, in the controller and in the VF page ???
Please use below code:
Simplified Controller:
Visual Force Page
i get en error :
Erreur : recup_donnees_test_Controller Erreur de compilation : expecting right curly bracket, found ';' à la ligne 3 colonne 79
but it seems to be well wrote ( i copy the script and your script seems to be ok). So i don't find the cause of the error...
I understand that you are helping me!
and initialize it in constructor as
I get an error:
Erreur : recup_donnees_test_Controller Erreur de compilation : unexpected token: '=' à la ligne 4 colonne 23
Now my controller is:
public with sharing class recup_donnees_test_Controller {
public List<Calendrier__c> Calendriers_Interne{get; private set;}
Calendriers_Interne = new LIST<Calendrier__c>();
public String ErrorOcc{get;private set;}
Public recup_donnees_test_Controller (){
Calendriers_Interne = [Select nom__c, id__c, Tache__c, SystemModstamp, Ressource__c, OwnerId, Name, LastModifiedDate, LastModifiedById, LastActivityDate, IsDeleted, Id, Heure_Fin__c, Heure_Debut__c, Date__c, CreatedDate, CreatedById, Categorie__c From Calendrier__c WHERE Ressource__c = 'FRANCO-IDRISS Béatrice' limit 1];
for(Calendriers_Interne tempCal :Calendriers_Interne)
{
Errorocc=tempCal.Date__c;
}
}
}
I asked you to initialize list in constrctor so your code should be like this
with your the new script, i get an error again...:
Erreur : recup_donnees_test_Controller Erreur de compilation : Invalid type: Calendriers_Interne à la ligne 10 colonne 11
oh ok I forgot that date__c is date type so replace
error again...
Erreur : recup_donnees_test_Controller Erreur de compilation : Invalid type: Calendriers_Interne à la ligne 11 colonne 11
The 'date__c' is a custom field, so it gives data. I doubt that change it for 'date' could be a good thing...
public with sharing class recup_donnees_test_Controller {
public List<Calendrier__c> Calendriers_Interne{get; private set;}
public Date ErrorOcc{get;private set;}
Public recup_donnees_test_Controller (){
Calendriers_Interne = new LIST<Calendrier__c>();
Calendriers_Interne = [Select nom__c, id__c, Tache__c, SystemModstamp, Ressource__c, OwnerId, Name, LastModifiedDate, LastModifiedById, LastActivityDate, IsDeleted, Id, Heure_Fin__c, Heure_Debut__c, Date__c, CreatedDate, CreatedById, Categorie__c From Calendrier__c WHERE Ressource__c = 'FRANCO-IDRISS Béatrice' limit 1];
for(Calendriers_Interne tempCal :Calendriers_Interne)
{
Errorocc=tempCal.Date__c;
}
}
}
Change datatype to again String and replace
Errorocc=tempCal.Date__c;
with
Errorocc= String.valueOf(tempCal.Date__c);
Does it solve problem
now i get:
Erreur : recup_donnees_test_Controller Erreur de compilation : Invalid type: Calendriers_Interne à la ligne 11 colonne 11
ahh. sorry. your variable name is little bit confusing
replace old for loop line with this one
for(Calendrier__c tempval :Calendriers_Interne)
now i get: Erreur : recup_donnees_test_Controller Erreur de compilation : Variable does not exist: tempCal.Date__c à la ligne 13 colonne 39
thank you for your help
use below code
for(Calendrier__c tempCal :Calendriers_Interne)
Yes, i had seen this error...
But now i get another error:
Erreur : recup_donnees_test_Controller Erreur de compilation : Illegal assignment from String to Date à la ligne 13 colonne 12
didn't you change the data type at top from date to String
public String ErrorOcc{get;private set;}
no...
Now i have changed public Date ErrorOcc{get;private set;} to: public String ErrorOcc{get;private set;}
but iget 2 errors:
Erreur : recup_donnees_test line 16, column 18: XML document structures must start and end within the same entity
Erreur : XML document structures must start and end within the same entity.
Here is the Controller script:
public with sharing class recup_donnees_test_Controller {
public List<Calendrier__c> Calendriers_Interne{get; private set;}
public String ErrorOcc{get;private set;}
Public recup_donnees_test_Controller (){
Calendriers_Interne = new LIST<Calendrier__c>();
Calendriers_Interne = [Select nom__c, id__c, Tache__c, SystemModstamp, Ressource__c, OwnerId, Name, LastModifiedDate, LastModifiedById, LastActivityDate, IsDeleted, Id, Heure_Fin__c, Heure_Debut__c, Date__c, CreatedDate, CreatedById, Categorie__c From Calendrier__c WHERE Ressource__c = 'FRANCO-IDRISS Béatrice' limit 1];
for(Calendrier__c tempCal :Calendriers_Interne)
{
Errorocc= String.valueOf(tempCal.Date__c);
}
}
}
can you post your vf page code.
Here is the VF page :
<apex:page Controller="recup_donnees_test_Controller" >
<script>
var ErrorOcc='{!ErrorOcc}';
alert(ErrorOcc);
</script>
<apex:pageBlock title="Calendrier">
<apex:pageBlockTable value="{!Calendriers}" var="c">
<apex:column headerValue="nom" value="{!c.nom__c}"/>
<apex:column headerValue="Tâche" value="{!c.Tache__c}"/>
<apex:column headerValue="Ressource" value="{!c.Ressource__c}"/>
<apex:column headerValue="Date" value="{!c.Date__c}"/>
<apex:column headerValue="Heure Début" value="{!c.Heure_Debut__c}"/>
<apex:column headerValue="Heure Fin" value="{!c.Heure_Fin__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
You are missing </apex:page> at the end. It should be like this
Now i get
Erreur : Propriété 'recup_donnees_test_Controller.Calendriers' inconnue
why it search a ".Calendrier" at the end of the controller name ?
It is because you are referecing wrong variable in apex page.. your list name in controller is Calendriers_Interne but you are refering Calendriers in apex page. please replace it with Calendriers_Interne
Thank you very much for your help Himanshu!
The script works. It's great!
i have a last question, if you agree.
In the query, I ve changed the limit to 10. In the for, Errorocc get only the last record. How can i collect the 10 records? Instead of only the last record?
congrats..
do you want to get date list in javascript ?
not only date, but several fields/data.
The objective for me is to get something like that:
records=Id:'a0FU0000000Y0l5MAC', Tache__c:'Banque BCP - Financement des TPE', Ressource__c:'FRANCO-IDRISS Béatrice', OwnerId:'005U0000000hDs5IAE', Heure_Fin__c:'18:00:00', Heure_Debut__c:'09:00:00', Date__c:'2011-05-31', Categorie__c:'Intra-fr-Confirmer'
if you want that then it's better to user salesforce javascript API
yes of course i don't need to get only 1 record...
So i will read about and try salesforce javascript API.
Thanks again
great. best of luck
It's a little disappointing to cannot make an array (or something like...) here...
for(Calendrier__c tempCal :Calendriers_Interne)
{
Errorocc= String.valueOf(tempCal.Date__c);
}
So, it's not possible to collect data in apex?
I have to search more....
have a nice evening!
Emmanuel
it will be quite easy with javascript api
give it a try at your end if you find it difficult then let me know i will help you out.
ok, but i have to use sharing rules from force.com and/or salesforce.
I need to 'respect' the sharing settings to access some data.
For example, "user_2" can access only some accounts and not others accounts.
Thats why i have choose first an apex query, instead of a javascript query.
Unless that it would be possible to do that with javascript...
In the Constructor You may assign value to Property
Or in the method and rendering the page in script