Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
875 lars 1
<!DOCTYPE html>
2
<html>
3
<head>
4
	<meta charset="utf-8">
5
	<link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">
6
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
7
 
8
	<title>DataTables example - Pipelining data to reduce Ajax calls for paging</title>
9
	<link rel="stylesheet" type="text/css" href="../../media/css/jquery.dataTables.css">
10
	<link rel="stylesheet" type="text/css" href="../resources/syntax/shCore.css">
11
	<link rel="stylesheet" type="text/css" href="../resources/demo.css">
12
	<style type="text/css" class="init">
13
 
14
	</style>
15
	<script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
16
	<script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js"></script>
17
	<script type="text/javascript" language="javascript" src="../resources/syntax/shCore.js"></script>
18
	<script type="text/javascript" language="javascript" src="../resources/demo.js"></script>
19
	<script type="text/javascript" language="javascript" class="init">
20
 
21
 
22
//
23
// Pipelining function for DataTables. To be used to the `ajax` option of DataTables
24
//
25
$.fn.dataTable.pipeline = function ( opts ) {
26
	// Configuration options
27
	var conf = $.extend( {
28
		pages: 5,     // number of pages to cache
29
		url: '',      // script url
30
		data: null,   // function or object with parameters to send to the server
31
		              // matching how `ajax.data` works in DataTables
32
		method: 'GET' // Ajax HTTP method
33
	}, opts );
34
 
35
	// Private variables for storing the cache
36
	var cacheLower = -1;
37
	var cacheUpper = null;
38
	var cacheLastRequest = null;
39
	var cacheLastJson = null;
40
 
41
	return function ( request, drawCallback, settings ) {
42
		var ajax          = false;
43
		var requestStart  = request.start;
44
		var drawStart     = request.start;
45
		var requestLength = request.length;
46
		var requestEnd    = requestStart + requestLength;
47
 
48
		if ( settings.clearCache ) {
49
			// API requested that the cache be cleared
50
			ajax = true;
51
			settings.clearCache = false;
52
		}
53
		else if ( cacheLower < 0 || requestStart < cacheLower || requestEnd > cacheUpper ) {
54
			// outside cached data - need to make a request
55
			ajax = true;
56
		}
57
		else if ( JSON.stringify( request.order )   !== JSON.stringify( cacheLastRequest.order ) ||
58
		          JSON.stringify( request.columns ) !== JSON.stringify( cacheLastRequest.columns ) ||
59
		          JSON.stringify( request.search )  !== JSON.stringify( cacheLastRequest.search )
60
		) {
61
			// properties changed (ordering, columns, searching)
62
			ajax = true;
63
		}
64
 
65
		// Store the request for checking next time around
66
		cacheLastRequest = $.extend( true, {}, request );
67
 
68
		if ( ajax ) {
69
			// Need data from the server
70
			if ( requestStart < cacheLower ) {
71
				requestStart = requestStart - (requestLength*(conf.pages-1));
72
 
73
				if ( requestStart < 0 ) {
74
					requestStart = 0;
75
				}
76
			}
77
 
78
			cacheLower = requestStart;
79
			cacheUpper = requestStart + (requestLength * conf.pages);
80
 
81
			request.start = requestStart;
82
			request.length = requestLength*conf.pages;
83
 
84
			// Provide the same `data` options as DataTables.
85
			if ( $.isFunction ( conf.data ) ) {
86
				// As a function it is executed with the data object as an arg
87
				// for manipulation. If an object is returned, it is used as the
88
				// data object to submit
89
				var d = conf.data( request );
90
				if ( d ) {
91
					$.extend( request, d );
92
				}
93
			}
94
			else if ( $.isPlainObject( conf.data ) ) {
95
				// As an object, the data given extends the default
96
				$.extend( request, conf.data );
97
			}
98
 
99
			settings.jqXHR = $.ajax( {
100
				"type":     conf.method,
101
				"url":      conf.url,
102
				"data":     request,
103
				"dataType": "json",
104
				"cache":    false,
105
				"success":  function ( json ) {
106
					cacheLastJson = $.extend(true, {}, json);
107
 
108
					if ( cacheLower != drawStart ) {
109
						json.data.splice( 0, drawStart-cacheLower );
110
					}
111
					json.data.splice( requestLength, json.data.length );
112
 
113
					drawCallback( json );
114
				}
115
			} );
116
		}
117
		else {
118
			json = $.extend( true, {}, cacheLastJson );
119
			json.draw = request.draw; // Update the echo for each response
120
			json.data.splice( 0, requestStart-cacheLower );
121
			json.data.splice( requestLength, json.data.length );
122
 
123
			drawCallback(json);
124
		}
125
	}
126
};
127
 
128
// Register an API method that will empty the pipelined data, forcing an Ajax
129
// fetch on the next draw (i.e. `table.clearPipeline().draw()`)
130
$.fn.dataTable.Api.register( 'clearPipeline()', function () {
131
	return this.iterator( 'table', function ( settings ) {
132
		settings.clearCache = true;
133
	} );
134
} );
135
 
136
 
137
//
138
// DataTables initialisation
139
//
140
$(document).ready(function() {
141
	$('#example').DataTable( {
142
		"processing": true,
143
		"serverSide": true,
144
		"ajax": $.fn.dataTable.pipeline( {
145
			url: 'scripts/server_processing.php',
146
			pages: 5 // number of pages to cache
147
		} )
148
	} );
149
} );
150
 
151
 
152
	</script>
153
</head>
154
 
155
<body class="dt-example">
156
	<div class="container">
157
		<section>
158
			<h1>DataTables example <span>Pipelining data to reduce Ajax calls for paging</span></h1>
159
 
160
			<div class="info">
161
				<p>Server-side processing can be quite hard on your server, since it makes an Ajax call to the server for every draw request that is made. On sites with a large
162
				number of page views, you could potentially end up DDoSing your own server with your own applications!</p>
163
 
164
				<p>This example shows one technique to reduce the number of Ajax calls that are made to the server by caching more data than is needed for each draw. This is done
165
				by intercepting the Ajax call and routing it through a data cache control; using the data from the cache if available, and making the Ajax request if not. This
166
				intercept of the Ajax request is performed by giving the <a href="//datatables.net/reference/option/ajax"><code class="option" title=
167
				"DataTables initialisation option">ajax</code></a> option as a function. This function then performs the logic of deciding if another Ajax call is needed, or if
168
				data from the cache can be used.</p>
169
 
170
				<p>Keep in mind that this caching is for paging only; the pipeline must be cleared for other interactions such as ordering and searching since the full data set,
171
				when using server-side processing, is only available at the server.</p>
172
			</div>
173
 
174
			<table id="example" class="display" cellspacing="0" width="100%">
175
				<thead>
176
					<tr>
177
						<th>First name</th>
178
						<th>Last name</th>
179
						<th>Position</th>
180
						<th>Office</th>
181
						<th>Start date</th>
182
						<th>Salary</th>
183
					</tr>
184
				</thead>
185
 
186
				<tfoot>
187
					<tr>
188
						<th>First name</th>
189
						<th>Last name</th>
190
						<th>Position</th>
191
						<th>Office</th>
192
						<th>Start date</th>
193
						<th>Salary</th>
194
					</tr>
195
				</tfoot>
196
			</table>
197
 
198
			<ul class="tabs">
199
				<li class="active">Javascript</li>
200
				<li>HTML</li>
201
				<li>CSS</li>
202
				<li>Ajax</li>
203
				<li>Server-side script</li>
204
			</ul>
205
 
206
			<div class="tabs">
207
				<div class="js">
208
					<p>The Javascript shown below is used to initialise the table shown in this example:</p><code class="multiline language-js">//
209
// Pipelining function for DataTables. To be used to the `ajax` option of DataTables
210
//
211
$.fn.dataTable.pipeline = function ( opts ) {
212
	// Configuration options
213
	var conf = $.extend( {
214
		pages: 5,     // number of pages to cache
215
		url: '',      // script url
216
		data: null,   // function or object with parameters to send to the server
217
		              // matching how `ajax.data` works in DataTables
218
		method: 'GET' // Ajax HTTP method
219
	}, opts );
220
 
221
	// Private variables for storing the cache
222
	var cacheLower = -1;
223
	var cacheUpper = null;
224
	var cacheLastRequest = null;
225
	var cacheLastJson = null;
226
 
227
	return function ( request, drawCallback, settings ) {
228
		var ajax          = false;
229
		var requestStart  = request.start;
230
		var drawStart     = request.start;
231
		var requestLength = request.length;
232
		var requestEnd    = requestStart + requestLength;
233
 
234
		if ( settings.clearCache ) {
235
			// API requested that the cache be cleared
236
			ajax = true;
237
			settings.clearCache = false;
238
		}
239
		else if ( cacheLower &lt; 0 || requestStart &lt; cacheLower || requestEnd &gt; cacheUpper ) {
240
			// outside cached data - need to make a request
241
			ajax = true;
242
		}
243
		else if ( JSON.stringify( request.order )   !== JSON.stringify( cacheLastRequest.order ) ||
244
		          JSON.stringify( request.columns ) !== JSON.stringify( cacheLastRequest.columns ) ||
245
		          JSON.stringify( request.search )  !== JSON.stringify( cacheLastRequest.search )
246
		) {
247
			// properties changed (ordering, columns, searching)
248
			ajax = true;
249
		}
250
 
251
		// Store the request for checking next time around
252
		cacheLastRequest = $.extend( true, {}, request );
253
 
254
		if ( ajax ) {
255
			// Need data from the server
256
			if ( requestStart &lt; cacheLower ) {
257
				requestStart = requestStart - (requestLength*(conf.pages-1));
258
 
259
				if ( requestStart &lt; 0 ) {
260
					requestStart = 0;
261
				}
262
			}
263
 
264
			cacheLower = requestStart;
265
			cacheUpper = requestStart + (requestLength * conf.pages);
266
 
267
			request.start = requestStart;
268
			request.length = requestLength*conf.pages;
269
 
270
			// Provide the same `data` options as DataTables.
271
			if ( $.isFunction ( conf.data ) ) {
272
				// As a function it is executed with the data object as an arg
273
				// for manipulation. If an object is returned, it is used as the
274
				// data object to submit
275
				var d = conf.data( request );
276
				if ( d ) {
277
					$.extend( request, d );
278
				}
279
			}
280
			else if ( $.isPlainObject( conf.data ) ) {
281
				// As an object, the data given extends the default
282
				$.extend( request, conf.data );
283
			}
284
 
285
			settings.jqXHR = $.ajax( {
286
				&quot;type&quot;:     conf.method,
287
				&quot;url&quot;:      conf.url,
288
				&quot;data&quot;:     request,
289
				&quot;dataType&quot;: &quot;json&quot;,
290
				&quot;cache&quot;:    false,
291
				&quot;success&quot;:  function ( json ) {
292
					cacheLastJson = $.extend(true, {}, json);
293
 
294
					if ( cacheLower != drawStart ) {
295
						json.data.splice( 0, drawStart-cacheLower );
296
					}
297
					json.data.splice( requestLength, json.data.length );
298
 
299
					drawCallback( json );
300
				}
301
			} );
302
		}
303
		else {
304
			json = $.extend( true, {}, cacheLastJson );
305
			json.draw = request.draw; // Update the echo for each response
306
			json.data.splice( 0, requestStart-cacheLower );
307
			json.data.splice( requestLength, json.data.length );
308
 
309
			drawCallback(json);
310
		}
311
	}
312
};
313
 
314
// Register an API method that will empty the pipelined data, forcing an Ajax
315
// fetch on the next draw (i.e. `table.clearPipeline().draw()`)
316
$.fn.dataTable.Api.register( 'clearPipeline()', function () {
317
	return this.iterator( 'table', function ( settings ) {
318
		settings.clearCache = true;
319
	} );
320
} );
321
 
322
 
323
//
324
// DataTables initialisation
325
//
326
$(document).ready(function() {
327
	$('#example').DataTable( {
328
		&quot;processing&quot;: true,
329
		&quot;serverSide&quot;: true,
330
		&quot;ajax&quot;: $.fn.dataTable.pipeline( {
331
			url: 'scripts/server_processing.php',
332
			pages: 5 // number of pages to cache
333
		} )
334
	} );
335
} );</code>
336
 
337
					<p>In addition to the above code, the following Javascript library files are loaded for use in this example:</p>
338
 
339
					<ul>
340
						<li><a href="//code.jquery.com/jquery-1.11.3.min.js">//code.jquery.com/jquery-1.11.3.min.js</a></li>
341
						<li><a href="../../media/js/jquery.dataTables.js">../../media/js/jquery.dataTables.js</a></li>
342
					</ul>
343
				</div>
344
 
345
				<div class="table">
346
					<p>The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:</p>
347
				</div>
348
 
349
				<div class="css">
350
					<div>
351
						<p>This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The
352
						additional CSS used is shown below:</p><code class="multiline language-css"></code>
353
					</div>
354
 
355
					<p>The following CSS library files are loaded for use in this example to provide the styling of the table:</p>
356
 
357
					<ul>
358
						<li><a href="../../media/css/jquery.dataTables.css">../../media/css/jquery.dataTables.css</a></li>
359
					</ul>
360
				</div>
361
 
362
				<div class="ajax">
363
					<p>This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is
364
					loaded.</p>
365
				</div>
366
 
367
				<div class="php">
368
					<p>The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side
369
					processing scripts can be written in any language, using <a href="//datatables.net/manual/server-side">the protocol described in the DataTables
370
					documentation</a>.</p>
371
				</div>
372
			</div>
373
		</section>
374
	</div>
375
 
376
	<section>
377
		<div class="footer">
378
			<div class="gradient"></div>
379
 
380
			<div class="liner">
381
				<h2>Other examples</h2>
382
 
383
				<div class="toc">
384
					<div class="toc-group">
385
						<h3><a href="../basic_init/index.html">Basic initialisation</a></h3>
386
						<ul class="toc">
387
							<li><a href="../basic_init/zero_configuration.html">Zero configuration</a></li>
388
							<li><a href="../basic_init/filter_only.html">Feature enable / disable</a></li>
389
							<li><a href="../basic_init/table_sorting.html">Default ordering (sorting)</a></li>
390
							<li><a href="../basic_init/multi_col_sort.html">Multi-column ordering</a></li>
391
							<li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
392
							<li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
393
							<li><a href="../basic_init/complex_header.html">Complex headers (rowspan and colspan)</a></li>
394
							<li><a href="../basic_init/dom.html">DOM positioning</a></li>
395
							<li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
396
							<li><a href="../basic_init/state_save.html">State saving</a></li>
397
							<li><a href="../basic_init/alt_pagination.html">Alternative pagination</a></li>
398
							<li><a href="../basic_init/scroll_y.html">Scroll - vertical</a></li>
399
							<li><a href="../basic_init/scroll_y_dynamic.html">Scroll - vertical, dynamic height</a></li>
400
							<li><a href="../basic_init/scroll_x.html">Scroll - horizontal</a></li>
401
							<li><a href="../basic_init/scroll_xy.html">Scroll - horizontal and vertical</a></li>
402
							<li><a href="../basic_init/comma-decimal.html">Language - Comma decimal place</a></li>
403
							<li><a href="../basic_init/language.html">Language options</a></li>
404
						</ul>
405
					</div>
406
 
407
					<div class="toc-group">
408
						<h3><a href="../advanced_init/index.html">Advanced initialisation</a></h3>
409
						<ul class="toc">
410
							<li><a href="../advanced_init/events_live.html">DOM / jQuery events</a></li>
411
							<li><a href="../advanced_init/dt_events.html">DataTables events</a></li>
412
							<li><a href="../advanced_init/column_render.html">Column rendering</a></li>
413
							<li><a href="../advanced_init/length_menu.html">Page length options</a></li>
414
							<li><a href="../advanced_init/dom_multiple_elements.html">Multiple table control elements</a></li>
415
							<li><a href="../advanced_init/complex_header.html">Complex headers with column visibility</a></li>
416
							<li><a href="../advanced_init/object_dom_read.html">Read HTML to data objects</a></li>
417
							<li><a href="../advanced_init/html5-data-attributes.html">HTML5 data-* attributes - cell data</a></li>
418
							<li><a href="../advanced_init/html5-data-options.html">HTML5 data-* attributes - table options</a></li>
419
							<li><a href="../advanced_init/language_file.html">Language file</a></li>
420
							<li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
421
							<li><a href="../advanced_init/row_callback.html">Row created callback</a></li>
422
							<li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
423
							<li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
424
							<li><a href="../advanced_init/dom_toolbar.html">Custom toolbar elements</a></li>
425
							<li><a href="../advanced_init/sort_direction_control.html">Order direction sequence control</a></li>
426
						</ul>
427
					</div>
428
 
429
					<div class="toc-group">
430
						<h3><a href="../styling/index.html">Styling</a></h3>
431
						<ul class="toc">
432
							<li><a href="../styling/display.html">Base style</a></li>
433
							<li><a href="../styling/no-classes.html">Base style - no styling classes</a></li>
434
							<li><a href="../styling/cell-border.html">Base style - cell borders</a></li>
435
							<li><a href="../styling/compact.html">Base style - compact</a></li>
436
							<li><a href="../styling/hover.html">Base style - hover</a></li>
437
							<li><a href="../styling/order-column.html">Base style - order-column</a></li>
438
							<li><a href="../styling/row-border.html">Base style - row borders</a></li>
439
							<li><a href="../styling/stripe.html">Base style - stripe</a></li>
440
							<li><a href="../styling/bootstrap.html">Bootstrap</a></li>
441
							<li><a href="../styling/foundation.html">Foundation</a></li>
442
							<li><a href="../styling/jqueryUI.html">jQuery UI ThemeRoller</a></li>
443
						</ul>
444
					</div>
445
 
446
					<div class="toc-group">
447
						<h3><a href="../data_sources/index.html">Data sources</a></h3>
448
						<ul class="toc">
449
							<li><a href="../data_sources/dom.html">HTML (DOM) sourced data</a></li>
450
							<li><a href="../data_sources/ajax.html">Ajax sourced data</a></li>
451
							<li><a href="../data_sources/js_array.html">Javascript sourced data</a></li>
452
							<li><a href="../data_sources/server_side.html">Server-side processing</a></li>
453
						</ul>
454
					</div>
455
 
456
					<div class="toc-group">
457
						<h3><a href="../api/index.html">API</a></h3>
458
						<ul class="toc">
459
							<li><a href="../api/add_row.html">Add rows</a></li>
460
							<li><a href="../api/multi_filter.html">Individual column searching (text inputs)</a></li>
461
							<li><a href="../api/multi_filter_select.html">Individual column searching (select inputs)</a></li>
462
							<li><a href="../api/highlight.html">Highlighting rows and columns</a></li>
463
							<li><a href="../api/row_details.html">Child rows (show extra / detailed information)</a></li>
464
							<li><a href="../api/select_row.html">Row selection (multiple rows)</a></li>
465
							<li><a href="../api/select_single_row.html">Row selection and deletion (single row)</a></li>
466
							<li><a href="../api/form.html">Form inputs</a></li>
467
							<li><a href="../api/counter_columns.html">Index column</a></li>
468
							<li><a href="../api/show_hide.html">Show / hide columns dynamically</a></li>
469
							<li><a href="../api/api_in_init.html">Using API in callbacks</a></li>
470
							<li><a href="../api/tabs_and_scrolling.html">Scrolling and Bootstrap tabs</a></li>
471
							<li><a href="../api/regex.html">Search API (regular expressions)</a></li>
472
						</ul>
473
					</div>
474
 
475
					<div class="toc-group">
476
						<h3><a href="../ajax/index.html">Ajax</a></h3>
477
						<ul class="toc">
478
							<li><a href="../ajax/simple.html">Ajax data source (arrays)</a></li>
479
							<li><a href="../ajax/objects.html">Ajax data source (objects)</a></li>
480
							<li><a href="../ajax/deep.html">Nested object data (objects)</a></li>
481
							<li><a href="../ajax/objects_subarrays.html">Nested object data (arrays)</a></li>
482
							<li><a href="../ajax/orthogonal-data.html">Orthogonal data</a></li>
483
							<li><a href="../ajax/null_data_source.html">Generated content for a column</a></li>
484
							<li><a href="../ajax/custom_data_property.html">Custom data source property</a></li>
485
							<li><a href="../ajax/custom_data_flat.html">Flat array data source</a></li>
486
							<li><a href="../ajax/defer_render.html">Deferred rendering for speed</a></li>
487
						</ul>
488
					</div>
489
 
490
					<div class="toc-group">
491
						<h3><a href="./index.html">Server-side</a></h3>
492
						<ul class="toc active">
493
							<li><a href="./simple.html">Server-side processing</a></li>
494
							<li><a href="./custom_vars.html">Custom HTTP variables</a></li>
495
							<li><a href="./post.html">POST data</a></li>
496
							<li><a href="./ids.html">Automatic addition of row ID attributes</a></li>
497
							<li><a href="./object_data.html">Object data source</a></li>
498
							<li><a href="./row_details.html">Row details</a></li>
499
							<li><a href="./select_rows.html">Row selection</a></li>
500
							<li><a href="./jsonp.html">JSONP data source for remote domains</a></li>
501
							<li><a href="./defer_loading.html">Deferred loading of data</a></li>
502
							<li class="active"><a href="./pipeline.html">Pipelining data to reduce Ajax calls for paging</a></li>
503
						</ul>
504
					</div>
505
 
506
					<div class="toc-group">
507
						<h3><a href="../plug-ins/index.html">Plug-ins</a></h3>
508
						<ul class="toc">
509
							<li><a href="../plug-ins/api.html">API plug-in methods</a></li>
510
							<li><a href="../plug-ins/sorting_auto.html">Ordering plug-ins (with type detection)</a></li>
511
							<li><a href="../plug-ins/sorting_manual.html">Ordering plug-ins (no type detection)</a></li>
512
							<li><a href="../plug-ins/range_filtering.html">Custom filtering - range search</a></li>
513
							<li><a href="../plug-ins/dom_sort.html">Live DOM ordering</a></li>
514
						</ul>
515
					</div>
516
				</div>
517
 
518
				<div class="epilogue">
519
					<p>Please refer to the <a href="http://www.datatables.net">DataTables documentation</a> for full information about its API properties and methods.<br>
520
					Additionally, there are a wide range of <a href="http://www.datatables.net/extensions">extensions</a> and <a href=
521
					"http://www.datatables.net/plug-ins">plug-ins</a> which extend the capabilities of DataTables.</p>
522
 
523
					<p class="copyright">DataTables designed and created by <a href="http://www.sprymedia.co.uk">SpryMedia Ltd</a> &#169; 2007-2015<br>
524
					DataTables is licensed under the <a href="http://www.datatables.net/mit">MIT license</a>.</p>
525
				</div>
526
			</div>
527
		</div>
528
	</section>
529
</body>
530
</html>