Created
October 26, 2012 21:35
-
-
Save elmarcoh/3961705 to your computer and use it in GitHub Desktop.
Using Tastypie as an Ajax Source for DataTables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#participantes-table').dataTable({ | |
bProcessing: true, | |
bServerSIde: true, | |
sAjaxSource: "/path/to/your/tastypie/api/list/objects/?format=json", | |
sAjaxDataProp: "objects", | |
aoColumns: [ | |
// Put the resource name that corresponds to each table column | |
{'mData': "your"}, | |
{'mData': "columns"}, | |
], | |
fnServerData: function(source, oaData, callback, settings) { | |
settings.jqXHR = $.get( | |
source, | |
function(data){ | |
// Magic happens here! tastypie provides the data but | |
// stupid dataTables requires it to have stupid names | |
data.echo = oaData.echo; | |
data.iTotalRecords = data.meta.total_count; | |
data.iTotalisplayRecords = data.meta.limit; | |
console.debug(data); | |
console.debug(callback); | |
callback(data); | |
}, | |
'json') | |
}, | |
}); |
Thanks a LOT for this snippets. I beleive DataTable and Tastypie are really great ! Wonder how other people do, actually :)
The previous answers were not working for me. Here is minor modification of what I ended-up. I activated sorting and searching as well.
conf = {
processing: true,
serverSide: true,
bProcessing: true,
bServerSIde: true,
sAjaxSource: '{% url 'api_dispatch_list' 'v1' 'your_resource_name' %}',
searchDelay: 1000,
sAjaxDataProp: "objects",
aoColumns: [
// Put the resource name that corresponds to each table column
{'mData': "you_resource_field"},
],
fnServerData: function(source, oaData, callback, settings) {
// Convert parameters from DataTable to Tastypie
var param = {
'limit': settings._iDisplayLength,
'offset': settings._iDisplayStart,
}
// Handle search ... to be customized of course
if (oaData[10] !== undefined) {
param['header__icontains'] = oaData[10].value
}
// Handle sorting ... to be customized of course
var header_sorted = oaData[12].value;
var header_sort_direction = oaData[13].value;
if (header_sorted === 0) {
if (header_sort_direction === 'asc') {
param['order_by'] = 'header';
}
if (header_sort_direction === 'desc') {
param['order_by'] = '-header';
}
}
settings.jqXHR = $.ajax({
url: source,
data: param,
success: function(data) {
data.sEcho = oaData.sEcho;
data.iTotalRecords = data.meta.total_count;
data.iTotalDisplayRecords = data.meta.total_count;
callback(data);
},
dataType: 'json'
})
},
}
$('table').DataTable(conf);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much for sharing this Gist. Worked like a charm!
Had to change:
To this:
Now, pagination wasn't working (clicked on the buttons but never got different results) so after checking the docs changed oaData to aoData and that fixed it.