comparison DataTables-1.9.4/examples/server_side/server_side.html @ 0:ac5f9272033b draft

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