File: example-widget-align-character.html

package info (click to toggle)
jquery-tablesorter 1%3A2.31.3%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 5,056 kB
  • sloc: javascript: 19,495; sh: 14; makefile: 8
file content (457 lines) | stat: -rw-r--r-- 16,997 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>jQuery tablesorter 2.0 - Align Character Widget</title>

	<!-- jQuery -->
	<script src="js/jquery-latest.min.js"></script>

	<!-- Demo stuff -->
	<link rel="stylesheet" href="css/jq.css">
	<link href="css/prettify.css" rel="stylesheet">
	<link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css">
	<script src="js/jquery-ui.min.js"></script>
	<script src="js/prettify.js"></script>
	<script src="js/docs.js"></script>

	<!-- Tablesorter: required -->
	<link rel="stylesheet" href="../css/theme.blue.css">
	<script src="../js/jquery.tablesorter.js"></script>
	<script src="../js/jquery.tablesorter.widgets.js"></script>

	<script src="../js/widgets/widget-alignChar.js"></script>

	<style>
	/* demo stuff */
	.accordion th:nth-child(2) {
		width: 140px;
	}
	#demo th {
		width: 25%;
	}
	</style>

	<style id="css">/* CSS needed for this widget */
.ts-align-wrap {
	display: block;
	white-space: nowrap; /* optional */
	width: 100%;
	overflow: hidden;
}
.ts-align-wrap, .ts-align-left, .ts-align-right {
	-moz-box-sizing: border-box;
	-webkit-box-sizing: border-box;
	box-sizing: border-box;
}
.ts-align-left {
	display: inline-block;
	text-align:right;
}
.ts-align-right {
	display: inline-block;
	text-align:left;
}
/* optional - colorize alignment character in Animals column */
td:nth-child(3) .ts-align-right i {
	color: red;
}</style>

	<script id="js">$(function() {

	$("table").tablesorter({
		theme: 'blue',
		widgets: ['zebra', 'alignChar'],
		widgetOptions : {
			alignChar_wrap         : '<i/>',
			alignChar_charAttrib   : 'data-align-char',
			alignChar_indexAttrib  : 'data-align-index',
			alignChar_adjustAttrib : 'data-align-adjust' // percentage width adjustments
		}
	});

});
</script>
</head>
<body id="align">

<div id="banner">
	<h1>table<em>sorter</em></h1>
	<h2>Align Character Widget</h2>
	<h3>Flexible client-side table sorting</h3>
	<a href="index.html">Back to documentation</a>
</div>

<div id="main">

	<p></p>
	<br>

	<div class="accordion">

		<h3><a href="#">Notes</a></h3>
		<div>
		<ul>
			<li>In <span class="version">v2.31.3</span>, tweaked the css to prevent change in vertical height. See <a href="https://github.com/Mottie/tablesorter/issues/1713">issue #1713</a>.</li>
			<li>In <span class="version">v2.19.0</span>, if there is nothing to the right of the set align character, then the align character will not be added; this prevents hanging decimals like <code>3000.</code>.<p></p></li>
			<li>This widget will <strong>only work</strong> in tablesorter version 2.8+ and jQuery version 1.7+.</li>
			<li>Make sure to include the CSS shown below to maintain the alignment.</li>
			<li><span class="label warning">*NOTE*</span> if the table cell becomes too narrow, the alignment <strong>will not</strong> be maintained &amp; some content may overflow into the next/previous cell (the widget css does include <code>overflow:hidden</code> on the wrapper, so some content will be hidden instead of overflowing).</li>
			<li>Until the day that <code>text-align: '.' center;</code> becomes standard in CSS4 (<a href="https://www.w3.org/TR/css-text-4/#character-alignment">ref</a>), if ever, this widget might prove useful.</li>
		</ul>
		</div>

		<h3><a href="#">Options</a></h3>
		<div>
			<h3>Align Character (alignChar) widget default options (added inside of tablesorter <code>widgetOptions</code>)</h3>
			<div>
				<span class="label label-info">TIP!</span> Click on the link in the Option column to reveal full details (or <a href="#" class="toggleAll">toggle</a>|<a href="#" class="showAll">show</a>|<a href="#" class="hideAll">hide</a> all) or double click to update the browser location.
			</div>
			<table class="tablesorter-blue">
				<colgroup><col style="width:135px"></colgroup>
				<thead>
					<tr><th>Option</th><th>Default</th><th class="sorter-false">Description</th></tr>
				</thead>
				<tbody>
					<tr>
						<td><a href="#" class="permalink">alignChar_wrap</a></td>
						<td><code>''</code></td>
						<td>
							This option allows you to wrap the alignment character in any HTML:
							<div class="collapsible">
							<br>
							When the widget completes the alignment, it is basically splitting the content of the table cell into two halves, and wrapping it in a span element. Below is an example of a modified cell showing all contents; it's from the "Animals" column (the cell that contains "Ox = stinky")
							<pre class="prettyprint lang-html">&lt;td&gt;
	&lt;span class=&quot;ts-align-wrap&quot;&gt;
		&lt;span class=&quot;ts-align-left&quot; style=&quot;min-width:37%&quot;&gt;Ox&amp;nbsp;&lt;/span&gt;
		&lt;span class=&quot;ts-align-right&quot; style=&quot;min-width:63%&quot;&gt;=&amp;nbsp;stinky&lt;/span&gt;
	&lt;/span&gt;
&lt;/td&gt;</pre>
							When this option is set with some HTML:
							<br><pre class="prettyprint lang-js">alignChar_wrap : '&lt;i/&gt;'</pre>
							it results in this layout:
							<pre class="prettyprint lang-html">&lt;td&gt;
	&lt;span class=&quot;ts-align-wrap&quot;&gt;
		&lt;span class=&quot;ts-align-left&quot; style=&quot;min-width:37%&quot;&gt;Ox&amp;nbsp;&lt;/span&gt;
		&lt;span class=&quot;ts-align-right&quot; style=&quot;min-width:63%&quot;&gt;
			&lt;i&gt;=&lt;/i&gt;&amp;nbsp;stinky
		&lt;/span&gt;
	&lt;/span&gt;
&lt;/td&gt;</pre>
							</div>
						</td>
					</tr>
					<tr>
						<td><a href="#" class="permalink">alignChar_charAttrib</a></td>
						<td><code>'data-align-char'</code></td>
						<td>
							This option points to the header data-attribute which contains the desired alignment character.
							<div class="collapsible">
								<br>
								Add it to the header as follows:
								<pre class="prettyprint lang-html">&lt;th data-align-char="."&gt;Numeric&lt;/th&gt;</pre>
								The <code>data-align-char</code> data-attribute contains the actual character to align. There is no default alignment character, so if this data-attribute is undefined, the column will be ignored.<br>
								<br>
								<span class="label warning">*NOTE*</span> if wanting to align the content on a space (see the "AlphaNumeric" column), use <code>&amp;nbsp;</code> instead of a regular space because all spaces within the content are replaced by non-breaking spaces.
								<pre class="prettyprint lang-html">&lt;th data-align-char="&amp;nbsp;"&gt;AlphaNumeric&lt;/th&gt;</pre>
							</div>
						</td>
					</tr>
					<tr>
						<td><a href="#" class="permalink">alignChar_indexAttrib</a></td>
						<td><code>'data-align-index'</code></td>
						<td>This option points to the header data-attribute which contains the desired alignment character index (<strong>one-based index</strong>).
							<div class="collapsible">
								<ul>
									<li>What that means is if your data has multiple periods, like in the URLs of the sites column below, you can tell the widget which period to line-up on.</li>
									<li>The alignment may seem to act incorrectly, if your data doesn't have enough periods to line up on, but it's doing the best it can! For example, in the sites column below, a table cell contains <code>"rental.nytimes.com/index.html"</code>. This URL has three periods, while all other cells in that column only contain two. When the <code>data-align-index</code> is set to <code>"3"</code>, the widget aligns the content on that third period. All of the other columns only have two, so they all align on their last period. Does that make it clear?</li>
									<li>Try adjusting the sliders in this demo to change the <code>data-align-index</code> of the "Animals" and "Sites" columns to see how this works.</li>
								</ul>
								Add this attribute to the header as follows:
								<pre class="prettyprint lang-html">&lt;th data-align-char="=" data-align-index="1"&gt;Animals&lt;/th&gt;</pre>
								The <code>data-align-index</code> data-attribute contains the actual character index. The index defaults to "1" if this data-attribute is undefined.
							</div>
						</td>
					</tr>
					<tr>
						<td><a href="#" class="permalink">alignChar_adjustAttrib</a></td>
						<td><code>'data-align-adjust'</code></td>
						<td>This option allows you to tweak the horizontal position of the aligned cell content.
							<div class="collapsible">
								<ul>
									<li>Here is an example of a modified cell showing its contents; it's from the "Numeric" column (the cell that contains "87.20000"):
										<pre class="prettyprint lang-html">&lt;td&gt;
		&lt;span class=&quot;ts-align-wrap&quot;&gt;
			&lt;span class=&quot;ts-align-left&quot; style=&quot;min-width:36%&quot;&gt;87&lt;/span&gt;
			&lt;span class=&quot;ts-align-right&quot; style=&quot;min-width:64%&quot;&gt;.20000&lt;/span&gt;
		&lt;/span&gt;
	&lt;/td&gt;</pre>As you can see, the "87" (integer-part) is in a "ts-align-left" span, set with a <code>text-align: right;</code> (it's on the left, but right aligned)<br>
									The ".20000" (fractional-part) is in a "ts-align-right" span, set with a <code>text-align: left;</code> (opposite of the other one)!<br>
									</li>
									<li>So in the above example, the left side is set to a width of "36%" and the right to "64%" (adding up to 100%), which "sort-of" centers the aligned character.</li>
									<li>If you don't want to move the position of the aligned character, you can set the <code>data-align-adjust</code> data-attribute with a percentage change. This value (positive or negative) will be added to the "ts-align-left" span, and subtracted from the "ts-align-right" span.</li>
									<li>Try adjusting the sliders in this demo to change the <code>data-align-adjust</code> of the "Sites" column to see how this works.</li>
								</ul>
								Add this attribute to the header as follows:
								<pre class="prettyprint lang-html">&lt;th data-align-char="." data-align-adjust="10"&gt;Numeric&lt;/th&gt;</pre>
								The <code>data-align-adjust</code> data-attribute contains a percentage value (without the percent sign). The adjustment defaults to "0" if this data-attribute is undefined.
							</div>
						</td>
					</tr>
				</tbody>
			</table>
		</div>

		<h3><a href="#">CSS</a></h3>
		<div>
			The following is *required* css. Without it, the alignment will be all wrong.
			<div id="css">
				<pre class="prettyprint lang-css"></pre>
			</div>
		</div>

	</div>

	<p></p>

<h1>Demo</h1>

<span class="demo-label">Animals align index:</span> <div id="slider0"></div> <span class="bright">1</span> (only indexes of 1 or 2 will work)<br>
<span class="demo-label">Sites align index:</span> <div id="slider1"></div> <span class="bright">1</span> (only indexes of 1 - 3 will work<br>
<span class="demo-label">Sites adjust value:</span> <div id="slider2"></div> <span class="bright">0</span>

	<div id="demo"><table class="tablesorter">
	<thead>
		<tr>
			<th data-align-char="&nbsp;">AlphaNumeric</th>
			<th data-align-char="." data-align-adjust="0">Numeric</th>
			<th data-align-char="=" data-align-index="1">Animals</th>
			<th data-align-char="." data-align-index="1">Sites</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>abc 123</td>
			<td>.423475</td>
			<td>Koala = cute = cudley</td>
			<td>search.google.com</td>
		</tr>
		<tr>
			<td>abc 1</td>
			<td>23.4</td>
			<td>Ox = stinky</td>
			<td>mail.yahoo.com</td>
		</tr>
		<tr>
			<td>abc 9</td>
			<td>1.0</td>
			<td>Girafee = tall</td>
			<td>http://www.facebook.com</td>
		</tr>
		<tr>
			<td>zyx 24</td>
			<td>7.67</td>
			<td>Bison = burger</td>
			<td>http://internship.whitehouse.gov/</td>
		</tr>
		<tr>
			<td>abc 11</td>
			<td>3000</td>
			<td>Chimp = banana lover</td>
			<td>about.ucla.edu/</td>
		</tr>
		<tr>
			<td>abc 2</td>
			<td>56.5</td>
			<td>Elephant = unforgetable</td>
			<td>http://www.wikipedia.org/</td>
		</tr>
		<tr>
			<td>abc 9</td>
			<td>15.5</td>
			<td>Lion = rawr</td>
			<td>rental.nytimes.com/index.html</td>
		</tr>
		<tr>
			<td>ABC 10</td>
			<td>87.20000</td>
			<td>Zebra = stripey</td>
			<td>http://android.google.com</td>
		</tr>
		<tr>
			<td>zyx 1</td>
			<td>999.1</td>
			<td>Koala = cute, again!</td>
			<td>http://irsmrt.mit.edu/</td>
		</tr>
		<tr>
			<td>zyx 12</td>
			<td>.2</td>
			<td>Llama = llove it</td>
			<td>http://aliens.nasa.gov/</td>
		</tr>
	</tbody>
</table></div>

	<h1>Page Header</h1>
	<div>
		<pre class="prettyprint lang-html">&lt;!-- Tablesorter: required --&gt;
&lt;link href=&quot;../css/theme.blue.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;../js/jquery.tablesorter.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../js/jquery.tablesorter.widgets.js&quot;&gt;&lt;/script&gt;

&lt;!-- Align Character widget --&gt;
&lt;script src=&quot;../js/widgets/widget-alignChar.js&quot;&gt;&lt;/script&gt;</pre>
	</div>

	<h1>Script</h1>
	<div id="javascript">
		<pre class="prettyprint lang-js"></pre>
	</div>

	<h1>CSS</h1>
	<div id="css">
		<pre class="prettyprint lang-css"></pre>
	</div>

	<h1>HTML</h1>
	<div>
		<pre class="prettyprint lang-html">&lt;table class=&quot;tablesorter&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th data-align-char=&quot;&amp;nbsp;&quot;&gt;AlphaNumeric&lt;/th&gt;
      &lt;th data-align-char=&quot;.&quot; data-align-adjust=&quot;0&quot;&gt;Numeric&lt;/th&gt;
      &lt;th data-align-char=&quot;=&quot; data-align-index=&quot;1&quot;&gt;Animals&lt;/th&gt;
      &lt;th data-align-char=&quot;.&quot; data-align-index=&quot;1&quot;&gt;Sites&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;abc 123&lt;/td&gt;
      &lt;td&gt;.423475&lt;/td&gt;
      &lt;td&gt;Koala = cute = cudley&lt;/td&gt;
      &lt;td&gt;search.google.com&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;abc 1&lt;/td&gt;
      &lt;td&gt;23.4&lt;/td&gt;
      &lt;td&gt;Ox = stinky&lt;/td&gt;
      &lt;td&gt;mail.yahoo.com&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;abc 9&lt;/td&gt;
      &lt;td&gt;1.0&lt;/td&gt;
      &lt;td&gt;Girafee = tall&lt;/td&gt;
      &lt;td&gt;http://www.facebook.com&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;zyx 24&lt;/td&gt;
      &lt;td&gt;7.67&lt;/td&gt;
      &lt;td&gt;Bison = burger&lt;/td&gt;
      &lt;td&gt;http://internship.whitehouse.gov/&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;abc 11&lt;/td&gt;
      &lt;td&gt;3000&lt;/td&gt;
      &lt;td&gt;Chimp = banana lover&lt;/td&gt;
      &lt;td&gt;about.ucla.edu/&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;abc 2&lt;/td&gt;
      &lt;td&gt;56.5&lt;/td&gt;
      &lt;td&gt;Elephant = unforgetable&lt;/td&gt;
      &lt;td&gt;http://www.wikipedia.org/&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;abc 9&lt;/td&gt;
      &lt;td&gt;15.5&lt;/td&gt;
      &lt;td&gt;Lion = rawr&lt;/td&gt;
      &lt;td&gt;rental.nytimes.com/index.html&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ABC 10&lt;/td&gt;
      &lt;td&gt;87.20000&lt;/td&gt;
      &lt;td&gt;Zebra = stripey&lt;/td&gt;
      &lt;td&gt;http://android.google.com&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;zyx 1&lt;/td&gt;
      &lt;td&gt;999.1&lt;/td&gt;
      &lt;td&gt;Koala = cute, again!&lt;/td&gt;
      &lt;td&gt;http://irsmrt.mit.edu/&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;zyx 12&lt;/td&gt;
      &lt;td&gt;0.2&lt;/td&gt;
      &lt;td&gt;Llama = llove it&lt;/td&gt;
      &lt;td&gt;http://aliens.nasa.gov/&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;</pre>
	</div>

</div>

<script>
/* DEMO ONLY CODE */
$(function() {
	var $table = $('#demo table'),
		c = $table[0].config,
		$animals = c.$headers.eq(2),
		$sites = c.$headers.eq(3);
	$animals.find('.tablesorter-header-inner')[0].innerHTML += ' ( "data-align-index" : <span></span> )';
	$sites.find('.tablesorter-header-inner')[0].innerHTML += ' ( "data-align-index" : <span></span>, "data-align-adjust" : <span></span> )';
	$( "#slider0" ).slider({
		value: 1,
		min: 0,
		max: 3,
		step: 1,
		create: function() {
			$animals.find('span').html( $animals.attr('data-align-index') );
		},
		slide: function( event, ui ) {
			$animals
				.attr('data-align-index', ui.value)
				.find('span').html( ui.value );
			$('.bright:eq(0)').html( ui.value );
			$table.trigger('refreshAlign');
		}
	});
	$( "#slider1" ).slider({
		value: 1,
		min: 0,
		max: 4,
		step: 1,
		create: function() {
			$sites.find('span').html( $sites.attr('data-align-index') );
		},
		slide: function( event, ui ) {
			$sites
				.attr('data-align-index', ui.value)
				.find('span:eq(0)').html( ui.value );
			$('.bright:eq(1)').html( ui.value );
			$table.trigger('refreshAlign');
		}
	});
	$( "#slider2" ).slider({
		value: 0,
		min: -30,
		max: 30,
		step: 5,
		create: function() {
			$sites.find('span').html( $sites.attr('data-align-index') );
		},
		slide: function( event, ui ) {
			$sites
				.attr('data-align-adjust', ui.value)
				.find('span:eq(1)').html( ui.value );
			$('.bright:eq(2)').html( ui.value );
			$table.trigger('refreshAlign');
		}
	});

});
</script>

</body>
</html>