I'm new to using JavaScript in Visualforce and have a basic question that I'm finding incredibly hard to locate the answer to:
I've brought data from my controller into a JS variable via JSON.serialize, and I'd like to take a look at what the object looks like when printed out. How do I print out JS variables onto a VF page? My goal is to create a table that outputs a list of contacts after I've applied my business logic to it. What would be the syntax to throw my array into something like an apex:repeat ?
What I've got:
Controller
public with sharing class ContactDynamicSearchController {
// Instance Variables
public List<Contact> contacts {get;set;}
// send query results to page
public List<Contact> runQuery() {
return [Select id, firstname, lastname From Contact];
}
}
VF Page:
<apex:page showHeader="true" sidebar="true" controller="ContactDynamicSearchController">
<script>
var contactArray = JSON.parse('{! contacts}');
// How do I print this out for testing?
</script>
<apex:pageBlock >
<h2>JavaScript version: </h2>
<h2>APEX Table Version:</h2>
<apex:pageBlockSection >
<apex:pageBlockTable value="{! contacts }" var="ct">
<apex:column value="{! ct.id }">
<apex:facet name="header">
ID
</apex:facet>
</apex:column>
<apex:column value="{! ct.firstname }">
<apex:facet name="header">
First Name
</apex:facet>
</apex:column>
<apex:column value="{! ct.lastname }">
<apex:facet name="header">
Last Name
</apex:facet>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>