PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/js/DataTables-1.9.1/examples/data_sources/server_side.html

https://bitbucket.org/veroreinah/bookworm
HTML | 394 lines | 350 code | 44 blank | 0 comment | 0 complexity | a2e198e03dd984e9442ec419f4feb940 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  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. $(document).ready(function() {
  15. $('#example').dataTable( {
  16. "bProcessing": true,
  17. "bServerSide": true,
  18. "sAjaxSource": "../server_side/scripts/server_processing.php",
  19. "sScrollY": "300",
  20. bPaginate: false,
  21. sScrollX: "100%",
  22. sScrollXInner: "120%"
  23. } );
  24. } );
  25. </script>
  26. </head>
  27. <body id="dt_example">
  28. <div id="container">
  29. <div class="full_width big">
  30. DataTables server-side processing example
  31. </div>
  32. <h1>Preamble</h1>
  33. <p>There are many ways to get your data into DataTables, and if you are working with seriously large databases, you might want to consider using the server-side options that DataTables provides. Basically all of the paging, filtering, sorting etc that DataTables does can be handed off to a server (or any other data source - Google Gears or Adobe Air for example!) and DataTables is just an events and display module.</p>
  34. <p>The example here shows a very simple display of the CSS data (used in all my other examples), but in this instance coming from the server on each draw. Filtering, multi-column sorting etc all work as you would expect.</p>
  35. <h1>Live example</h1>
  36. <div id="dynamic">
  37. <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
  38. <thead>
  39. <tr>
  40. <th width="20%">Rendering engine</th>
  41. <th width="25%">Browser</th>
  42. <th width="25%">Platform(s)</th>
  43. <th width="15%">Engine version</th>
  44. <th width="15%">CSS grade</th>
  45. </tr>
  46. </thead>
  47. <tbody>
  48. <tr>
  49. <td colspan="5" class="dataTables_empty">Loading data from server</td>
  50. </tr>
  51. </tbody>
  52. <tfoot>
  53. <tr>
  54. <th>Rendering engine</th>
  55. <th>Browser</th>
  56. <th>Platform(s)</th>
  57. <th>Engine version</th>
  58. <th>CSS grade</th>
  59. </tr>
  60. </tfoot>
  61. </table>
  62. </div>
  63. <div class="spacer"></div>
  64. <h1>Initialisation code</h1>
  65. <pre class="brush: js;">$(document).ready(function() {
  66. $('#example').dataTable( {
  67. "bProcessing": true,
  68. "bServerSide": true,
  69. "sAjaxSource": "../server_side/scripts/server_processing.php"
  70. } );
  71. } );</pre>
  72. <style type="text/css">
  73. @import "../examples_support/syntax/css/shCore.css";
  74. </style>
  75. <script type="text/javascript" language="javascript" src="../examples_support/syntax/js/shCore.js"></script>
  76. <h1>Server response</h1>
  77. <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>
  78. <pre id="latest_xhr" class="brush: js;"></pre>
  79. <h1>Server side (PHP) code</h1>
  80. <pre>&lt;?php
  81. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  82. * Easy set variables
  83. */
  84. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  85. * you want to insert a non-database field (for example a counter or static image)
  86. */
  87. $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
  88. /* Indexed column (used for fast and accurate table cardinality) */
  89. $sIndexColumn = "id";
  90. /* DB table to use */
  91. $sTable = "ajax";
  92. /* Database connection information */
  93. $gaSql['user'] = "";
  94. $gaSql['password'] = "";
  95. $gaSql['db'] = "";
  96. $gaSql['server'] = "localhost";
  97. /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
  98. include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
  99. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  100. * If you just want to use the basic configuration for DataTables with PHP server-side, there is
  101. * no need to edit below this line
  102. */
  103. /*
  104. * MySQL connection
  105. */
  106. $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
  107. die( 'Could not open connection to server' );
  108. mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
  109. die( 'Could not select database '. $gaSql['db'] );
  110. /*
  111. * Paging
  112. */
  113. $sLimit = "";
  114. if ( isset( $_GET['iDisplayStart'] ) &amp;&amp; $_GET['iDisplayLength'] != '-1' )
  115. {
  116. $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
  117. mysql_real_escape_string( $_GET['iDisplayLength'] );
  118. }
  119. /*
  120. * Ordering
  121. */
  122. $sOrder = "";
  123. if ( isset( $_GET['iSortCol_0'] ) )
  124. {
  125. $sOrder = "ORDER BY ";
  126. for ( $i=0 ; $i&lt;intval( $_GET['iSortingCols'] ) ; $i++ )
  127. {
  128. if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
  129. {
  130. $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
  131. mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
  132. }
  133. }
  134. $sOrder = substr_replace( $sOrder, "", -2 );
  135. if ( $sOrder == "ORDER BY" )
  136. {
  137. $sOrder = "";
  138. }
  139. }
  140. /*
  141. * Filtering
  142. * NOTE this does not match the built-in DataTables filtering which does it
  143. * word by word on any field. It's possible to do here, but concerned about efficiency
  144. * on very large tables, and MySQL's regex functionality is very limited
  145. */
  146. $sWhere = "";
  147. if ( isset($_GET['sSearch']) &amp;&amp; $_GET['sSearch'] != "" )
  148. {
  149. $sWhere = "WHERE (";
  150. for ( $i=0 ; $i&lt;count($aColumns) ; $i++ )
  151. {
  152. $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  153. }
  154. $sWhere = substr_replace( $sWhere, "", -3 );
  155. $sWhere .= ')';
  156. }
  157. /* Individual column filtering */
  158. for ( $i=0 ; $i&lt;count($aColumns) ; $i++ )
  159. {
  160. if ( isset($_GET['bSearchable_'.$i]) &amp;&amp; $_GET['bSearchable_'.$i] == "true" &amp;&amp; $_GET['sSearch_'.$i] != '' )
  161. {
  162. if ( $sWhere == "" )
  163. {
  164. $sWhere = "WHERE ";
  165. }
  166. else
  167. {
  168. $sWhere .= " AND ";
  169. }
  170. $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
  171. }
  172. }
  173. /*
  174. * SQL queries
  175. * Get data to display
  176. */
  177. $sQuery = "
  178. SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
  179. FROM $sTable
  180. $sWhere
  181. $sOrder
  182. $sLimit
  183. ";
  184. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  185. /* Data set length after filtering */
  186. $sQuery = "
  187. SELECT FOUND_ROWS()
  188. ";
  189. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  190. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  191. $iFilteredTotal = $aResultFilterTotal[0];
  192. /* Total data set length */
  193. $sQuery = "
  194. SELECT COUNT(`".$sIndexColumn."`)
  195. FROM $sTable
  196. ";
  197. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  198. $aResultTotal = mysql_fetch_array($rResultTotal);
  199. $iTotal = $aResultTotal[0];
  200. /*
  201. * Output
  202. */
  203. $output = array(
  204. "sEcho" =&gt; intval($_GET['sEcho']),
  205. "iTotalRecords" =&gt; $iTotal,
  206. "iTotalDisplayRecords" =&gt; $iFilteredTotal,
  207. "aaData" =&gt; array()
  208. );
  209. while ( $aRow = mysql_fetch_array( $rResult ) )
  210. {
  211. $row = array();
  212. for ( $i=0 ; $i&lt;count($aColumns) ; $i++ )
  213. {
  214. if ( $aColumns[$i] == "version" )
  215. {
  216. /* Special output formatting for 'version' column */
  217. $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  218. }
  219. else if ( $aColumns[$i] != ' ' )
  220. {
  221. /* General output */
  222. $row[] = $aRow[ $aColumns[$i] ];
  223. }
  224. }
  225. $output['aaData'][] = $row;
  226. }
  227. echo json_encode( $output );
  228. ?&gt;</pre>
  229. <h1>Other examples</h1>
  230. <div class="demo_links">
  231. <h2>Basic initialisation</h2>
  232. <ul>
  233. <li><a href="../basic_init/zero_config.html">Zero configuration</a></li>
  234. <li><a href="../basic_init/filter_only.html">Feature enablement</a></li>
  235. <li><a href="../basic_init/table_sorting.html">Sorting data</a></li>
  236. <li><a href="../basic_init/multi_col_sort.html">Multi-column sorting</a></li>
  237. <li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
  238. <li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
  239. <li><a href="../basic_init/complex_header.html">Complex headers - grouping with colspan</a></li>
  240. <li><a href="../basic_init/dom.html">DOM positioning</a></li>
  241. <li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
  242. <li><a href="../basic_init/state_save.html">State saving</a></li>
  243. <li><a href="../basic_init/alt_pagination.html">Alternative pagination styles</a></li>
  244. <li>Scrolling: <br>
  245. <a href="../basic_init/scroll_x.html">Horizontal</a> /
  246. <a href="../basic_init/scroll_y.html">Vertical</a> /
  247. <a href="../basic_init/scroll_xy.html">Both</a> /
  248. <a href="../basic_init/scroll_y_theme.html">Themed</a> /
  249. <a href="../basic_init/scroll_y_infinite.html">Infinite</a>
  250. </li>
  251. <li><a href="../basic_init/language.html">Change language information (internationalisation)</a></li>
  252. <li><a href="../basic_init/themes.html">ThemeRoller themes (Smoothness)</a></li>
  253. </ul>
  254. <h2>Advanced initialisation</h2>
  255. <ul>
  256. <li>Events: <br>
  257. <a href="../advanced_init/events_live.html">Live events</a> /
  258. <a href="../advanced_init/events_pre_init.html">Pre-init</a> /
  259. <a href="../advanced_init/events_post_init.html">Post-init</a>
  260. </li>
  261. <li><a href="../advanced_init/column_render.html">Column rendering</a></li>
  262. <li><a href="../advanced_init/html_sort.html">Sorting without HTML tags</a></li>
  263. <li><a href="../advanced_init/dom_multiple_elements.html">Multiple table controls (sDom)</a></li>
  264. <li><a href="../advanced_init/length_menu.html">Defining length menu options</a></li>
  265. <li><a href="../advanced_init/complex_header.html">Complex headers and hidden columns</a></li>
  266. <li><a href="../advanced_init/dom_toolbar.html">Custom toolbar (element) around table</a></li>
  267. <li><a href="../advanced_init/highlight.html">Row highlighting with CSS</a></li>
  268. <li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
  269. <li><a href="../advanced_init/row_callback.html">Row callback</a></li>
  270. <li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
  271. <li><a href="../advanced_init/sorting_control.html">Control sorting direction of columns</a></li>
  272. <li><a href="../advanced_init/language_file.html">Change language information from a file (internationalisation)</a></li>
  273. <li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
  274. <li><a href="../advanced_init/localstorage.html">State saving with localStorage</a></li>
  275. <li><a href="../advanced_init/dt_events.html">Custom events</a></li>
  276. </ul>
  277. <h2>API</h2>
  278. <ul>
  279. <li><a href="../api/add_row.html">Dynamically add a new row</a></li>
  280. <li><a href="../api/multi_filter.html">Individual column filtering (using "input" elements)</a></li>
  281. <li><a href="../api/multi_filter_select.html">Individual column filtering (using "select" elements)</a></li>
  282. <li><a href="../api/highlight.html">Highlight rows and columns</a></li>
  283. <li><a href="../api/row_details.html">Show and hide details about a particular record</a></li>
  284. <li><a href="../api/select_row.html">User selectable rows (multiple rows)</a></li>
  285. <li><a href="../api/select_single_row.html">User selectable rows (single row) and delete rows</a></li>
  286. <li><a href="../api/editable.html">Editable rows (with jEditable)</a></li>
  287. <li><a href="../api/form.html">Submit form with elements in table</a></li>
  288. <li><a href="../api/counter_column.html">Index column (static number column)</a></li>
  289. <li><a href="../api/show_hide.html">Show and hide columns dynamically</a></li>
  290. <li><a href="../api/api_in_init.html">API function use in initialisation object (callback)</a></li>
  291. <li><a href="../api/tabs_and_scrolling.html">DataTables scrolling and tabs</a></li>
  292. <li><a href="../api/regex.html">Regular expression filtering</a></li>
  293. </ul>
  294. </div>
  295. <div class="demo_links">
  296. <h2>Data sources</h2>
  297. <ul>
  298. <li><a href="../data_sources/dom.html">DOM</a></li>
  299. <li><a href="../data_sources/js_array.html">Javascript array</a></li>
  300. <li><a href="../data_sources/ajax.html">Ajax source</a></li>
  301. <li><a href="../data_sources/server_side.html">Server side processing</a></li>
  302. </ul>
  303. <h2>Server-side processing</h2>
  304. <ul>
  305. <li><a href="../server_side/server_side.html">Obtain server-side data</a></li>
  306. <li><a href="../server_side/custom_vars.html">Add extra HTTP variables</a></li>
  307. <li><a href="../server_side/post.html">Use HTTP POST</a></li>
  308. <li><a href="../server_side/ids.html">Automatic addition of IDs and classes to rows</a></li>
  309. <li><a href="../server_side/object_data.html">Reading table data from objects</a></li>
  310. <li><a href="../server_side/row_details.html">Show and hide details about a particular record</a></li>
  311. <li><a href="../server_side/select_rows.html">User selectable rows (multiple rows)</a></li>
  312. <li><a href="../server_side/jsonp.html">JSONP for a cross domain data source</a></li>
  313. <li><a href="../server_side/editable.html">jEditable integration with DataTables</a></li>
  314. <li><a href="../server_side/defer_loading.html">Deferred loading of Ajax data</a></li>
  315. <li><a href="../server_side/pipeline.html">Pipelining data (reduce Ajax calls for paging)</a></li>
  316. </ul>
  317. <h2>Ajax data source</h2>
  318. <ul>
  319. <li><a href="../ajax/ajax.html">Ajax sourced data (array of arrays)</a></li>
  320. <li><a href="../ajax/objects.html">Ajax sourced data (array of objects)</a></li>
  321. <li><a href="../ajax/defer_render.html">Deferred DOM creation for extra speed</a></li>
  322. <li><a href="../ajax/null_data_source.html">Empty data source columns</a></li>
  323. <li><a href="../ajax/custom_data_property.html">Use a data source other than aaData (the default)</a></li>
  324. <li><a href="../ajax/objects_subarrays.html">Read column data from sub-arrays</a></li>
  325. <li><a href="../ajax/deep.html">Read column data from deeply nested properties</a></li>
  326. </ul>
  327. <h2>Plug-ins</h2>
  328. <ul>
  329. <li><a href="../plug-ins/plugin_api.html">Add custom API functions</a></li>
  330. <li><a href="../plug-ins/sorting_plugin.html">Sorting and automatic type detection</a></li>
  331. <li><a href="../plug-ins/sorting_sType.html">Sorting without automatic type detection</a></li>
  332. <li><a href="../plug-ins/paging_plugin.html">Custom pagination controls</a></li>
  333. <li><a href="../plug-ins/range_filtering.html">Range filtering / custom filtering</a></li>
  334. <li><a href="../plug-ins/dom_sort.html">Live DOM sorting</a></li>
  335. <li><a href="../plug-ins/html_sort.html">Automatic HTML type detection</a></li>
  336. </ul>
  337. </div>
  338. <div id="footer" class="clear" style="text-align:center;">
  339. <p>
  340. Please refer to the <a href="http://www.datatables.net/usage">DataTables documentation</a> for full information about its API properties and methods.<br>
  341. 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.
  342. </p>
  343. <span style="font-size:10px;">
  344. DataTables designed and created by <a href="http://www.sprymedia.co.uk">Allan Jardine</a> &copy; 2007-2011<br>
  345. 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>.
  346. </span>
  347. </div>
  348. </div>
  349. </body>
  350. </html>