DataTables - Passing Data to the Server in Client-Side processing mode
DataTables is AWESOME. I use it for list-based selection, table-based editing, data display and filtering and anything else I can think of because it is so robust that virtually anything is possible. Really.
If the dataset being used is fairly small (you may interpret small any way you like), DataTables client-side processing is amazing. It sorts and filters, updates all the navigation, lets you adjust the number of records displayed - as well as many more optional features - with very little effort.
Client-side processing - where filtering, paging and sorting calculations are all performed in the web-browser.
Ref: https://datatables.net/manual/data/#Client-side-processing
but
For client-side processing no additional data is submitted to the server
Ref: https://datatables.net/reference/option/ajax.data
The goal was to have a single client-side datatable which would display different content based on the user's actions. For example, 'list all the items with a status of "new" and a (time) segment of 1'.
HTML for the DataTable
<table id="datatable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Id</th>
<th>Vendor</th>
<th>Item</th>
<th>Date</th>
</tr>
</thead>
</table>
JavaScript
var datatable;
$(function($){
datatable = $('#datatable').DataTable(
{
"ajax": {
"url": "data-10-1.php"
},
"columns":
[
{"data": "link"},
{"data": "vendor"},
{"data": "item"},
{"data": "date"}
]
});
// Handle the click events
$(".summary").on("click", function(evt){
var target = evt.target, status, segment;
if (target.hasAttribute("data-status")) {
status=$(target).attr("data-status");
segment=$(target).attr("data-segment");
datatable.ajax.url("data-"+status+"-"+segment+".php");
datatable.ajax.reload();
datatable.draw('full-reset');
}
});
});
Notice that the URLs are variable. The status and segment values are passed in the URL itself, there is no POST or GET data.
Sample HTML which invokes the event handler. This HTML is inside the div with class="summary".
<span class="details" data-status="10" data-segment="4" data-status-text="new">84</span>
In order to extract the status and segment out of the URL, you can use Apache RewriteRules, like so:
<directory /var/www/html>
RewriteEngine On
RewriteRule ^(data)-(\d+)-(\d+)(\.php)$ $1$4?status=$2&segment=$3 [L]
</directory>
status and segment can then be accessed as $_GET variables. Be sure to validate them.
How does it work? As the user clicks on the spans, the event handler reads the data attributes and constructs a URL that is sent to the server. When Apache receives the request, it rewrites it to deliver the data embedded in the URL as GET parameters.
This post courtesy of Game Creek Video
Print article | This entry was posted by elvis on 10/26/17 at 06:13:00 pm . Follow any responses to this post through RSS 2.0. |