Skip to content
Browse files

Add Twitter Bootstrap 2 support

  • Loading branch information...
1 parent 0d13aa6 commit 07dc58401295ce86a17432e3dec479ced7e3d06d @ZenCocoon ZenCocoon committed Apr 18, 2012
View
36 Readme.md
@@ -7,12 +7,40 @@ It provides all the basic DataTables files, but not (yet) the extras.
## Installation
1. Add `gem 'jquery-datatables-rails'` to the assets group in your Gemfile and run `bundle install`.
-1. Add the JavaScript to application.js:
+1. Add the JavaScript to `application.js`:
- `//= require dataTables/jquery.dataTables`
-1. Add the stylesheets to application.css:
+`//= require dataTables/jquery.dataTables`
- `*= require dataTables/jquery.dataTables`
+1. Add the stylesheets to `application.css`:
+
+`*= require dataTables/jquery.dataTables`
+
+## Twitter Bootstrap 2 support
+
+1. Add the JavaScript to `application.js`:
+
+`//= require dataTables/jquery.dataTables.bootstrap`
+
+1. Remove the initial stylesheets from `application.css`
+1. Add the stylesheets to `application.css`:
+
+`*= require dataTables/jquery.dataTables.bootstrap`
+
+1. Initialize your datatables like this:
+
+```javascript
+// For fluid containers
+$('.datatable').dataTable({
+ "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
+ "sPaginationType": "bootstrap"
+});
+
+// For fixed width containers
+$('.datatable').dataTable({
+ "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
+ "sPaginationType": "bootstrap"
+});
+```
## Plugins
View
95 vendor/assets/javascripts/dataTables/jquery.dataTables.bootstrap.js
@@ -0,0 +1,95 @@
+/* Default class modification */
+$.extend( $.fn.dataTableExt.oStdClasses, {
+ "sWrapper": "dataTables_wrapper form-inline"
+} );
+
+/* API method to get paging information */
+$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
+{
+ return {
+ "iStart": oSettings._iDisplayStart,
+ "iEnd": oSettings.fnDisplayEnd(),
+ "iLength": oSettings._iDisplayLength,
+ "iTotal": oSettings.fnRecordsTotal(),
+ "iFilteredTotal": oSettings.fnRecordsDisplay(),
+ "iPage": Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
+ "iTotalPages": Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
+ };
+}
+
+/* Bootstrap style pagination control */
+$.extend( $.fn.dataTableExt.oPagination, {
+ "bootstrap": {
+ "fnInit": function( oSettings, nPaging, fnDraw ) {
+ var oLang = oSettings.oLanguage.oPaginate;
+ var fnClickHandler = function ( e ) {
+ e.preventDefault();
+ if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
+ fnDraw( oSettings );
+ }
+ };
+
+ $(nPaging).addClass('pagination').append(
+ '<ul>'+
+ '<li class="prev disabled"><a href="#">&larr; '+oLang.sPrevious+'</a></li>'+
+ '<li class="next disabled"><a href="#">'+oLang.sNext+' &rarr; </a></li>'+
+ '</ul>'
+ );
+ var els = $('a', nPaging);
+ $(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler );
+ $(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler );
+ },
+
+ "fnUpdate": function ( oSettings, fnDraw ) {
+ var iListLength = 5;
+ var oPaging = oSettings.oInstance.fnPagingInfo();
+ var an = oSettings.aanFeatures.p;
+ var i, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2);
+
+ if ( oPaging.iTotalPages < iListLength) {
+ iStart = 1;
+ iEnd = oPaging.iTotalPages;
+ }
+ else if ( oPaging.iPage <= iHalf ) {
+ iStart = 1;
+ iEnd = iListLength;
+ } else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) {
+ iStart = oPaging.iTotalPages - iListLength + 1;
+ iEnd = oPaging.iTotalPages;
+ } else {
+ iStart = oPaging.iPage - iHalf + 1;
+ iEnd = iStart + iListLength - 1;
+ }
+
+ for ( i=0, iLen=an.length ; i<iLen ; i++ ) {
+ // Remove the middle elements
+ $('li:gt(0)', an[i]).filter(':not(:last)').remove();
+
+ // Add the new list items and their event handlers
+ for ( j=iStart ; j<=iEnd ; j++ ) {
+ sClass = (j==oPaging.iPage+1) ? 'class="active"' : '';
+ $('<li '+sClass+'><a href="#">'+j+'</a></li>')
+ .insertBefore( $('li:last', an[i])[0] )
+ .bind('click', function (e) {
+ e.preventDefault();
+ oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
+ fnDraw( oSettings );
+ } );
+ }
+
+ // Add / remove disabled classes from the static elements
+ if ( oPaging.iPage === 0 ) {
+ $('li:first', an[i]).addClass('disabled');
+ } else {
+ $('li:first', an[i]).removeClass('disabled');
+ }
+
+ if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
+ $('li:last', an[i]).addClass('disabled');
+ } else {
+ $('li:last', an[i]).removeClass('disabled');
+ }
+ }
+ }
+ }
+} );
View
46 vendor/assets/stylesheets/dataTables/jquery.dataTables.bootstrap.css.scss
@@ -0,0 +1,46 @@
+div.dataTables_length label {
+ float: left;
+ text-align: left;
+}
+
+div.dataTables_length select {
+ width: 75px;
+}
+
+div.dataTables_filter label {
+ float: right;
+}
+
+div.dataTables_info {
+ padding-top: 8px;
+}
+
+div.dataTables_paginate {
+ float: right;
+ margin: 0;
+}
+
+table.dataTable {
+ clear: both;
+ margin-bottom: 6px !important;
+}
+
+table.dataTable thead .sorting,
+table.dataTable thead .sorting_asc,
+table.dataTable thead .sorting_desc,
+table.dataTable thead .sorting_asc_disabled,
+table.dataTable thead .sorting_desc_disabled {
+ cursor: pointer;
+ *cursor: hand;
+}
+
+table.dataTable thead .sorting { background: image-url('dataTables/sort_both.png') no-repeat center right; }
+table.dataTable thead .sorting_asc { background: image-url('dataTables/sort_asc.png') no-repeat center right; }
+table.dataTable thead .sorting_desc { background: image-url('dataTables/sort_desc.png') no-repeat center right; }
+
+table.dataTable thead .sorting_asc_disabled { background: image-url('dataTables/sort_asc_disabled.png') no-repeat center right; }
+table.dataTable thead .sorting_desc_disabled { background: image-url('dataTables/sort_desc_disabled.png') no-repeat center right; }
+
+table.dataTable th:active {
+ outline: none;
+}

0 comments on commit 07dc584

Please sign in to comment.
Something went wrong with that request. Please try again.