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
SurjenduKSurjenduK 

How to do complex ajax request within VisualForce?

Code:
<apex:page>

<script src="/soap/ajax/12.0/connection.js" type="text/javascript"></script>
<script>
function setupPage(search)
{
var output = document.getElementById("output");
try
{
sforce.connection.login("surkuila@gmail.com", "pooja1234$lFX0WCCdn6h1ut6WoxWkwUB0c");
var queryResult = sforce.connection.query("Select Id, name from Lead where name like '%" + search + "%'");
layoutResults(queryResult, output);
}
catch(error)
{
queryFailed(error, output);
}
}

function queryFailed(error, out)
{
out.innerHTML = "<font color=red>An error has occurred:</font> <p>" + error;
}

function layoutResults(queryResult, out)
{
if (queryResult.size > 0)
{
var output="";
var records = queryResult.getArray('records');
for (var i = 0; i < records.length; i++)
{
var lead = records[i];
output += lead.Id + " " + lead.Name + "<BR>";
}
out.innerHTML = output;
}
else
{
out.innerHTML = "No records matched.";
}
}

</script>


<div class="bNext">
<div class="withFilter">
<div class="rolodex">
<a href="javascript:setupPage('A')" class="listItem"><span class="listItemPad">A</span></a>
<a href="javascript:setupPage('B')" class="listItem"><span class="listItemPad">B</span></a>
</div>
</div>
</div>

<body>
<div id="output"></div>
</body>

</apex:page>

 The above code does a ajax request on a hyperlink click and dumps the output data. I want to build a <apex:dataTable> instead of just dumping the data. How do I achieve this?

A basic question " How to do complex ajax request within VisualForce". All the examples in the pdf and books are simple and does not reflect the actual complexity of the ajax problems we face. Any help from gurus? My earlier example has everything in HTML covered in just <apex:page>. The apex:page is the only tag i am using to make it a visualforce page.

I eventually want it to be a VF page and not SControl. So please dont give me suggestions to use S-Control.

How to do complex ajax request within VisualForce?


Ron HessRon Hess
ajax is done for you by the visualforce components.
consider this example

page:
Code:
<apex:page controller="conController" 
 showHeader="true" 
 tabStyle="Contact" 
 sidebar="false" >
 
<style type="text/css">td { vertical-align:top; }</style>


<apex:form>
 <apex:panelGrid columns="2">
 <!-- left side list -->
 <apex:pageBlock id="body">
 
  <apex:pageBlockList 
  value="{!contacts}" var="c">
   <apex:column>
    <u>{!c.name}</u>
    
    <apex:actionSupport 
     event="onclick" 
     rerender="detail">
     <apex:param name="cid" value="{!c.id}" />
    </apex:actionSupport>
    
    </apex:column>
   <apex:column>
    {!c.account.name}
     <apex:actionSupport 
     event="onclick" 
     rerender="detail">
     <apex:param name="cid" value="{!c.id}" />
    </apex:actionSupport>
    
    </apex:column>
 
  </apex:pageBlockList>
 </apex:pageBlock>

 <apex:outputPanel id="detail">  
     <apex:detail subject="{!contactRecord}" title="false" relatedList="false"/>  

 </apex:outputPanel>
 </apex:panelGrid>
</apex:form>


</apex:page>

 
and controller
Code:
public class conController {

public List<Contact> getContacts() {
        return [SELECT Id, Name, Account.Name, Phone, Email FROM Contact ORDER BY LastModifiedDate DESC LIMIT 10];
}

public Contact getContactRecord() {
        Id id = System.currentPageReference().getParameters().get('cid');
        return id == null — new Contact() : [SELECT Id, Name, AccountId FROM Contact WHERE Id = :id];
}

}

 

when you click on the names in the list on the left, the box on the right redraws (partial page) using ajax , the tag the you add is
<apex:actionSupport event="onclick" rerender="detail">

your example is just querying the database, and dumping a table, that is best done on the server in the controller, as shown above.



orikkerorikker
Hello, could you please help with controller for the Lead?

I have leads that have parent-child relationship with contacts.
I am trying to pull a list of leads where converted=False for example.
I will use this controller in the visualforce email template.

Thanks.
orikkerorikker
There is also some kind of error with your current controller

Error: Compile Error: unexpected char: 0xȸ4 at line 9 column 27