function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Madhura BMadhura B 

Problem passing a value from the list to JavaScript

Hi

 

Can someone please tell me how to pass a value from the list to javascript in VF page when an output Link is clicked?

My code is as follows.

 

<script type="text/javascript">
  function Counter()
  {
   
    var id={!Docs}  // i want to know how to assign the id from the list Docs here. 'coz var id ="{!Docs.id}" doesn't work and // it is not the d.Document_Id__c used in the tag below i want to use here


    var updateRecords = [];
    result=sforce.connection.query("Select id,counter__c From Document_Details__c where id=:id");
    
    records = result.getArray("records");
    
      var DocumentDetails = new sforce.SObject("Document_Details__c");
      var c=records[0].counter__c;
      DocumentDetails.counter__c=c++;
      
      updateRecords.push(DocumentDetails);
      result=sforce.connection.update(updateRecords);
 
      if (result[0].getBoolean("success"))
        {
           log("document with id " + result[0].id + " updated");
           alert("success");
        }
     else
       {
        log("failed to update document " + result[0]);
        alert("falied");
       }
}
 </script>

 

<apex:pageBlockTable value="{!Docs}" var="d" rendered="{!if(Docs.size>0,true,false)}">

 

<apex:column >
               <apex:outputLink value="/servlet/servlet.FileDownload?file={!d.Document_Id__c}" onclick="Counter()" >
                 <apex:image url="{!URLFOR($Resource.LibraryImages)}" title="Click to Download" />
               </apex:outputLink>   
 </apex:column>

</apex:pageBlockTable>

 

Is the approach i have used right? or should i be using JavaScript remoting??

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

you can use action support to update that document count in apex controller. This is more stable approach.

 

By using action support and pass selected record Id as apex :param 

 

below is code snippet for both vf page 

 

<apex:pageBlockTable value="{!Docs}" var="d" rendered="{!if(Docs.size>0,true,false)}" id="pb">
 
<apex:column >
               <apex:outputLink value="/servlet/servlet.FileDownload?file={!d.Document_Id__c}" >
               <apex:image url="{!URLFOR($Resource.LibraryImages)}" title="Click to Download" />

               <apex:actionSupport event="onclick" 
                                action="{!incrementCounter}" 
                                rerender="pb">
               <apex:param assignTo="{!SelectedId}" name="selId" value="{!p.Id}"/>
               </apex:actionSupport>       

</apex:outputLink>  
 
 </apex:column>
</apex:pageBlockTable>


 

 

 

Controller code :

 

public string SelectedId {get;set;}
    public PageReference incrementCounter() 
    {
        System.debug('----------------'+ SelectedId );
        /* here you can update counter logic */
        return null;
    }

 

 

Thanks,

bForce

All Answers

b-Forceb-Force

you can use action support to update that document count in apex controller. This is more stable approach.

 

By using action support and pass selected record Id as apex :param 

 

below is code snippet for both vf page 

 

<apex:pageBlockTable value="{!Docs}" var="d" rendered="{!if(Docs.size>0,true,false)}" id="pb">
 
<apex:column >
               <apex:outputLink value="/servlet/servlet.FileDownload?file={!d.Document_Id__c}" >
               <apex:image url="{!URLFOR($Resource.LibraryImages)}" title="Click to Download" />

               <apex:actionSupport event="onclick" 
                                action="{!incrementCounter}" 
                                rerender="pb">
               <apex:param assignTo="{!SelectedId}" name="selId" value="{!p.Id}"/>
               </apex:actionSupport>       

</apex:outputLink>  
 
 </apex:column>
</apex:pageBlockTable>


 

 

 

Controller code :

 

public string SelectedId {get;set;}
    public PageReference incrementCounter() 
    {
        System.debug('----------------'+ SelectedId );
        /* here you can update counter logic */
        return null;
    }

 

 

Thanks,

bForce

This was selected as the best answer
Navatar_DbSupNavatar_DbSup

Hi,

 

You can pass the parameter in the JavaScript function like:

<apex:outputLink value="/servlet/servlet.FileDownload?file={!d.Document_Id__c}" onclick="Counter('{!d.Document_Id__c}')" >

                 <apex:image url="{!URLFOR($Resource.LibraryImages)}" title="Click to Download" />

               </apex:outputLink>  

 

You can receive the parameter in JavaScript like:

function Counter(id)

  {

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Madhura BMadhura B

Thanks bForce

 

My counter works perfect now.:)

 

however my table doesn't seem to rerender. Any suggestions around that please?

b-Forceb-Force

make sure that you are passing correct id in rerender attribute of action support,

you can pass form id over there , so It will rernder the form 

 

or you can share vf code here, we will check that 

 

Thanks,

bForce

Madhura BMadhura B

I tried with rerender but it stil doesn't work.

The last column in the pageBlocktable should be updated when outputlink is clicked

 

<apex:page sidebar="false" Controller="MyLibrary">

 <apex:form >
   <apex:pageBlock >
    <apex:pageBlockSection columns="1">
    
     <apex:PanelGrid width="30%">
       <apex:pageBlockSection >
           <apex:outputLabel value="Parent Folder" styleClass="labelCol first last" for="parent"/>
               <apex:selectList value="{!SelectedParentFolder}" size="1" id="parent" >
                      <apex:selectOptions value="{!ParentFolderList}" />
                       <apex:actionSupport event="onchange" reRender="folder"/>
               </apex:selectList>
         </apex:pageBlockSection>   
      </apex:PanelGrid>
     
 
        <apex:PanelGrid id="folder" width="30%">
             <apex:pageBlockSection >
             <apex:outputLabel value="Sub Folder" styleClass="labelCol first last" for="sfolder"/>
                   <apex:selectList value="{!SelectFolder}" size="1" id="sfolder" >
                          <apex:selectOptions value="{!FolderList}" />
                           <apex:actionSupport event="onchange" reRender="docs" action="{!Go}"/>
                   </apex:selectList>
             </apex:pageBlockSection>
       </apex:PanelGrid>  
   
   </apex:pageBlockSection>

 <apex:pageBlockSection columns="1" id="docs">
    <apex:pageMessage severity="info" summary="Sorry the Folder has no Content to display." rendered="{!if(UserLib.size!=0,if(Docs.size==0,true,false),false)}"/>
     <apex:pageMessage severity="info" summary="Sorry you do not have access to the selected Folder" rendered="{!if(UserLib.size==0,if(Docs.size==0,true,false),false)}"/>
   <apex:pageBlockTable value="{!Docs}" var="d" rendered="{!if(Docs.size>0,true,false)}" id="table" >
     
     <apex:column headerValue="Name" >
       <apex:outputField value="{!d.Name__c}" />
     </apex:column>
     
     <apex:column headerValue="Description">
       <apex:outputField value="{!d.Description__c}" />
     </apex:column>
     
     <apex:column headerValue="Type">
       <apex:outputField value="{!d.Type__c}" />
     </apex:column>
     
    <apex:column headerValue="Uploaded By">
       <apex:outputField value="{!d.Uploaded_By__c}" />
     </apex:column>
     
     <apex:column headerValue="Created Date">
       <apex:outputField value="{!d.Document_Created_On__c}" />
     </apex:column>
     
    
     <apex:column >
               <apex:outputLink value="/servlet/servlet.FileDownload?file={!d.Document_Id__c}" >
                 <apex:image url="{!URLFOR($Resource.LibraryImages)}" title="Click to Download" />

                   <apex:actionSupport event="onclick" reRender="table" action="{!incrementCounter}" >
                      <apex:param assignTo="{!SelectedId}" name="selId" value="{!d.Id}"/>
                   </apex:actionSupport>
                 
               </apex:outputLink>   
     </apex:column>
    
     <apex:column headerValue="Downloaded" >
            <apex:outputText value="{!d.Counter__c}" />   
     </apex:column>
   
      
   </apex:pageBlockTable>
</apex:pageBlockSection>


 </apex:pageBlock>

 

 

Thanks

b-Forceb-Force

You had bind Docs list with page block table,

When you are updating counter in Controller, you need to refresh DOcs list with new value. YOu can do this by qurying DOCS by hitting on more additional SOQL.

 

Also make sure you are rendering correct object . It looks pageBlocktable  "table" is fine .

 

 

Thanks,

bForce

Madhura BMadhura B

I have done that as well. Sorry i should have provided the controller earlier.

Here it is

 

 public void go()
 {
       Docs=new List<Document_Details__c>();
       Docs.clear();
       system.debug('---'+docs.size());
       
     try{
 
       MyLib=[select id from My_Library__c where Folder__c=:SelectFolder];
       UserLib =[select Accessed_by_User__c from User_Library__c where My_Library__c=: MyLib.id ];
            
      if(UserLib.size()!=0)
      {
          
           for (Integer j = 0; j< UserLib.size(); j++)
            {
               if(u.id==UserLib[j].Accessed_by_User__c)
               {   
                    Docs=[Select id, Name__c, Document_Id__c, counter__c,Uploaded_by__c,Type__c,Description__c,Document_Created_On__c,My_Library__c From Document_Details__c where My_Library__c=: MyLib.id];
                                   
               }
               
            }   
      }
      
      
    
   }catch(Exception e){}
 }
 
    public pagereference incrementCounter()
    {
       
        UpdateCount = [select id, counter__c from Document_Details__c where id =:SelectedId];  
        
        Decimal num= updatecount.counter__c;
        updatecount.counter__c=num+1;
      
        update updatecount;
        
        Docs.clear();
     
        Docs=[Select id, Name__c, Document_Id__c, counter__c,Uploaded_by__c,Type__c,Description__c,Document_Created_On__c,My_Library__c From Document_Details__c where My_Library__c=: MyLib.id];
 
        return null;
       
        
    }

 

VF page is same as my previous reply.

However it still doesn't seem to reRender.

 

Thanks,

b-Forceb-Force

provide Id to form object, and rerendor this form in action support.

 

Also check the DOc s list in System debug in increment counter function

 

Thanks,

BForce

Madhura BMadhura B

System debug shows that the counter is incremented alright.

 

i gave the form an id and put the same in the reRender but still it doesn't work.

Is it got something to do with outputlink. 'Coz my outpit link pops up a window to download a file.

 

b-Forceb-Force

It looks your specific element is not rendering , try using action stauts .