pipeline.html 21 KB

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