Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Last active December 10, 2015 22:38
Show Gist options
  • Save jtarleton/4503930 to your computer and use it in GitHub Desktop.
Save jtarleton/4503930 to your computer and use it in GitHub Desktop.
Basic datatables ajax implementation w/ JSON
<html>
<head>
<script type="text/javascript" src="datatables.js"></script>
</head>
<div class="well box">
<table id="example">
<thead>
<tr><th>id</th><th>name</th></tr>
</thead>
<tfoot>
<tr><th></th><th></th></tr>
</tfoot>
</table>
</div>
</html>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#example').dataTable({
"sScrollY" : "420px",
"aaSorting" : [[ 1, "desc" ]],
"bPaginate" : true,
"bDeferRender" : false,
"bScrollAutoCss" : true,
"bScrollCollapse" : true,
"bStateSave" : true,
"bProcessing" : true,
"sPaginationType" : "full_numbers",
"iDisplayLength" : 100,
"bServerSide" : false,
"sAjaxSource" : "http://www.domain.com/json_out.php",
"fnInitComplete" : function(oSettings, json) {
},
"aoColumns" : [
{ "mData" : "_id" },
{ "mData" : "name" },
],
"oLanguage" : {
"sSearch" : "Filter across all data:"
}
});
});
</script>
<?php
/*
http://www.datatables.net/
*/
class DataTableCallbackMaker
{
static public function getJSON($input)
{
$output = array();
$cols = array('_id','name');
for($i =0;$i<3;$i++)
{
foreach($cols as $col)
$output['aaData'][$i][$col] = "Row $i";
}
return json_encode($output);
}
}
$input = $_GET;
DataTableCallbackMaker::getJSON($input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment