Skip to content

Instantly share code, notes, and snippets.

@wvpv
Created June 13, 2025 00:17
Show Gist options
  • Save wvpv/6dbdfea4351cfccd0ff459ced69c5586 to your computer and use it in GitHub Desktop.
Save wvpv/6dbdfea4351cfccd0ff459ced69c5586 to your computer and use it in GitHub Desktop.
DataTable AJAX example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DataTable</title>
<link href="https://cdn.datatables.net/2.3.2/css/dataTables.dataTables.min.css" rel="stylesheet" integrity="sha384-gC2LYLqCExndkNE9hTLhmEXvk8ZgIf42nRengHFbC9uaws2Ho0TW+ENGe4w15AHy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha384-NXgwF8Kv9SSAr+jemKKcbvQsz+teULH/a5UNJvZc6kP47hZgl62M1vGnw6gHQhb1" crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/2.3.2/js/dataTables.min.js" integrity="sha384-RZEqG156bBQSxYY9lwjUz/nKVkqYj/QNK9dEjjyJ/EVTO7ndWwk6ZWEkvaKdRm/U" crossorigin="anonymous"></script>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<table id="example" class="display">
<thead>
<tr>
<th>Subscriber Key</th>
<th>Email Address</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function() {
$('#example').DataTable({
serverSide: true,
ordering: false,
searching: false,
pageLength: 10,
columns: [
{ data: 'SubscriberKey' },
{ data: 'EmailAddress' },
{ data: 'FirstName' },
{ data: 'LastName' }
],
ajax: function(data, callback, settings) {
var page = Math.ceil(settings._iDisplayStart / settings._iDisplayLength) + 1;
$.ajax({
url: 'YOURHANDLERURLHERE',
method: 'GET',
data: {
page: page,
pageSize: settings._iDisplayLength
},
dataType: 'json',
success: function(response) {
callback({
draw: data.draw,
recordsTotal: response.recordsTotal,
recordsFiltered: response.recordsFiltered || response.recordsTotal,
data: response.data
});
},
error: function() {
console.error("unable to retrieve data");
}
});
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment