-
Notifications
You must be signed in to change notification settings - Fork 11
/
demo.html
487 lines (438 loc) · 20.4 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Knockout DataTable Demo</title>
<link rel='stylesheet' type='text/css' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'>
<link rel='stylesheet' type='text/css' href='knockout-datatable.css'>
<script src='https://knockoutjs.com/downloads/knockout-3.2.0.js'></script>
<script src='knockout-datatable.min.js'></script>
<style type='text/css'>
/* yanked from bootstrap demos: https://getbootstrap.com/examples/jumbotron-narrow/ */
/* Space out content a bit */
body {
padding-top: 20px;
padding-bottom: 20px;
}
/* Everything but the jumbotron gets side spacing for mobile first views */
.header,
.marketing,
.footer {
padding-left: 15px;
padding-right: 15px;
}
/* Custom page header */
.header {
border-bottom: 1px solid #e5e5e5;
}
/* Make the masthead heading the same height as the navigation */
.header h3 {
margin-top: 0;
margin-bottom: 0;
line-height: 40px;
padding-bottom: 19px;
}
/* Custom page footer */
.footer {
padding-top: 19px;
color: #777;
border-top: 1px solid #e5e5e5;
}
/* Customize container */
@media (min-width: 768px) {
.container {
max-width: 730px;
}
}
.container-narrow > hr {
margin: 30px 0;
}
/* Main marketing message and sign up button */
.jumbotron {
text-align: center;
border-bottom: 1px solid #e5e5e5;
}
.jumbotron .btn {
font-size: 21px;
padding: 14px 24px;
}
/* Supporting marketing content */
.marketing {
margin: 40px 0;
}
.marketing p + h4 {
margin-top: 28px;
}
/* Responsive: Portrait tablets and up */
@media screen and (min-width: 768px) {
/* Remove the padding we set earlier */
.header,
.marketing,
.footer {
padding-left: 0;
padding-right: 0;
}
/* Space out the masthead */
.header {
margin-bottom: 30px;
}
/* Remove the bottom border on the jumbotron for visual effect */
.jumbotron {
border-bottom: 0;
}
}
dd {
margin-bottom: 1em;
}
pre {
overflow: auto;
word-wrap: normal;
white-space: pre;
}
</style>
<script type='text/javascript'>
var citiesList=[{city_name:"Tokyo",country_name:"Japan",population:31714e3},{city_name:"Seoul",country_name:"South Korea",population:25721e3},{city_name:"Jakarta",country_name:"Indonesia",population:23308500},{city_name:"Delhi",country_name:"India",population:21753486},{city_name:"Mumbai",country_name:"India",population:20748395},{city_name:"Mexico City",country_name:"Mexico",population:20116842},{city_name:"São Paulo",country_name:"Brazil",population:19956590},{city_name:"New York",country_name:"United States",population:19831858},{city_name:"Moscow",country_name:"Russia",population:17e6},{city_name:"Osaka-Kobe-Kyoto (Keihanshin)",country_name:"Japan",population:16663e3},{city_name:"Kolkata",country_name:"India",population:14617882},{city_name:"London",country_name:"United Kingdom",population:13614409},{city_name:"Los Angeles",country_name:"United States",population:13052921},{city_name:"Buenos Aires",country_name:"Argentina",population:12801364},{city_name:"Dhaka",country_name:"Bangladesh",population:12797394},{city_name:"Paris",country_name:"France",population:12161542},{city_name:"Manila",country_name:"Philippines",population:11855975},{city_name:"Rio de Janeiro",country_name:"Brazil",population:11846530},{city_name:"Chicago",country_name:"United States",population:9522434},{city_name:"Nagoya",country_name:"Japan",population:9046e3},{city_name:"Chennai",country_name:"India",population:8917749},{city_name:"Bangalore",country_name:"India",population:8728906},{city_name:"Lima",country_name:"Peru",population:8482619},{city_name:"Bogotá",country_name:"Colombia",population:7881156},{city_name:"Hyderabad",country_name:"India",population:7749334},{city_name:"Dallas–Fort Worth",country_name:"United States",population:6700991},{city_name:"Santiago",country_name:"Chile",population:6683852},{city_name:"Madrid",country_name:"Spain",population:6387824},{city_name:"Ahmedabad",country_name:"India",population:6352254},{city_name:"Houston",country_name:"United States",population:6177035},{city_name:"Philadelphia",country_name:"United States",population:6018800},{city_name:"Washington, D.C.",country_name:"United States",population:5860342},{city_name:"Miami",country_name:"United States",population:5762717},{city_name:"Toronto",country_name:"Canada",population:5583064},{city_name:"Belo Horizonte",country_name:"Brazil",population:5504635},{city_name:"Atlanta",country_name:"United States",population:5457831},{city_name:"Barcelona",country_name:"Spain",population:5357422},{city_name:"Singapore",country_name:"Singapore",population:5312e3},{city_name:"Ruhr",country_name:"Germany",population:5135136},{city_name:"Berlin",country_name:"Germany",population:5097712},{city_name:"Pune",country_name:"India",population:5049968},{city_name:"Beijing",country_name:"China",population:125e4},{city_name:"Cairo",country_name:"Egypt",population:1445e3},{city_name:"Hong Kong-Shenzhen",country_name:"China",population:158e4},{city_name:"Karachi",country_name:"Pakistan",population:118e4},{city_name:"Shanghai",country_name:"China",population:1665e3}];
var City;
City = (function() {
function City(view, row) {
this.view = view;
this.population = ko.observable(row.population);
this.countryName = row.country_name;
this.cityName = row.city_name;
}
return City;
})();
this.CitiesModel = (function() {
function CitiesModel() {
var rows, tableOptions,
_this = this;
tableOptions = {
recordWord: 'city',
recordWordPlural: 'cities',
sortDir: 'desc',
sortField: 'population',
perPage: 15,
unsortedClass: "glyphicon glyphicon-sort",
ascSortClass: "glyphicon glyphicon-sort-by-attributes",
descSortClass: "glyphicon glyphicon-sort-by-attributes-alt"
};
rows = citiesList.map(function(row) {
return new City(_this, row);
});
this.table = new DataTable(rows, tableOptions);
ko.applyBindings(this, document.getElementById('sorted-table'));
}
return CitiesModel;
})();
this.CitiesModelUnsorted = (function() {
function CitiesModelUnsorted() {
var rows, tableOptions,
_this = this;
tableOptions = {
recordWord: 'city',
recordWordPlural: 'cities',
perPage: 15
};
rows = citiesList.map(function(row) {
return new City(_this, row);
});
this.table = new DataTable(rows, tableOptions);
ko.applyBindings(this, document.getElementById('unsorted-table'));
}
return CitiesModelUnsorted;
})();
</script>
</head>
<body>
<div class='container'>
<div class='header'>
<ul class='nav nav-pills pull-right'>
<li class='active'><a href="#">Demo</a></li>
<li><a href='https://github.com/immense/knockout-datatable'>Code</a></li>
</ul>
<h3 class='text-muted'>Knockout DataTable</h3>
</div>
<div class='jumbotron'>
<h1>Knockout DataTable Demo</h1>
</div>
<div class='row'>
<div class='col-md-12'>
<h2>Simple example</h2>
<p>S'pose we wanted to display a table of cities. Just create a view model for the data:</p>
<pre class="pre-scrollable">
class City
constructor: (@view, row) ->
@population = ko.observable row.population
@countryName = row.country_name
@cityName = row.city_name
class @CitiesModel
constructor: ->
tableOptions =
recordWord: 'city'
recordWordPlural: 'cities'
sortDir: 'desc'
sortField: 'population'
perPage: 15
unsortedClass: 'glyphicon glyphicon-sort'
ascSortClass: 'glyphicon glyphicon-sort-by-attributes'
descSortClass: 'glyphicon glyphicon-sort-by-attributes-alt'
@table = new DataTable [], tableOptions
@table.loading true
req = new XMLHttpRequest()
req.open 'GET', '/api/cities', true
req.onload = =>
if req.status >= 200 and req.status < 400
response = JSON.parse req.responseText
rows = response.results.map (row) => new City @, row
@table.rows rows
@table.loading false
else
alert "Error communicating with server"
@table.loading false
req.onerror = =>
alert "Error communicating with server"
@table.loading false
req.send()
ko.applyBindings @</pre>
<p>And a table, like so:</p>
<pre class="pre-scrollable">
<div data-bind="with: table">
<div class="pull-right">
<strong>Results per page</strong>
<select data-bind="options: [10,25,50], value: perPage"></select>
</div>
<input type="text" data-bind="textInput: filter" placeholder="Search"/>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th style="width: 34%;" data-bind="click: toggleSort('cityName')" class="sortable">
City
<i data-bind="css: sortClass('cityName')"></i>
</th>
<th style="width: 33%;" data-bind="click: toggleSort('countryName')" class="sortable">
Country
<i data-bind="css: sortClass('countryName')"></i>
</th>
<th style="width: 33%;" data-bind="click: toggleSort('population')" class="sortable">
Population
<i data-bind="css: sortClass('population')"></i>
</th>
</tr>
</thead>
<tbody>
<tr data-bind="visible: showNoData">
<td colspan="3" class="aligncenter">
This table has no data.
</td>
</tr>
<tr data-bind="visible: showLoading">
<td colspan="3" class="aligncenter">
<i data-bind="css: {'icon-spin': showLoading}" class="icon-spinner"></i>
Loading data...
</td>
</tr>
<!-- ko foreach: {data: pagedRows, as: '$row'} -->
<tr>
<td data-bind="text: $row.cityName"></td>
<td data-bind="text: $row.countryName"></td>
<td data-bind="text: $row.population"></td>
</tr>
<!-- /ko -->
</tbody>
</table>
<span data-bind="text: recordsText" class="label label-info pull-right"></span>
<div data-bind="visible: pages() > 1">
<ul class="pagination">
<li data-bind="css: leftPagerClass, click: prevPage">
<a href="#">&laquo;</a>
</li>
<!-- ko foreach: {data: (new Array(pages()))} -->
<li data-bind="css: $parent.pageClass($index() + 1)">
<a href="#" data-bind="text: $index() + 1, click: $parent.gotoPage($index() + 1)"></a>
</li>
<!-- /ko -->
<li data-bind="css: rightPagerClass, click: nextPage">
<a href="#">&raquo;</a>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function(){
new CitiesModel();
});
</script></pre>
<h3>Result:</h3>
<div id="sorted-table" data-bind="with: table">
<div class="pull-right">
<strong>Results per page</strong>
<select data-bind="options: [10,25,50], value: perPage"></select>
</div>
<input type="text" data-bind="textInput: filter" placeholder="Search"/>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th style="width: 34%;" data-bind="click: toggleSort('cityName')" class="sortable">
City
<i data-bind="css: sortClass('cityName')"></i>
</th>
<th style="width: 33%;" data-bind="click: toggleSort('countryName')" class="sortable">
Country
<i data-bind="css: sortClass('countryName')"></i>
</th>
<th style="width: 33%;" data-bind="click: toggleSort('population')" class="sortable">
Population
<i data-bind="css: sortClass('population')"></i>
</th>
</tr>
</thead>
<tbody>
<tr data-bind="visible: showNoData">
<td colspan="3" class="aligncenter">
This table has no data.
</td>
</tr>
<tr data-bind="visible: showLoading">
<td colspan="3" class="aligncenter">
<i data-bind="css: {'icon-spin': showLoading}" class="icon-spinner"></i>
Loading data...
</td>
</tr>
<!-- ko foreach: {data: pagedRows, as: '$row'} -->
<tr>
<td data-bind="text: $row.cityName"></td>
<td data-bind="text: $row.countryName"></td>
<td data-bind="text: $row.population"></td>
</tr>
<!-- /ko -->
</tbody>
</table>
<span data-bind="text: recordsText" class="label label-info pull-right"></span>
<div data-bind="visible: pages() > 1">
<ul class="pagination">
<li data-bind="css: leftPagerClass, click: prevPage">
<a href="#">«</a>
</li>
<!-- ko foreach: {data: (new Array(pages()))} -->
<li data-bind="css: $parent.pageClass($index() + 1)">
<a href="#" data-bind="text: $index() + 1, click: $parent.gotoPage($index() + 1)"></a>
</li>
<!-- /ko -->
<li data-bind="css: rightPagerClass, click: nextPage">
<a href="#">»</a>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function(){
window.view = new CitiesModel();
});
</script>
<h3>Example without sorting</h3>
<div id="unsorted-table" data-bind="with: table">
<div class="pull-right">
<strong>Results per page</strong>
<select data-bind="options: [10,25,50], value: perPage"></select>
</div>
<input type="text" data-bind="textInput: filter" placeholder="Search"/>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th style="width: 34%;">
City
</th>
<th style="width: 33%;">
Country
</th>
<th style="width: 33%;">
Population
</th>
</tr>
</thead>
<tbody>
<tr data-bind="visible: showNoData">
<td colspan="3" class="aligncenter">
This table has no data.
</td>
</tr>
<tr data-bind="visible: showLoading">
<td colspan="3" class="aligncenter">
<i data-bind="css: {'icon-spin': showLoading}" class="icon-spinner"></i>
Loading data...
</td>
</tr>
<!-- ko foreach: {data: pagedRows, as: '$row'} -->
<tr>
<td data-bind="text: $row.cityName"></td>
<td data-bind="text: $row.countryName"></td>
<td data-bind="text: $row.population"></td>
</tr>
<!-- /ko -->
</tbody>
</table>
<span data-bind="text: recordsText" class="label label-info pull-right"></span>
<div data-bind="visible: pages() > 1">
<ul class="pagination">
<li data-bind="css: leftPagerClass, click: prevPage">
<a href="#">«</a>
</li>
<!-- ko foreach: {data: (new Array(pages()))} -->
<li data-bind="css: $parent.pageClass($index() + 1)">
<a href="#" data-bind="text: $index() + 1, click: $parent.gotoPage($index() + 1)"></a>
</li>
<!-- /ko -->
<li data-bind="css: rightPagerClass, click: nextPage">
<a href="#">»</a>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function(){
window.unsorted_cities = new CitiesModelUnsorted();
});
</script>
<h2>Options</h2>
<p>When instanciating with <code>new DataTable</code> you have can pass in the following options as the second parameter:</p>
<dl class='dl-horizontal'>
<dt><code>recordWord</code></dt>
<dd>The name of your rows. In the case above, we used <code>city</code>. Default: <code>record</code></dd>
<dt><code>recordWordPlural</code></dt>
<dd>The plural name of your rows. Since we used <code>city</code> as our recordWord, we used <code>cities</code> for recordWordPlural. Default: <code>recordWord + 's'</code></dd>
<dt><code>sortDir</code></dt>
<dd>The initial sorting direction for the table. Default: <code>'asc'</code></dd>
<dt><code>sortField</code></dt>
<dd>The initial sorting column for the table. As of v0.5.0, this setting is optional and the order of <code>table.rows</code> will be maintained and sorting will be disabled.</dd>
<dt><code>perPage</code></dt>
<dd>Integer indicating the number of rows to be shown per page. Default: <code>15</code></dd>
<dt><code>unsortedClass</code><br /><code>descSortClass</code><br /><code>ascSortClass</code></dt>
<dd>The classes given to the icons in the <code>th</code> elements indicating the direction of sorting. Set to '' if you would rather have no icons. Default: <code>''</code> for each</dd>
</dl>
<p>Additionally, you can define the <code>match</code> function on the row class, and the datatable will use it for filtering. If left undefined (as in the example above), the DataTable will automatically search all columns defined on the row. E.g:</p>
<dl>
<dt><code>row.match:</code></dt>
<dd><pre>
(filter) ->
@population().toLowerCase().indexOf(filter) >= 0 or
@countryName .toLowerCase().indexOf(filter) >= 0 or
@cityName .toLowerCase().indexOf(filter) >= 0</pre></dd>
</dl>
<h2>Further Usage</h2>
<p>Knockout DataTable comes packaged with some advanced filtering. Below is a list of example search terms and the results returned.</p>
<dl>
<dt><code>cityName:atlanta</code></dt>
<dd>Results with 'atlanta' in cityName (case insensitive)</dd>
<dt><code>cItYnAmE:aTlAnTa</code></dt>
<dd>Results with 'atlanta' in cityName (case insensitive)</dd>
<dt><code>countryName:United cityName:L</code></dt>
<dd>Results with 'united' in countryName and 'l' in cityName (case insensitive)<p class="text-muted">Note: as of right now, there is no built-in support for multi-word searching with ':'-delimeted searching</span></dd>
<dt><code>countryname:japan 6</code></dt>
<dd>Results with 'japan' in countryName and '6' somewhere in one of the columns (case insensitive)</dd>
</dl>
</div>
</div>
</div>
</body>
</html>