I'm trying to use sAjaxSource with server side processing from Apex class which will provide JSON as source.
First I was trying simple example, which won't work and won't output any error (not in Salesforce and not in JS console), I'm just getting empty JQuery DataTable.
Visualforce page:
<apex:page controller="vfDBTestController">
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js" />
<apex:stylesheet value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css"/>
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js" />
<apex:includeScript value="{!URLFOR($Resource.DataTables_1_9_4, 'DataTables-1.9.4/media/js/jquery.dataTables.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.DataTables_1_9_4, 'DataTables-1.9.4/media/css/jquery.dataTables_themeroller.css')}" />
<script type="text/javascript">
var j$ = jQuery.noConflict();
j$(document).ready( function ()
{
j$('#tblid').dataTable(
{
"bProcessing" : true,
"sAjaxSource" : {!JSONDataSource}
} );
} );
</script>
<apex:form >
<table cellpadding="0" cellspacing="0" border="0" class="display" id="tblid">
<thead>
<th>cell1</th>
<th>cell2</th>
</thead>
<tbody>
</tbody>
</table>
</apex:form>
</apex:page>
Apex:
public with sharing class vfDBTestController
{
public vfDBTestController(){}
public String getJSONDataSource()
{
List<Account> lstAaData = [SELECT Id, Name FROM Account];
// Create a JSONGenerator object.
// Pass true to the constructor for pretty print formatting.
JSONGenerator gen = JSON.createGenerator(true);
// Write data to the JSON string.
gen.writeStartObject();
gen.writeFieldName('aaData');
gen.writeStartArray();
for (Account acc : lstAaData)
{
gen.writeStartObject();
gen.writeStringField('cell1', acc.Name);
gen.writeStringField('cell2', acc.Id);
gen.writeEndObject();
}
gen.writeEndArray();
gen.writeEndObject();
String JSONString = gen.getAsString();
system.debug('JSONString: ' + JSONString);
return JSONString;
}
}
JSON output from system debug:
{"aaData":[{"cell1" : "GenePoint","cell2" : "001b000000MTqJJAA1"},{"cell1" : "United Oil & Gas, UK","cell2" : "001b000000MTqJKAA1"},{"cell1" : "United Oil & Gas, Singapore","cell2" : "001b000000MTqJLAA1"}]}
Best Answer
I have found an answer with help from here. I will give a small example, which shows how to use this technique related to JQuery DataTable.
Note: this is only small example that shows how one can communicate with JQuery DataTable and Apex, to get a good grasp on the matter. Hope it will help more people who trying this.
Ajax Response VF page:
AjaxRespController:
VF client page: