Login / Join
Sorting Table Colums with Javascript
submitted on 14/06/2013 by
Codicode
+ infos
Config
Embed
Share
Sorting Table Colums with Javascript
by
Codicode
Sorting Table Colums populated via json with Javascript and jQuery
Framework 1 :
-- None --
jQuery 2.0.3
jQuery 1.9.1
jQuery 1.9.0
jQuery 1.8.0
jQuery 1.7.1
jQuery 1.6.3
jQuery UI 1.10.3
MooTools 1.4.5
AngularJS 1.0.4
Prototype 1.7.1.0
Dojo 1.8.3
Chrome Frame 1.0.3
Ext Core 3.1.0
SWFObject 2.2
WebFont Loader 1.1.2
Framework 2 :
-- None --
jQuery 2.0.3
jQuery 1.9.1
jQuery 1.9.0
jQuery 1.8.0
jQuery 1.7.1
jQuery 1.6.3
jQuery UI 1.10.3
MooTools 1.4.5
AngularJS 1.0.4
Prototype 1.7.1.0
Dojo 1.8.3
Chrome Frame 1.0.3
Ext Core 3.1.0
SWFObject 2.2
WebFont Loader 1.1.2
+ Js file(s) :
+ Css file(s) :
Apply
Link :
Direct http link :
Fork button :
Html code :
Embed preview :
Copy the following html code to your page to embed the preview,
To include many previews on a single page, just repeat the first line.
You can change the width, height and border color (bcolor).
<div class="refork-widget" code="0ec" style="width:100%;height:200px;" bcolor="#ccc"></div>
<script type="text/javascript" src="//snippet.run/d/z.js"></script>
HTML
<div align="center"> <table> <thead id="headings"> <tr> <th id="fname">First Name</th> <th id="lname">Last Name</th> <th id="age">Age</th> </tr> </thead> <tbody id="results"> </tbody> </table> </div>
CSS
body { background-color:#fff; font:verdana; } table { margin: 3px; } table th { padding: 5px 0px 5px 0px; border: 0px solid #000; background-color:#222; color:#fff; font-weight: bold; cursor: pointer; } table td { padding: 5px 50px 5px 50px; border: 0px solid #000; background-color:#ddd; }
Javascript
var arr = [ { fname: 'Fred', lname: 'Alexander', age: 19 }, { fname: 'Kraig', lname: 'Malao', age: 81 }, { fname: 'Salomon', lname: 'Kipppa', age: 38 } ]; $(function() { $('#headings th').click(function() { var id = $(this).attr('id'); var asc = (!$(this).attr('asc')); // switch the order, true if not set // set asc="asc" when sorted in ascending order $('#headings th').each(function() { $(this).removeAttr('asc'); }); if (asc) $(this).attr('asc', 'asc'); sortResults(id, asc); }); showResults(); }); function sortResults(prop, asc) { arr = arr.sort(function(a, b) { if (asc) return (a[prop] > b[prop]); else return (b[prop] > a[prop]); }); showResults(); } function showResults () { var html = ''; for (var e in arr) { html += '<tr>' +'<td>'+arr[e].fname+'</td>' +'<td>'+arr[e].lname+'</td>' +'<td>'+arr[e].age+'</td>' +'</tr>'; } $('#results').html(html); }
©2013 Copyright snippet.run
Privacy
-
Terms of use
-
About
-
Contact