pipeline.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  5. <link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/media/images/favicon.ico" />
  6. <title>DataTables example</title>
  7. <style type="text/css" title="currentStyle">
  8. @import "../../media/css/demo_page.css";
  9. @import "../../media/css/demo_table.css";
  10. </style>
  11. <script type="text/javascript" language="javascript" src="../../media/js/jquery.js"></script>
  12. <script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js"></script>
  13. <script type="text/javascript" charset="utf-8">
  14. var oCache = {
  15. iCacheLower: -1
  16. };
  17. function fnSetKey( aoData, sKey, mValue )
  18. {
  19. for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
  20. {
  21. if ( aoData[i].name == sKey )
  22. {
  23. aoData[i].value = mValue;
  24. }
  25. }
  26. }
  27. function fnGetKey( aoData, sKey )
  28. {
  29. for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
  30. {
  31. if ( aoData[i].name == sKey )
  32. {
  33. return aoData[i].value;
  34. }
  35. }
  36. return null;
  37. }
  38. function fnDataTablesPipeline ( sSource, aoData, fnCallback, oSettings ) {
  39. var iPipe = 5; /* Ajust the pipe size */
  40. var bNeedServer = false;
  41. var sEcho = fnGetKey(aoData, "sEcho");
  42. var iRequestStart = fnGetKey(aoData, "iDisplayStart");
  43. var iRequestLength = fnGetKey(aoData, "iDisplayLength");
  44. var iRequestEnd = iRequestStart + iRequestLength;
  45. oCache.iDisplayStart = iRequestStart;
  46. /* outside pipeline? */
  47. if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
  48. {
  49. bNeedServer = true;
  50. }
  51. /* sorting etc changed? */
  52. if ( oCache.lastRequest && !bNeedServer )
  53. {
  54. for( var i=0, iLen=aoData.length ; i<iLen ; i++ )
  55. {
  56. if ( aoData[i].name != "iDisplayStart" && aoData[i].name != "iDisplayLength" && aoData[i].name != "sEcho" )
  57. {
  58. if ( aoData[i].value != oCache.lastRequest[i].value )
  59. {
  60. bNeedServer = true;
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. /* Store the request for checking next time around */
  67. oCache.lastRequest = aoData.slice();
  68. if ( bNeedServer )
  69. {
  70. if ( iRequestStart < oCache.iCacheLower )
  71. {
  72. iRequestStart = iRequestStart - (iRequestLength*(iPipe-1));
  73. if ( iRequestStart < 0 )
  74. {
  75. iRequestStart = 0;
  76. }
  77. }
  78. oCache.iCacheLower = iRequestStart;
  79. oCache.iCacheUpper = iRequestStart + (iRequestLength * iPipe);
  80. oCache.iDisplayLength = fnGetKey( aoData, "iDisplayLength" );
  81. fnSetKey( aoData, "iDisplayStart", iRequestStart );
  82. fnSetKey( aoData, "iDisplayLength", iRequestLength*iPipe );
  83. oSettings.jqXHR = $.getJSON( sSource, aoData, function (json) {
  84. /* Callback processing */
  85. oCache.lastJson = jQuery.extend(true, {}, json);
  86. if ( oCache.iCacheLower != oCache.iDisplayStart )
  87. {
  88. json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
  89. }
  90. json.aaData.splice( oCache.iDisplayLength, json.aaData.length );
  91. fnCallback(json)
  92. } );
  93. }
  94. else
  95. {
  96. json = jQuery.extend(true, {}, oCache.lastJson);
  97. json.sEcho = sEcho; /* Update the echo for each response */
  98. json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
  99. json.aaData.splice( iRequestLength, json.aaData.length );
  100. fnCallback(json);
  101. return;
  102. }
  103. }
  104. $(document).ready(function() {
  105. $('#example').dataTable( {
  106. "bProcessing": true,
  107. "bServerSide": true,
  108. "sAjaxSource": "scripts/server_processing.php",
  109. "fnServerData": fnDataTablesPipeline
  110. } );
  111. } );
  112. </script>
  113. </head>
  114. <body id="dt_example">
  115. <div id="container">
  116. <div class="full_width big">
  117. DataTables server-side processing with pipelining example
  118. </div>
  119. <h1>Preamble</h1>
  120. <p>When using server-side processing with DataTables, it can be quite intensive on your server having an Ajax call every time the user performs some kind of interaction - you can effectively DDOS your server with your own application!</p>
  121. <p>This example shows how you might over-come this by modifying the request set to the server to retrieve more information than is actually required for a single page's display. This means that the user can page multiple times (5 times the display size is the default) before a request must be made of the server. Paging is typically the most common interaction performed with a DataTable, so this can be most beneficial to your server's resource usage. Of course the pipeline must be cleared for interactions other than paging (sorting, filtering etc), but that's the trade off that can be made (sending extra information is cheap - while another XHR is expensive).</p>
  122. <h1>Live example</h1>
  123. <div id="dynamic">
  124. <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
  125. <thead>
  126. <tr>
  127. <th width="20%">Rendering engine</th>
  128. <th width="25%">Browser</th>
  129. <th width="25%">Platform(s)</th>
  130. <th width="15%">Engine version</th>
  131. <th width="15%">CSS grade</th>
  132. </tr>
  133. </thead>
  134. <tbody>
  135. <tr>
  136. <td colspan="5" class="dataTables_empty">Loading data from server</td>
  137. </tr>
  138. </tbody>
  139. <tfoot>
  140. <tr>
  141. <th>Rendering engine</th>
  142. <th>Browser</th>
  143. <th>Platform(s)</th>
  144. <th>Engine version</th>
  145. <th>CSS grade</th>
  146. </tr>
  147. </tfoot>
  148. </table>
  149. </div>
  150. <div class="spacer"></div>
  151. <h1>Initialisation code</h1>
  152. <pre class="brush: js;">var oCache = {
  153. iCacheLower: -1
  154. };
  155. function fnSetKey( aoData, sKey, mValue )
  156. {
  157. for ( var i=0, iLen=aoData.length ; i&lt;iLen ; i++ )
  158. {
  159. if ( aoData[i].name == sKey )
  160. {
  161. aoData[i].value = mValue;
  162. }
  163. }
  164. }
  165. function fnGetKey( aoData, sKey )
  166. {
  167. for ( var i=0, iLen=aoData.length ; i&lt;iLen ; i++ )
  168. {
  169. if ( aoData[i].name == sKey )
  170. {
  171. return aoData[i].value;
  172. }
  173. }
  174. return null;
  175. }
  176. function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
  177. var iPipe = 5; /* Ajust the pipe size */
  178. var bNeedServer = false;
  179. var sEcho = fnGetKey(aoData, "sEcho");
  180. var iRequestStart = fnGetKey(aoData, "iDisplayStart");
  181. var iRequestLength = fnGetKey(aoData, "iDisplayLength");
  182. var iRequestEnd = iRequestStart + iRequestLength;
  183. oCache.iDisplayStart = iRequestStart;
  184. /* outside pipeline? */
  185. if ( oCache.iCacheLower &lt; 0 || iRequestStart &lt; oCache.iCacheLower || iRequestEnd &gt; oCache.iCacheUpper )
  186. {
  187. bNeedServer = true;
  188. }
  189. /* sorting etc changed? */
  190. if ( oCache.lastRequest &amp;&amp; !bNeedServer )
  191. {
  192. for( var i=0, iLen=aoData.length ; i&lt;iLen ; i++ )
  193. {
  194. if ( aoData[i].name != "iDisplayStart" &amp;&amp; aoData[i].name != "iDisplayLength" &amp;&amp; aoData[i].name != "sEcho" )
  195. {
  196. if ( aoData[i].value != oCache.lastRequest[i].value )
  197. {
  198. bNeedServer = true;
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. /* Store the request for checking next time around */
  205. oCache.lastRequest = aoData.slice();
  206. if ( bNeedServer )
  207. {
  208. if ( iRequestStart &lt; oCache.iCacheLower )
  209. {
  210. iRequestStart = iRequestStart - (iRequestLength*(iPipe-1));
  211. if ( iRequestStart &lt; 0 )
  212. {
  213. iRequestStart = 0;
  214. }
  215. }
  216. oCache.iCacheLower = iRequestStart;
  217. oCache.iCacheUpper = iRequestStart + (iRequestLength * iPipe);
  218. oCache.iDisplayLength = fnGetKey( aoData, "iDisplayLength" );
  219. fnSetKey( aoData, "iDisplayStart", iRequestStart );
  220. fnSetKey( aoData, "iDisplayLength", iRequestLength*iPipe );
  221. $.getJSON( sSource, aoData, function (json) {
  222. /* Callback processing */
  223. oCache.lastJson = jQuery.extend(true, {}, json);
  224. if ( oCache.iCacheLower != oCache.iDisplayStart )
  225. {
  226. json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
  227. }
  228. json.aaData.splice( oCache.iDisplayLength, json.aaData.length );
  229. fnCallback(json)
  230. } );
  231. }
  232. else
  233. {
  234. json = jQuery.extend(true, {}, oCache.lastJson);
  235. json.sEcho = sEcho; /* Update the echo for each response */
  236. json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
  237. json.aaData.splice( iRequestLength, json.aaData.length );
  238. fnCallback(json);
  239. return;
  240. }
  241. }
  242. $(document).ready(function() {
  243. $('#example').dataTable( {
  244. "bProcessing": true,
  245. "bServerSide": true,
  246. "sAjaxSource": "scripts/server_processing.php",
  247. "fnServerData": fnDataTablesPipeline
  248. } );
  249. } );</pre>
  250. <style type="text/css">
  251. @import "../examples_support/syntax/css/shCore.css";
  252. </style>
  253. <script type="text/javascript" language="javascript" src="../examples_support/syntax/js/shCore.js"></script>
  254. <h1>Server response</h1>
  255. <p>The code below shows the latest JSON data that has been returned from the server in response to the Ajax request made by DataTables. This will update as further requests are made.</p>
  256. <pre id="latest_xhr" class="brush: js;"></pre>
  257. <h1>Other examples</h1>
  258. <div class="demo_links">
  259. <h2>Basic initialisation</h2>
  260. <ul>
  261. <li><a href="../basic_init/zero_config.html">Zero configuration</a></li>
  262. <li><a href="../basic_init/filter_only.html">Feature enablement</a></li>
  263. <li><a href="../basic_init/table_sorting.html">Sorting data</a></li>
  264. <li><a href="../basic_init/multi_col_sort.html">Multi-column sorting</a></li>
  265. <li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
  266. <li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
  267. <li><a href="../basic_init/complex_header.html">Complex headers - grouping with colspan</a></li>
  268. <li><a href="../basic_init/dom.html">DOM positioning</a></li>
  269. <li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
  270. <li><a href="../basic_init/state_save.html">State saving</a></li>
  271. <li><a href="../basic_init/alt_pagination.html">Alternative pagination styles</a></li>
  272. <li>Scrolling: <br>
  273. <a href="../basic_init/scroll_x.html">Horizontal</a> /
  274. <a href="../basic_init/scroll_y.html">Vertical</a> /
  275. <a href="../basic_init/scroll_xy.html">Both</a> /
  276. <a href="../basic_init/scroll_y_theme.html">Themed</a> /
  277. <a href="../basic_init/scroll_y_infinite.html">Infinite</a>
  278. </li>
  279. <li><a href="../basic_init/language.html">Change language information (internationalisation)</a></li>
  280. <li><a href="../basic_init/themes.html">ThemeRoller themes (Smoothness)</a></li>
  281. </ul>
  282. <h2>Advanced initialisation</h2>
  283. <ul>
  284. <li>Events: <br>
  285. <a href="../advanced_init/events_live.html">Live events</a> /
  286. <a href="../advanced_init/events_pre_init.html">Pre-init</a> /
  287. <a href="../advanced_init/events_post_init.html">Post-init</a>
  288. </li>
  289. <li><a href="../advanced_init/column_render.html">Column rendering</a></li>
  290. <li><a href="../advanced_init/html_sort.html">Sorting without HTML tags</a></li>
  291. <li><a href="../advanced_init/dom_multiple_elements.html">Multiple table controls (sDom)</a></li>
  292. <li><a href="../advanced_init/length_menu.html">Defining length menu options</a></li>
  293. <li><a href="../advanced_init/complex_header.html">Complex headers and hidden columns</a></li>
  294. <li><a href="../advanced_init/dom_toolbar.html">Custom toolbar (element) around table</a></li>
  295. <li><a href="../advanced_init/highlight.html">Row highlighting with CSS</a></li>
  296. <li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
  297. <li><a href="../advanced_init/row_callback.html">Row callback</a></li>
  298. <li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
  299. <li><a href="../advanced_init/sorting_control.html">Control sorting direction of columns</a></li>
  300. <li><a href="../advanced_init/language_file.html">Change language information from a file (internationalisation)</a></li>
  301. <li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
  302. <li><a href="../advanced_init/localstorage.html">State saving with localStorage</a></li>
  303. <li><a href="../advanced_init/dt_events.html">Custom events</a></li>
  304. </ul>
  305. <h2>API</h2>
  306. <ul>
  307. <li><a href="../api/add_row.html">Dynamically add a new row</a></li>
  308. <li><a href="../api/multi_filter.html">Individual column filtering (using "input" elements)</a></li>
  309. <li><a href="../api/multi_filter_select.html">Individual column filtering (using "select" elements)</a></li>
  310. <li><a href="../api/highlight.html">Highlight rows and columns</a></li>
  311. <li><a href="../api/row_details.html">Show and hide details about a particular record</a></li>
  312. <li><a href="../api/select_row.html">User selectable rows (multiple rows)</a></li>
  313. <li><a href="../api/select_single_row.html">User selectable rows (single row) and delete rows</a></li>
  314. <li><a href="../api/editable.html">Editable rows (with jEditable)</a></li>
  315. <li><a href="../api/form.html">Submit form with elements in table</a></li>
  316. <li><a href="../api/counter_column.html">Index column (static number column)</a></li>
  317. <li><a href="../api/show_hide.html">Show and hide columns dynamically</a></li>
  318. <li><a href="../api/api_in_init.html">API function use in initialisation object (callback)</a></li>
  319. <li><a href="../api/tabs_and_scrolling.html">DataTables scrolling and tabs</a></li>
  320. <li><a href="../api/regex.html">Regular expression filtering</a></li>
  321. </ul>
  322. </div>
  323. <div class="demo_links">
  324. <h2>Data sources</h2>
  325. <ul>
  326. <li><a href="../data_sources/dom.html">DOM</a></li>
  327. <li><a href="../data_sources/js_array.html">Javascript array</a></li>
  328. <li><a href="../data_sources/ajax.html">Ajax source</a></li>
  329. <li><a href="../data_sources/server_side.html">Server side processing</a></li>
  330. </ul>
  331. <h2>Server-side processing</h2>
  332. <ul>
  333. <li><a href="../server_side/server_side.html">Obtain server-side data</a></li>
  334. <li><a href="../server_side/custom_vars.html">Add extra HTTP variables</a></li>
  335. <li><a href="../server_side/post.html">Use HTTP POST</a></li>
  336. <li><a href="../server_side/ids.html">Automatic addition of IDs and classes to rows</a></li>
  337. <li><a href="../server_side/object_data.html">Reading table data from objects</a></li>
  338. <li><a href="../server_side/row_details.html">Show and hide details about a particular record</a></li>
  339. <li><a href="../server_side/select_rows.html">User selectable rows (multiple rows)</a></li>
  340. <li><a href="../server_side/jsonp.html">JSONP for a cross domain data source</a></li>
  341. <li><a href="../server_side/editable.html">jEditable integration with DataTables</a></li>
  342. <li><a href="../server_side/defer_loading.html">Deferred loading of Ajax data</a></li>
  343. <li><a href="../server_side/pipeline.html">Pipelining data (reduce Ajax calls for paging)</a></li>
  344. </ul>
  345. <h2>Ajax data source</h2>
  346. <ul>
  347. <li><a href="../ajax/ajax.html">Ajax sourced data (array of arrays)</a></li>
  348. <li><a href="../ajax/objects.html">Ajax sourced data (array of objects)</a></li>
  349. <li><a href="../ajax/defer_render.html">Deferred DOM creation for extra speed</a></li>
  350. <li><a href="../ajax/null_data_source.html">Empty data source columns</a></li>
  351. <li><a href="../ajax/custom_data_property.html">Use a data source other than aaData (the default)</a></li>
  352. <li><a href="../ajax/objects_subarrays.html">Read column data from sub-arrays</a></li>
  353. <li><a href="../ajax/deep.html">Read column data from deeply nested properties</a></li>
  354. </ul>
  355. <h2>Plug-ins</h2>
  356. <ul>
  357. <li><a href="../plug-ins/plugin_api.html">Add custom API functions</a></li>
  358. <li><a href="../plug-ins/sorting_plugin.html">Sorting and automatic type detection</a></li>
  359. <li><a href="../plug-ins/sorting_sType.html">Sorting without automatic type detection</a></li>
  360. <li><a href="../plug-ins/paging_plugin.html">Custom pagination controls</a></li>
  361. <li><a href="../plug-ins/range_filtering.html">Range filtering / custom filtering</a></li>
  362. <li><a href="../plug-ins/dom_sort.html">Live DOM sorting</a></li>
  363. <li><a href="../plug-ins/html_sort.html">Automatic HTML type detection</a></li>
  364. </ul>
  365. </div>
  366. <div id="footer" class="clear" style="text-align:center;">
  367. <p>
  368. Please refer to the <a href="http://www.datatables.net/usage">DataTables documentation</a> for full information about its API properties and methods.<br>
  369. Additionally, there are a wide range of <a href="http://www.datatables.net/extras">extras</a> and <a href="http://www.datatables.net/plug-ins">plug-ins</a> which extend the capabilities of DataTables.
  370. </p>
  371. <span style="font-size:10px;">
  372. DataTables designed and created by <a href="http://www.sprymedia.co.uk">Allan Jardine</a> &copy; 2007-2011<br>
  373. DataTables is dual licensed under the <a href="http://www.datatables.net/license_gpl2">GPL v2 license</a> or a <a href="http://www.datatables.net/license_bsd">BSD (3-point) license</a>.
  374. </span>
  375. </div>
  376. </div>
  377. </body>
  378. </html>