NOTE!
- This widget can be applied to the original plugin. The code is in the "jquery.tablesorter.widgets.js" file. But you'll need to add the
<i>
into the header manually. - New! v2.7!
- You will need to modify the
headerTemplate
option to include the bootstrap icon! See the example in the code. - Setting
theme
option to"Bootstrap"
will now override/ignore theuitheme
value in thewidgetOptions
.
- You will need to modify the
- New! In tablesorter v2.4, the
uitheme
option has changed to indicate the theme instead of an array of icons to use:- All theme class names are now contained within
$.tablesorter.themes
with the Bootstrap theme saved to$.tablesorter.themes.bootstrap
. - The themes variable allows you to modify the class names for the table, header, sort icons, active state, hover state, filter inputs and zebra striping. See the code below on how to extend these variables.
- Set the
uitheme
widgettheme
option (changed in v2.7) to"bootstrap"
to set the widget to use the Bootstrap theme. See the jQuery UI demo for another example.
- All theme class names are now contained within
- This demo shows how to get around an issue with the filter widget:
- The zebra widget button below was added to show that when bootstrap's "table-striped" class is applied, the css defined zebra striping will not apply correctly because table rows are hidden but still accounted for by the css
nth-child()
selector. - To better understand this issue, disable the zebra widget (using the toggle button). Now in the "Calculus" column.
- Try other filter searches with the zebra widget disabled, like in the "Sex" column.
- To solve this issue, just enable the zebra widget and the "even" and "odd" row class names
willshould over-ride thenth-child()
styling.
NOTE! This doesn't seem to work in the latest Bootstrap version, so you'll have to remove the "table-striped" class completely from the table. - The only down side is that for custom bootstrap themes, you'll need to edit the "theme.bootstrap.css" file for bootstrap.
- The zebra widget button below was added to show that when bootstrap's "table-striped" class is applied, the css defined zebra striping will not apply correctly because table rows are hidden but still accounted for by the css
- This demo uses HTML5 data attributes and therefore needs jQuery 1.4+.
Demo
Name |
Major |
Sex |
English |
Japanese |
Calculus |
Geometry |
---|---|---|---|---|---|---|
Name | Major | Sex | English | Japanese | Calculus | Geometry |
1 - 10 / 50 (50) | ||||||
Student01 | Languages | male | 80 | 70 | 75 | 80 |
Student02 | Mathematics | male | 90 | 88 | 100 | 90 |
Student03 | Languages | female | 85 | 95 | 80 | 85 |
Student04 | Languages | male | 60 | 55 | 100 | 100 |
Student05 | Languages | female | 68 | 80 | 95 | 80 |
Student06 | Mathematics | male | 100 | 99 | 100 | 90 |
Student07 | Mathematics | male | 85 | 68 | 90 | 90 |
Student08 | Languages | male | 100 | 90 | 90 | 85 |
Student09 | Mathematics | male | 80 | 50 | 65 | 75 |
Student10 | Languages | male | 85 | 100 | 100 | 90 |
Page Header
<!-- Bootstrap stylesheet --> <link rel="stylesheet" href="/css/bootstrap.min.css"> <!-- bootstrap widget theme --> <link rel="stylesheet" href="/tablesorter/css/theme.bootstrap.css"> <!-- tablesorter plugin --> <script src="../js/jquery.tablesorter.js"></script> <!-- tablesorter widget file - loaded after the plugin --> <script src="../js/jquery.tablesorter.widgets.js"></script>
Javascript
$(function() { $.extend($.tablesorter.themes.bootstrap, { // these classes are added to the table. To see other table classes available, // look here: http://twitter.github.com/bootstrap/base-css.html#tables table : 'table table-bordered', header : 'bootstrap-header', // give the header a gradient background footerRow : '', footerCells: '', icons : '', // add "icon-white" to make them white; this icon class is added to the <i> in the header sortNone : 'bootstrap-icon-unsorted', sortAsc : 'icon-chevron-up', sortDesc : 'icon-chevron-down', active : '', // applied when column is sorted hover : '', // use custom css here - bootstrap class may not override it filterRow : '', // filter row class even : '', // odd row zebra striping odd : '' // even row zebra striping }); // call the tablesorter plugin and apply the uitheme widget $("table").tablesorter({ theme : "bootstrap", // this will widthFixed: true, headerTemplate : '{content} {icon}', // new in v2.7. Needed to add the bootstrap icon! // widget code contained in the jquery.tablesorter.widgets.js file // use the zebra stripe widget if you plan on hiding any rows (filter widget) widgets : [ "uitheme", "filter", "zebra" ], widgetOptions : { // using the default zebra striping class name, so it actually isn't included in the theme variable above // this is ONLY needed for bootstrap theming if you are using the filter widget, because rows are hidden zebra : ["even", "odd"], // reset filters button filter_reset : ".reset", // set the uitheme widget to use the bootstrap theme class names // uitheme : "bootstrap" } }) .tablesorterPager({ // target the pager markup - see the HTML block below container: $(".pager"), // target the pager page select dropdown - choose a page cssGoto : ".pagenum", // remove rows from the table to speed up the sort of large tables. // setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled. removeRows: false, // output string - default is '{page}/{totalPages}'; // possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows} output: '{startRow} - {endRow} / {filteredRows} ({totalRows})' }); });