Skip to content

Instantly share code, notes, and snippets.

@ropable
Last active Oct 12, 2015
Embed
What would you like to do?
Using jQuery DataTables 1.10.9 with a server-side data source (via Django Tastypie)
$(function () {
// DataTables configuration.
conf = {
processing: true,
serverSide: true,
searching: false, // Disable search filter.
//searchDelay: 500,
ordering: false, // Disable column sorting.
pageLength: 25, // Initial number of results to show.
pagingType: "numbers", // Simple paging style.
ajax: function(data, callback, settings) {
data.limit = settings._iDisplayLength;
data.offset = settings._iDisplayStart;
$.get(
"{% url 'api_dispatch_list' api_name='v1' resource_name='referral' %}",
data, // Query parameters for the API call.
function(resp) {
// Map the API response to the DataTables format and pass it to DataTables callback.
callback({
recordsTotal: resp.meta.total_count,
recordsFiltered: resp.meta.total_count,
data: resp.objects
});
}
);
},
columns: [
{"data": "id"},
{
"data": "regions",
"render": function(data, type, row, meta) {
return data.map(function(obj) {return obj.name}).join(", ")
}
},
{"data": "referring_org.name"},
{"data": "type.name"},
{"data": "reference"},
{
"data": "referral_date",
"type": "date",
"render": function(data, type, row, meta) {
var d = new Date(data);
return d.toDateString();
}
},
{"data": "address"},
],
}
// Initialise the DataTable.
var dTable = $("table#referralsTable").DataTable(conf);
// Fill the filter select lists (after init).
prs.report.updateRegion();
prs.report.updateReferralType();
prs.report.updateOrganisation();
// Onchange events for select filters.
$("select#filter-region").change(function () {
dTable.ajax.reload();
});
$("select#filter-referralType").change(function () {
dTable.ajax.reload();
});
$("select#filter-organisation").change(function () {
dTable.ajax.reload();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment