Project

General

Profile

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 - Row selection</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="../../media/js/jquery.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
$(document).ready(function() {
23
	var selected = [];
24

    
25
	$("#example").dataTable({
26
		"processing": true,
27
		"serverSide": true,
28
		"ajax": "scripts/ids-arrays.php",
29
		"rowCallback": function( row, data, displayIndex ) {
30
			if ( $.inArray(data.DT_RowId, selected) !== -1 ) {
31
				$(row).addClass('selected');
32
			}
33
		}
34
	});
35

    
36
	$('#example tbody').on('click', 'tr', function () {
37
		var id = this.id;
38
		var index = $.inArray(id, selected);
39

    
40
		if ( index === -1 ) {
41
			selected.push( id );
42
		} else {
43
			selected.splice( index, 1 );
44
		}
45

    
46
		$(this).toggleClass('selected');
47
	} );
48
} );
49

    
50

    
51
	</script>
52
</head>
53

    
54
<body class="dt-example">
55
	<div class="container">
56
		<section>
57
			<h1>DataTables example <span>Row selection</span></h1>
58

    
59
			<div class="info">
60
				<p>When you want to have user selectable rows in DataTables, it is relatively trivial when using DOM
61
				based data - but when using server-side processing, DataTables doesn't retain DOM row elements over
62
				pages / filtering etc. As such, you will need to keep a track of which rows a user as selected and mark
63
				them as selected on each draw.</p>
64

    
65
				<p>This is shown in this demo, which uses a unique ID assigned to the TR element (this is done
66
				automatically through the use of the <code>DT_RowId</code> special property returned as part of the
67
				object given by the server for each row) to track which rows are selected and reselect them is
68
				appropriate on a draw.</p>
69
			</div>
70

    
71
			<table id="example" class="display" cellspacing="0" width="100%">
72
				<thead>
73
					<tr>
74
						<th>Name</th>
75
						<th>Position</th>
76
						<th>Office</th>
77
						<th>Extn.</th>
78
						<th>Start date</th>
79
						<th>Salary</th>
80
					</tr>
81
				</thead>
82

    
83
				<tfoot>
84
					<tr>
85
						<th>Name</th>
86
						<th>Position</th>
87
						<th>Office</th>
88
						<th>Extn.</th>
89
						<th>Start date</th>
90
						<th>Salary</th>
91
					</tr>
92
				</tfoot>
93
			</table>
94

    
95
			<ul class="tabs">
96
				<li class="active">Javascript</li>
97
				<li>HTML</li>
98
				<li>CSS</li>
99
				<li>Ajax</li>
100
				<li>Server-side script</li>
101
			</ul>
102

    
103
			<div class="tabs">
104
				<div class="js">
105
					<p>The Javascript shown below is used to initialise the table shown in this
106
					example:</p><code class="multiline brush: js;">$(document).ready(function() {
107
	var selected = [];
108

    
109
	$(&quot;#example&quot;).dataTable({
110
		&quot;processing&quot;: true,
111
		&quot;serverSide&quot;: true,
112
		&quot;ajax&quot;: &quot;scripts/ids-arrays.php&quot;,
113
		&quot;rowCallback&quot;: function( row, data, displayIndex ) {
114
			if ( $.inArray(data.DT_RowId, selected) !== -1 ) {
115
				$(row).addClass('selected');
116
			}
117
		}
118
	});
119

    
120
	$('#example tbody').on('click', 'tr', function () {
121
		var id = this.id;
122
		var index = $.inArray(id, selected);
123

    
124
		if ( index === -1 ) {
125
			selected.push( id );
126
		} else {
127
			selected.splice( index, 1 );
128
		}
129

    
130
		$(this).toggleClass('selected');
131
	} );
132
} );</code>
133

    
134
					<p>In addition to the above code, the following Javascript library files are loaded for use in this
135
					example:</p>
136

    
137
					<ul>
138
						<li><a href="../../media/js/jquery.js">../../media/js/jquery.js</a></li>
139
						<li><a href="../../media/js/jquery.dataTables.js">../../media/js/jquery.dataTables.js</a></li>
140
					</ul>
141
				</div>
142

    
143
				<div class="table">
144
					<p>The HTML shown below is the raw HTML table element, before it has been enhanced by
145
					DataTables:</p>
146
				</div>
147

    
148
				<div class="css">
149
					<div>
150
						<p>This example uses a little bit of additional CSS beyond what is loaded from the library
151
						files (below), in order to correctly display the table. The additional CSS used is shown
152
						below:</p><code class="multiline brush: js;"></code>
153
					</div>
154

    
155
					<p>The following CSS library files are loaded for use in this example to provide the styling of the
156
					table:</p>
157

    
158
					<ul>
159
						<li><a href=
160
						"../../media/css/jquery.dataTables.css">../../media/css/jquery.dataTables.css</a></li>
161
					</ul>
162
				</div>
163

    
164
				<div class="ajax">
165
					<p>This table loads data by Ajax. The latest data that has been loaded is shown below. This data
166
					will update automatically as any additional data is loaded.</p>
167
				</div>
168

    
169
				<div class="php">
170
					<p>The script used to perform the server-side processing for this table is shown below. Please note
171
					that this is just an example script using PHP. Server-side processing scripts can be written in any
172
					language, using <a href="//datatables.net/manual/server-side">the protocol described in the
173
					DataTables documentation</a>.</p>
174
				</div>
175
			</div>
176
		</section>
177
	</div>
178

    
179
	<section>
180
		<div class="footer">
181
			<div class="gradient"></div>
182

    
183
			<div class="liner">
184
				<h2>Other examples</h2>
185

    
186
				<div class="toc">
187
					<div class="toc-group">
188
						<h3><a href="../basic_init/index.html">Basic initialisation</a></h3>
189
						<ul class="toc">
190
							<li><a href="../basic_init/zero_configuration.html">Zero configuration</a></li>
191
							<li><a href="../basic_init/filter_only.html">Feature enable / disable</a></li>
192
							<li><a href="../basic_init/table_sorting.html">Default ordering (sorting)</a></li>
193
							<li><a href="../basic_init/multi_col_sort.html">Multi-column ordering</a></li>
194
							<li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
195
							<li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
196
							<li><a href="../basic_init/complex_header.html">Complex headers (rowspan and
197
							colspan)</a></li>
198
							<li><a href="../basic_init/dom.html">DOM positioning</a></li>
199
							<li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
200
							<li><a href="../basic_init/state_save.html">State saving</a></li>
201
							<li><a href="../basic_init/alt_pagination.html">Alternative pagination</a></li>
202
							<li><a href="../basic_init/scroll_y.html">Scroll - vertical</a></li>
203
							<li><a href="../basic_init/scroll_x.html">Scroll - horizontal</a></li>
204
							<li><a href="../basic_init/scroll_xy.html">Scroll - horizontal and vertical</a></li>
205
							<li><a href="../basic_init/scroll_y_theme.html">Scroll - vertical with jQuery UI
206
							ThemeRoller</a></li>
207
							<li><a href="../basic_init/comma-decimal.html">Language - Comma decimal place</a></li>
208
							<li><a href="../basic_init/language.html">Language options</a></li>
209
						</ul>
210
					</div>
211

    
212
					<div class="toc-group">
213
						<h3><a href="../advanced_init/index.html">Advanced initialisation</a></h3>
214
						<ul class="toc">
215
							<li><a href="../advanced_init/events_live.html">DOM / jQuery events</a></li>
216
							<li><a href="../advanced_init/dt_events.html">DataTables events</a></li>
217
							<li><a href="../advanced_init/column_render.html">Column rendering</a></li>
218
							<li><a href="../advanced_init/length_menu.html">Page length options</a></li>
219
							<li><a href="../advanced_init/dom_multiple_elements.html">Multiple table control
220
							elements</a></li>
221
							<li><a href="../advanced_init/complex_header.html">Complex headers (rowspan /
222
							colspan)</a></li>
223
							<li><a href="../advanced_init/html5-data-attributes.html">HTML5 data-* attributes</a></li>
224
							<li><a href="../advanced_init/language_file.html">Language file</a></li>
225
							<li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
226
							<li><a href="../advanced_init/row_callback.html">Row created callback</a></li>
227
							<li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
228
							<li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
229
							<li><a href="../advanced_init/dom_toolbar.html">Custom toolbar elements</a></li>
230
							<li><a href="../advanced_init/sort_direction_control.html">Order direction sequence
231
							control</a></li>
232
						</ul>
233
					</div>
234

    
235
					<div class="toc-group">
236
						<h3><a href="../styling/index.html">Styling</a></h3>
237
						<ul class="toc">
238
							<li><a href="../styling/display.html">Base style</a></li>
239
							<li><a href="../styling/no-classes.html">Base style - no styling classes</a></li>
240
							<li><a href="../styling/row-border.html">Base style - row borders</a></li>
241
							<li><a href="../styling/cell-border.html">Base style - cell borders</a></li>
242
							<li><a href="../styling/hover.html">Base style - hover</a></li>
243
							<li><a href="../styling/order-column.html">Base style - order-column</a></li>
244
							<li><a href="../styling/stripe.html">Base style - stripe</a></li>
245
							<li><a href="../styling/jqueryUI.html">jQuery UI ThemeRoller</a></li>
246
							<li><a href="../styling/bootstrap.html">Bootstrap</a></li>
247
							<li><a href="../styling/foundation.html">Foundation</a></li>
248
						</ul>
249
					</div>
250

    
251
					<div class="toc-group">
252
						<h3><a href="../data_sources/index.html">Data sources</a></h3>
253
						<ul class="toc">
254
							<li><a href="../data_sources/dom.html">HTML (DOM) sourced data</a></li>
255
							<li><a href="../data_sources/ajax.html">Ajax sourced data</a></li>
256
							<li><a href="../data_sources/js_array.html">Javascript sourced data</a></li>
257
							<li><a href="../data_sources/server_side.html">Server-side processing</a></li>
258
						</ul>
259
					</div>
260

    
261
					<div class="toc-group">
262
						<h3><a href="../api/index.html">API</a></h3>
263
						<ul class="toc">
264
							<li><a href="../api/add_row.html">Add rows</a></li>
265
							<li><a href="../api/multi_filter.html">Individual column filtering (text inputs)</a></li>
266
							<li><a href="../api/multi_filter_select.html">Individual column filtering (select
267
							inputs)</a></li>
268
							<li><a href="../api/highlight.html">Highlighting rows and columns</a></li>
269
							<li><a href="../api/row_details.html">Child rows (show extra / detailed
270
							information)</a></li>
271
							<li><a href="../api/select_row.html">Row selection (multiple rows)</a></li>
272
							<li><a href="../api/select_single_row.html">Row selection and deletion (single
273
							row)</a></li>
274
							<li><a href="../api/form.html">Form inputs</a></li>
275
							<li><a href="../api/counter_columns.html">Index column</a></li>
276
							<li><a href="../api/show_hide.html">Show / hide columns dynamically</a></li>
277
							<li><a href="../api/api_in_init.html">Using API in callbacks</a></li>
278
							<li><a href="../api/tabs_and_scrolling.html">Scrolling and jQuery UI tabs</a></li>
279
							<li><a href="../api/regex.html">Filtering API (regular expressions)</a></li>
280
						</ul>
281
					</div>
282

    
283
					<div class="toc-group">
284
						<h3><a href="../ajax/index.html">Ajax</a></h3>
285
						<ul class="toc">
286
							<li><a href="../ajax/simple.html">Ajax data source (arrays)</a></li>
287
							<li><a href="../ajax/objects.html">Ajax data source (objects)</a></li>
288
							<li><a href="../ajax/deep.html">Nested object data (objects)</a></li>
289
							<li><a href="../ajax/objects_subarrays.html">Nested object data (arrays)</a></li>
290
							<li><a href="../ajax/orthogonal-data.html">Orthogonal data</a></li>
291
							<li><a href="../ajax/null_data_source.html">Generated content for a column</a></li>
292
							<li><a href="../ajax/custom_data_property.html">Custom data source property</a></li>
293
							<li><a href="../ajax/custom_data_flat.html">Flat array data source</a></li>
294
							<li><a href="../ajax/defer_render.html">Deferred rendering for speed</a></li>
295
						</ul>
296
					</div>
297

    
298
					<div class="toc-group">
299
						<h3><a href="./index.html">Server-side</a></h3>
300
						<ul class="toc active">
301
							<li><a href="./simple.html">Server-side processing</a></li>
302
							<li><a href="./custom_vars.html">Custom HTTP variables</a></li>
303
							<li><a href="./post.html">POST data</a></li>
304
							<li><a href="./ids.html">Automatic addition of row ID attributes</a></li>
305
							<li><a href="./object_data.html">Object data source</a></li>
306
							<li><a href="./row_details.html">Row details</a></li>
307
							<li class="active"><a href="./select_rows.html">Row selection</a></li>
308
							<li><a href="./jsonp.html">JSONP data source for remote domains</a></li>
309
							<li><a href="./defer_loading.html">Deferred loading of data</a></li>
310
							<li><a href="./pipeline.html">Pipelining data to reduce Ajax calls for paging</a></li>
311
						</ul>
312
					</div>
313

    
314
					<div class="toc-group">
315
						<h3><a href="../plug-ins/index.html">Plug-ins</a></h3>
316
						<ul class="toc">
317
							<li><a href="../plug-ins/api.html">API plug-in methods</a></li>
318
							<li><a href="../plug-ins/sorting_auto.html">Ordering plug-ins (with type
319
							detection)</a></li>
320
							<li><a href="../plug-ins/sorting_manual.html">Ordering plug-ins (no type
321
							detection)</a></li>
322
							<li><a href="../plug-ins/range_filtering.html">Custom filtering - range search</a></li>
323
							<li><a href="../plug-ins/dom_sort.html">Live DOM ordering</a></li>
324
						</ul>
325
					</div>
326
				</div>
327

    
328
				<div class="epilogue">
329
					<p>Please refer to the <a href="http://www.datatables.net">DataTables documentation</a> for full
330
					information about its API properties and methods.<br>
331
					Additionally, there are a wide range of <a href="http://www.datatables.net/extras">extras</a> and
332
					<a href="http://www.datatables.net/plug-ins">plug-ins</a> which extend the capabilities of
333
					DataTables.</p>
334

    
335
					<p class="copyright">DataTables designed and created by <a href=
336
					"http://www.sprymedia.co.uk">SpryMedia Ltd</a> &#169; 2007-2014<br>
337
					DataTables is licensed under the <a href="http://www.datatables.net/mit">MIT license</a>.</p>
338
				</div>
339
			</div>
340
		</div>
341
	</section>
342
</body>
343
</html>
(10-10/11)