Skip to content

Instantly share code, notes, and snippets.

@itayw
Created November 1, 2015 12:12
Show Gist options
  • Save itayw/82c3106a14a651a2a9d4 to your computer and use it in GitHub Desktop.
Save itayw/82c3106a14a651a2a9d4 to your computer and use it in GitHub Desktop.
<link rel="stylesheet" href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<table id="simpletable">
<thead>
<tr>
</tr>
</thead>
</table>
var fields = [
{key: '', name:'', type:''}
];
$.fn.dataTable.pipeline = function ( opts ) {
return function ( request, drawCallback, settings ) {
console.log(arguments);
var query = {
timeframe: 'last_year',
dimensions: [],
metrics: [],
collection: 'statistics',
type: 'raw',
sort: [[ fields[request.order[0].column].key, request.order[0].dir.toUpperCase()]],
limit: request.length,
from: request.start
};
fields.forEach(function(f){
if (f.type === 'dimension')
query.dimensions.push(f);
else if (f.type === 'metric')
query.metrics.push(f);
});
if (request.search && request.search.value.length > 0)
query.filter = [
[fields[0].key, 'regex', request.search.value]
];
joola.fetch(query, function(err, results){
var _results = {data:[]};
results[0].documents.forEach(function(doc){
var _row = [];
Object.keys(doc).forEach(function(key){
_row.push(doc[key]);
});
_results.data.push(_row);
});
drawCallback(_results)
});
}
};
$('#simpletable').find('th').remove();
fields.forEach(function(f){
$('#simpletable thead tr').append('<th data-name="' + (f.name || f.key) + '">' + (f.name || f.key ) + '</th>');
});
$('#simpletable').DataTable( {
processing: true,
serverSide: true,
searching: false,
"ajax": $.fn.dataTable.pipeline( {} )
});
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment