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 - Ajax data source (objects)</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
$(document).ready(function() {
22
	$('#example').dataTable( {
23
		"ajax": "data/objects.txt",
24
		"columns": [
25
			{ "data": "name" },
26
			{ "data": "position" },
27
			{ "data": "office" },
28
			{ "data": "extn" },
29
			{ "data": "start_date" },
30
			{ "data": "salary" }
31
		]
32
	} );
33
} );
34

    
35
	</script>
36
</head>
37

    
38
<body class="dt-example">
39
	<div class="container">
40
		<section>
41
			<h1>DataTables example <span>Ajax data source (objects)</span></h1>
42

    
43
			<div class="info">
44
				<p>To try and make life easy, by default, DataTables expects arrays to be used as the data source for
45
				rows in the table. However, this isn't always useful, and you may wish to have DataTables use objects
46
				as the data source for each row (i.e. each row has its data described by an object) as this can make
47
				working with the data much more understandable, particularly if you are using the API and you don't
48
				need to keep track of array indexes.</p>
49

    
50
				<p>This can be done quite simply by using the <a href=
51
				"//datatables.net/reference/option/columns.data"><code class="option" title=
52
				"DataTables initialisation option">columns.data<span>DT</span></code></a> option which you use to tell
53
				DataTables which property to use from the data source object for each column.</p>
54

    
55
				<p>In this example the Ajax source returns an array of objects, which DataTables uses to display the
56
				table. The structure of the row's data source in this example is:</p>
57
				<pre>
58
<code class="multiline">{
59
	"name":	   "Tiger Nixon",
60
	"position":   "System Architect",
61
	"salary":	 "$3,120",
62
	"start_date": "2011/04/25",
63
	"office":	 "Edinburgh",
64
	"extn":	   "5421"
65
}
66
</code>
67
</pre>
68
			</div>
69

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

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

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

    
102
			<div class="tabs">
103
				<div class="js">
104
					<p>The Javascript shown below is used to initialise the table shown in this
105
					example:</p><code class="multiline brush: js;">$(document).ready(function() {
106
	$('#example').dataTable( {
107
		&quot;ajax&quot;: &quot;data/objects.txt&quot;,
108
		&quot;columns&quot;: [
109
			{ &quot;data&quot;: &quot;name&quot; },
110
			{ &quot;data&quot;: &quot;position&quot; },
111
			{ &quot;data&quot;: &quot;office&quot; },
112
			{ &quot;data&quot;: &quot;extn&quot; },
113
			{ &quot;data&quot;: &quot;start_date&quot; },
114
			{ &quot;data&quot;: &quot;salary&quot; }
115
		]
116
	} );
117
} );</code>
118

    
119
					<p>In addition to the above code, the following Javascript library files are loaded for use in this
120
					example:</p>
121

    
122
					<ul>
123
						<li><a href="../../media/js/jquery.js">../../media/js/jquery.js</a></li>
124
						<li><a href="../../media/js/jquery.dataTables.js">../../media/js/jquery.dataTables.js</a></li>
125
					</ul>
126
				</div>
127

    
128
				<div class="table">
129
					<p>The HTML shown below is the raw HTML table element, before it has been enhanced by
130
					DataTables:</p>
131
				</div>
132

    
133
				<div class="css">
134
					<div>
135
						<p>This example uses a little bit of additional CSS beyond what is loaded from the library
136
						files (below), in order to correctly display the table. The additional CSS used is shown
137
						below:</p><code class="multiline brush: js;"></code>
138
					</div>
139

    
140
					<p>The following CSS library files are loaded for use in this example to provide the styling of the
141
					table:</p>
142

    
143
					<ul>
144
						<li><a href=
145
						"../../media/css/jquery.dataTables.css">../../media/css/jquery.dataTables.css</a></li>
146
					</ul>
147
				</div>
148

    
149
				<div class="ajax">
150
					<p>This table loads data by Ajax. The latest data that has been loaded is shown below. This data
151
					will update automatically as any additional data is loaded.</p>
152
				</div>
153

    
154
				<div class="php">
155
					<p>The script used to perform the server-side processing for this table is shown below. Please note
156
					that this is just an example script using PHP. Server-side processing scripts can be written in any
157
					language, using <a href="//datatables.net/manual/server-side">the protocol described in the
158
					DataTables documentation</a>.</p>
159
				</div>
160
			</div>
161
		</section>
162
	</div>
163

    
164
	<section>
165
		<div class="footer">
166
			<div class="gradient"></div>
167

    
168
			<div class="liner">
169
				<h2>Other examples</h2>
170

    
171
				<div class="toc">
172
					<div class="toc-group">
173
						<h3><a href="../basic_init/index.html">Basic initialisation</a></h3>
174
						<ul class="toc">
175
							<li><a href="../basic_init/zero_configuration.html">Zero configuration</a></li>
176
							<li><a href="../basic_init/filter_only.html">Feature enable / disable</a></li>
177
							<li><a href="../basic_init/table_sorting.html">Default ordering (sorting)</a></li>
178
							<li><a href="../basic_init/multi_col_sort.html">Multi-column ordering</a></li>
179
							<li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
180
							<li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
181
							<li><a href="../basic_init/complex_header.html">Complex headers (rowspan and
182
							colspan)</a></li>
183
							<li><a href="../basic_init/dom.html">DOM positioning</a></li>
184
							<li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
185
							<li><a href="../basic_init/state_save.html">State saving</a></li>
186
							<li><a href="../basic_init/alt_pagination.html">Alternative pagination</a></li>
187
							<li><a href="../basic_init/scroll_y.html">Scroll - vertical</a></li>
188
							<li><a href="../basic_init/scroll_x.html">Scroll - horizontal</a></li>
189
							<li><a href="../basic_init/scroll_xy.html">Scroll - horizontal and vertical</a></li>
190
							<li><a href="../basic_init/scroll_y_theme.html">Scroll - vertical with jQuery UI
191
							ThemeRoller</a></li>
192
							<li><a href="../basic_init/comma-decimal.html">Language - Comma decimal place</a></li>
193
							<li><a href="../basic_init/language.html">Language options</a></li>
194
						</ul>
195
					</div>
196

    
197
					<div class="toc-group">
198
						<h3><a href="../advanced_init/index.html">Advanced initialisation</a></h3>
199
						<ul class="toc">
200
							<li><a href="../advanced_init/events_live.html">DOM / jQuery events</a></li>
201
							<li><a href="../advanced_init/dt_events.html">DataTables events</a></li>
202
							<li><a href="../advanced_init/column_render.html">Column rendering</a></li>
203
							<li><a href="../advanced_init/length_menu.html">Page length options</a></li>
204
							<li><a href="../advanced_init/dom_multiple_elements.html">Multiple table control
205
							elements</a></li>
206
							<li><a href="../advanced_init/complex_header.html">Complex headers (rowspan /
207
							colspan)</a></li>
208
							<li><a href="../advanced_init/html5-data-attributes.html">HTML5 data-* attributes</a></li>
209
							<li><a href="../advanced_init/language_file.html">Language file</a></li>
210
							<li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
211
							<li><a href="../advanced_init/row_callback.html">Row created callback</a></li>
212
							<li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
213
							<li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
214
							<li><a href="../advanced_init/dom_toolbar.html">Custom toolbar elements</a></li>
215
							<li><a href="../advanced_init/sort_direction_control.html">Order direction sequence
216
							control</a></li>
217
						</ul>
218
					</div>
219

    
220
					<div class="toc-group">
221
						<h3><a href="../styling/index.html">Styling</a></h3>
222
						<ul class="toc">
223
							<li><a href="../styling/display.html">Base style</a></li>
224
							<li><a href="../styling/no-classes.html">Base style - no styling classes</a></li>
225
							<li><a href="../styling/row-border.html">Base style - row borders</a></li>
226
							<li><a href="../styling/cell-border.html">Base style - cell borders</a></li>
227
							<li><a href="../styling/hover.html">Base style - hover</a></li>
228
							<li><a href="../styling/order-column.html">Base style - order-column</a></li>
229
							<li><a href="../styling/stripe.html">Base style - stripe</a></li>
230
							<li><a href="../styling/jqueryUI.html">jQuery UI ThemeRoller</a></li>
231
							<li><a href="../styling/bootstrap.html">Bootstrap</a></li>
232
							<li><a href="../styling/foundation.html">Foundation</a></li>
233
						</ul>
234
					</div>
235

    
236
					<div class="toc-group">
237
						<h3><a href="../data_sources/index.html">Data sources</a></h3>
238
						<ul class="toc">
239
							<li><a href="../data_sources/dom.html">HTML (DOM) sourced data</a></li>
240
							<li><a href="../data_sources/ajax.html">Ajax sourced data</a></li>
241
							<li><a href="../data_sources/js_array.html">Javascript sourced data</a></li>
242
							<li><a href="../data_sources/server_side.html">Server-side processing</a></li>
243
						</ul>
244
					</div>
245

    
246
					<div class="toc-group">
247
						<h3><a href="../api/index.html">API</a></h3>
248
						<ul class="toc">
249
							<li><a href="../api/add_row.html">Add rows</a></li>
250
							<li><a href="../api/multi_filter.html">Individual column filtering (text inputs)</a></li>
251
							<li><a href="../api/multi_filter_select.html">Individual column filtering (select
252
							inputs)</a></li>
253
							<li><a href="../api/highlight.html">Highlighting rows and columns</a></li>
254
							<li><a href="../api/row_details.html">Child rows (show extra / detailed
255
							information)</a></li>
256
							<li><a href="../api/select_row.html">Row selection (multiple rows)</a></li>
257
							<li><a href="../api/select_single_row.html">Row selection and deletion (single
258
							row)</a></li>
259
							<li><a href="../api/form.html">Form inputs</a></li>
260
							<li><a href="../api/counter_columns.html">Index column</a></li>
261
							<li><a href="../api/show_hide.html">Show / hide columns dynamically</a></li>
262
							<li><a href="../api/api_in_init.html">Using API in callbacks</a></li>
263
							<li><a href="../api/tabs_and_scrolling.html">Scrolling and jQuery UI tabs</a></li>
264
							<li><a href="../api/regex.html">Filtering API (regular expressions)</a></li>
265
						</ul>
266
					</div>
267

    
268
					<div class="toc-group">
269
						<h3><a href="./index.html">Ajax</a></h3>
270
						<ul class="toc active">
271
							<li><a href="./simple.html">Ajax data source (arrays)</a></li>
272
							<li class="active"><a href="./objects.html">Ajax data source (objects)</a></li>
273
							<li><a href="./deep.html">Nested object data (objects)</a></li>
274
							<li><a href="./objects_subarrays.html">Nested object data (arrays)</a></li>
275
							<li><a href="./orthogonal-data.html">Orthogonal data</a></li>
276
							<li><a href="./null_data_source.html">Generated content for a column</a></li>
277
							<li><a href="./custom_data_property.html">Custom data source property</a></li>
278
							<li><a href="./custom_data_flat.html">Flat array data source</a></li>
279
							<li><a href="./defer_render.html">Deferred rendering for speed</a></li>
280
						</ul>
281
					</div>
282

    
283
					<div class="toc-group">
284
						<h3><a href="../server_side/index.html">Server-side</a></h3>
285
						<ul class="toc">
286
							<li><a href="../server_side/simple.html">Server-side processing</a></li>
287
							<li><a href="../server_side/custom_vars.html">Custom HTTP variables</a></li>
288
							<li><a href="../server_side/post.html">POST data</a></li>
289
							<li><a href="../server_side/ids.html">Automatic addition of row ID attributes</a></li>
290
							<li><a href="../server_side/object_data.html">Object data source</a></li>
291
							<li><a href="../server_side/row_details.html">Row details</a></li>
292
							<li><a href="../server_side/select_rows.html">Row selection</a></li>
293
							<li><a href="../server_side/jsonp.html">JSONP data source for remote domains</a></li>
294
							<li><a href="../server_side/defer_loading.html">Deferred loading of data</a></li>
295
							<li><a href="../server_side/pipeline.html">Pipelining data to reduce Ajax calls for
296
							paging</a></li>
297
						</ul>
298
					</div>
299

    
300
					<div class="toc-group">
301
						<h3><a href="../plug-ins/index.html">Plug-ins</a></h3>
302
						<ul class="toc">
303
							<li><a href="../plug-ins/api.html">API plug-in methods</a></li>
304
							<li><a href="../plug-ins/sorting_auto.html">Ordering plug-ins (with type
305
							detection)</a></li>
306
							<li><a href="../plug-ins/sorting_manual.html">Ordering plug-ins (no type
307
							detection)</a></li>
308
							<li><a href="../plug-ins/range_filtering.html">Custom filtering - range search</a></li>
309
							<li><a href="../plug-ins/dom_sort.html">Live DOM ordering</a></li>
310
						</ul>
311
					</div>
312
				</div>
313

    
314
				<div class="epilogue">
315
					<p>Please refer to the <a href="http://www.datatables.net">DataTables documentation</a> for full
316
					information about its API properties and methods.<br>
317
					Additionally, there are a wide range of <a href="http://www.datatables.net/extras">extras</a> and
318
					<a href="http://www.datatables.net/plug-ins">plug-ins</a> which extend the capabilities of
319
					DataTables.</p>
320

    
321
					<p class="copyright">DataTables designed and created by <a href=
322
					"http://www.sprymedia.co.uk">SpryMedia Ltd</a> &#169; 2007-2014<br>
323
					DataTables is licensed under the <a href="http://www.datatables.net/mit">MIT license</a>.</p>
324
				</div>
325
			</div>
326
		</div>
327
	</section>
328
</body>
329
</html>
(7-7/10)