| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>jQuery tablesorter 2.0 - Formatter Widget</title> |
| 6 |
|
| 7 |
|
| 8 |
<script src="js/jquery-latest.min.js"></script> |
| 9 |
|
| 10 |
<script> |
| 11 |
// tweak "Added" column to keep recent dates |
| 12 |
$(function() { |
| 13 |
var d, t, |
| 14 |
tweaks = [ 9e5, 8e8, 2e7, 0, 6e10, 1e11 ], |
| 15 |
$t = $('#demo tbody'), |
| 16 |
date = new Date().getTime(); |
| 17 |
$t.find('tr td:nth-child(4)').each(function(i) { |
| 18 |
d = new Date( date - tweaks[i] ); |
| 19 |
t = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear() + ' ' + |
| 20 |
d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); |
| 21 |
$(this).html( t ); |
| 22 |
}); |
| 23 |
}); |
| 24 |
</script> |
| 25 |
|
| 26 |
|
| 27 |
<link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css"> |
| 28 |
<script src="js/jquery-ui.min.js"></script> |
| 29 |
<link rel="stylesheet" href="css/jq.css"> |
| 30 |
<link href="css/prettify.css" rel="stylesheet"> |
| 31 |
<script src="js/prettify.js"></script> |
| 32 |
<script src="js/docs.js"></script> |
| 33 |
|
| 34 |
|
| 35 |
<link rel="stylesheet" href="../css/theme.blue.css"> |
| 36 |
<link rel="stylesheet" href="../css/theme.ice.css"> |
| 37 |
<script src="../js/jquery.tablesorter.js"></script> |
| 38 |
<script src="../js/widgets/widget-filter.js"></script> |
| 39 |
|
| 40 |
<script src="../js/widgets/widget-formatter.js"></script> |
| 41 |
|
| 42 |
<style id="css">/* demo styling */ |
| 43 |
#table strong { |
| 44 |
color: #a00; |
| 45 |
}</style> |
| 46 |
<script id="js">$(function() { |
| 47 |
|
| 48 |
$('.tablesorter').tablesorter({ |
| 49 |
theme: 'blue', |
| 50 |
widgets: ['formatter', 'filter'], |
| 51 |
widgetOptions: { |
| 52 |
filter_matchType : { 'input': 'match', 'select': 'exact' }, |
| 53 |
formatter_column: { |
| 54 |
'.emphasize' : function( text, data ) { |
| 55 |
return '<strong>' + text + '</strong>'; |
| 56 |
}, |
| 57 |
'.supercar' : function( text, data ) { |
| 58 |
if ( text === '' ) { return ''; } |
| 59 |
// replace table cell with a link |
| 60 |
return '<a href="https://www.google.com/search?tbm=isch&q=' + text.replace(/\s/g, '+') + '">' + text + '</a>'; |
| 61 |
}, |
| 62 |
'.date' : function( text, data ) { |
| 63 |
var date = new Date( text ); |
| 64 |
if ( date instanceof Date && isFinite( date ) ) { |
| 65 |
data.$cell.attr( data.config.textAttribute, text ); |
| 66 |
return '<em>' + prettyDate( date ) + '</em>'; |
| 67 |
} |
| 68 |
return text; |
| 69 |
}, |
| 70 |
'.duckify' : function( text, data ) { |
| 71 |
if ( text === '' ) { return ''; } |
| 72 |
// add text to data-attribute; this overrides the parser |
| 73 |
data.$cell.attr( data.config.textAttribute, text ); |
| 74 |
// replace table cell with a link |
| 75 |
return '<a href="https://duckduckgo.com/?ia=images&q=' + text + '">Search The Duck</a>'; |
| 76 |
}, |
| 77 |
'.blue' : function( text, data ) { |
| 78 |
return '<span class="blue">' + text + '</span>'; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
}); |
| 85 |
|
| 86 |
/* |
| 87 |
* JavaScript Pretty Date (MODIFIED) |
| 88 |
* Copyright (c) 2011 John Resig (ejohn.org) |
| 89 |
* Licensed under the MIT and GPL licenses. |
| 90 |
*/ |
| 91 |
function prettyDate(date) { |
| 92 |
var diff = (((new Date()).getTime() - date.getTime()) / 1000), |
| 93 |
day_diff = Math.floor(diff / 86400); |
| 94 |
if ( isNaN(day_diff) || day_diff < 0 ) { return ''; } |
| 95 |
return day_diff == 0 && ( |
| 96 |
diff < 60 && 'just now' || |
| 97 |
diff < 120 && '1 minute ago' || |
| 98 |
diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' || |
| 99 |
diff < 7200 && '1 hour ago' || |
| 100 |
diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') || |
| 101 |
day_diff == 1 && 'Yesterday' || |
| 102 |
day_diff < 7 && day_diff + ' days ago' || |
| 103 |
day_diff < 61 && Math.ceil( day_diff / 7 ) + ' weeks ago' || |
| 104 |
day_diff < 730 && Math.floor( day_diff / 30 ) + ' months ago' || |
| 105 |
Math.floor( day_diff / 365 ) + ' years ago'; |
| 106 |
}</script> |
| 107 |
</head> |
| 108 |
<body> |
| 109 |
<div id="banner"> |
| 110 |
<h1>table<em>sorter</em></h1> |
| 111 |
<h2>Formatter Widget</h2> |
| 112 |
<h3>Flexible client-side table sorting</h3> |
| 113 |
<a href="index.html">Back to documentation</a> |
| 114 |
</div> |
| 115 |
<div id="main"> |
| 116 |
|
| 117 |
<p></p> |
| 118 |
<br> |
| 119 |
|
| 120 |
<div id="root" class="accordion"> |
| 121 |
|
| 122 |
<h3><a href="#">Notes</a></h3> |
| 123 |
<div> |
| 124 |
<ul> |
| 125 |
<li>This widget will only work in tablesorter version 2.16+ and jQuery version 1.7+.</li> |
| 126 |
<li>It allows you to add custom formatting to table columns.</li> |
| 127 |
<li>You have the option of storing the original cell text within the table cell data-attribute to maintain the sort order.</li> |
| 128 |
<li>This widget is not optimized for large tables; but it only runs on initialization and after any updates have occurred.</li> |
| 129 |
</ul> |
| 130 |
</div> |
| 131 |
|
| 132 |
<h3><a href="#">Options</a></h3> |
| 133 |
<div> |
| 134 |
<h4>Formatter widget default options (added inside of tablesorter <code>widgetOptions</code>)</h4> |
| 135 |
<div> |
| 136 |
<span class="label label-info">TIP!</span> Click on the link in the function 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. |
| 137 |
</div> |
| 138 |
<table class="options tablesorter-blue"> |
| 139 |
<thead> |
| 140 |
<tr><th class="option">Option</th><th class="sorter-false">Description</th></tr> |
| 141 |
</thead> |
| 142 |
<tbody> |
| 143 |
|
| 144 |
<tr id="formatter-column"> |
| 145 |
<td><a href="#" class="permalink">formatter_column</a></td> |
| 146 |
<td>Assign a formatting function by column index or column header class name; these functions only apply to the <code>tbody</code> cells. |
| 147 |
<div class="collapsible"> |
| 148 |
<br> |
| 149 |
Set up the formatter to return the HTML for the table cell. |
| 150 |
<pre class="prettyprint lang-js">formatter_column: { |
| 151 |
'.emphasize' : function( text, data ) { |
| 152 |
return '<strong>' + text + '</strong>'; |
| 153 |
} |
| 154 |
}</pre> |
| 155 |
If the text in the cell is to be changed, but you don't want the sort to be effected, then add the original text into the data-attribute as follows: |
| 156 |
<pre class="prettyprint lang-js">formatter_column: { |
| 157 |
'.makesearch' : function( text, data ) { |
| 158 |
// don't make a link if there is no text in the cell |
| 159 |
if ( text === '' ) { return ''; } |
| 160 |
// add text to data-attribute; this overrides the parser |
| 161 |
data.$cell.attr( data.config.textAttribute, text ); |
| 162 |
// then replace text with a link |
| 163 |
return '<a href="http://mysite.com?query=' + encodeURIComponent(text) + '">Get Definition</a>'; |
| 164 |
} |
| 165 |
}</pre> |
| 166 |
The <code>data</code> parameter provided to the <code>formatter_column</code> function contains the following information: |
| 167 |
<pre class="prettyprint lang-js">data = { |
| 168 |
config : table.config, |
| 169 |
wo : table.config.widgetOptions, |
| 170 |
$header : $('th'), // the header cell of the current column |
| 171 |
$row : $('tr'), // jQuery object of the row currently being processed |
| 172 |
$cell : $('td'), // jQuery object of the cell currently being processed |
| 173 |
text : 'Batman', // the text from the current cell |
| 174 |
columnIndex : 0 // the column index of the current cell |
| 175 |
}</pre> |
| 176 |
</div> |
| 177 |
</td> |
| 178 |
</tr> |
| 179 |
|
| 180 |
<tr id="formatter-event"> |
| 181 |
<td><a href="#" class="permalink">formatter_event</a></td> |
| 182 |
<td>The formatter will update when this event is triggered on the table. |
| 183 |
<div class="collapsible"> |
| 184 |
<br> |
| 185 |
The default setting is <code>'applyFormatter'</code> |
| 186 |
<p></p> |
| 187 |
The formatter is set up to run again after an "updateComplete" or "pagerComplete" event fires; this option allows setting a custom-named event to force the formatter to update in addition to the complete events. |
| 188 |
<p></p> |
| 189 |
<span class="label warning">* NOTE *</span> So, if the text in the cell gets modified by the formatter, make sure your code saves the original text into the data-attribute or it will get lost. |
| 190 |
<p></p> |
| 191 |
Force an update as follows: |
| 192 |
<pre class="prettyprint lang-js">$('table').trigger('applyFormatter');</pre> |
| 193 |
</div> |
| 194 |
</td> |
| 195 |
</tr> |
| 196 |
|
| 197 |
</tbody> |
| 198 |
</table> |
| 199 |
</div> |
| 200 |
|
| 201 |
</div> |
| 202 |
|
| 203 |
<p></p> |
| 204 |
<h1>Demo</h1> |
| 205 |
|
| 206 |
<div id="demo"><table id="table" class="tablesorter"> |
| 207 |
<thead> |
| 208 |
<tr> |
| 209 |
<th class="emphasize filter-exact">Super Hero</th> |
| 210 |
<th class="supercar">Super Car</th> |
| 211 |
<th class="duckify filter-select">Image Links</th> |
| 212 |
<th class="date">Added</th> |
| 213 |
</tr> |
| 214 |
</thead> |
| 215 |
<tbody> |
| 216 |
|
| 217 |
<tr><td>Superman</td><td>Bugatti Veyron</td><td>Duckie</td><td>1/1/2015</td></tr> |
| 218 |
<tr><td>Flash</td><td>Hennessey Venom</td><td>Horse</td><td>12/22/2014</td></tr> |
| 219 |
<tr><td>Batman</td><td>Koenigsegg Agera</td><td>Elephant</td><td>3/10/2010</td></tr> |
| 220 |
<tr><td>Green Lantern</td><td>SSC Ultimate Aero</td><td>Cow</td><td>5/5/2012</td></tr> |
| 221 |
<tr><td>Howard the Duck</td><td>Koenigsegg CCX</td><td>Pizza</td><td>1/1/2005</td></tr> |
| 222 |
<tr><td>Wonder Woman</td><td>McLaren F1</td><td>Tiger</td><td>3/1/2013</td></tr> |
| 223 |
</tbody> |
| 224 |
</table></div> |
| 225 |
|
| 226 |
<h1>Javascript</h1> |
| 227 |
<div id="javascript"> |
| 228 |
<pre class="prettyprint lang-javascript"></pre> |
| 229 |
</div> |
| 230 |
<h1>CSS</h1> |
| 231 |
<div id="css"> |
| 232 |
<pre class="prettyprint lang-css"></pre> |
| 233 |
</div> |
| 234 |
<h1>HTML</h1> |
| 235 |
<div id="html"> |
| 236 |
<pre class="prettyprint lang-html"></pre> |
| 237 |
</div> |
| 238 |
|
| 239 |
</div> |
| 240 |
|
| 241 |
</body> |
| 242 |
</html> |