1- /* Set the defaults for DataTables initialisation */
2- $ . extend ( true , $ . fn . dataTable . defaults , {
3- "sDom" : "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>" ,
4- "sPaginationType" : "bootstrap" ,
5- "oLanguage" : {
6- "sLengthMenu" : "_MENU_ records per page"
7- }
1+ /*! DataTables Bootstrap 2 integration
2+ * ©2011-2014 SpryMedia Ltd - datatables.net/license
3+ */
4+
5+ /**
6+ * DataTables integration for Bootstrap 2. This requires Bootstrap 2 and
7+ * DataTables 1.10 or newer.
8+ *
9+ * This file sets the defaults and adds options to DataTables to style its
10+ * controls using Bootstrap. See http://datatables.net/manual/styling/bootstrap
11+ * for further information.
12+ */
13+ ( function ( window , document , $ , DataTable , undefined ) {
14+
15+ $ . extend ( true , DataTable . defaults , {
16+ "dom" :
17+ "<'row-fluid'<'span6'l><'span6'f>r>" +
18+ "<'row-fluid'<'span12't>>" +
19+ "<'row-fluid'<'span6'i><'span6'p>>" ,
20+ renderer : 'bootstrap'
821} ) ;
922
1023
1124/* Default class modification */
12- $ . extend ( $ . fn . dataTableExt . oStdClasses , {
13- " sWrapper" : "dataTables_wrapper form-inline"
25+ $ . extend ( DataTable . ext . classes , {
26+ sWrapper : "dataTables_wrapper form-inline dt-bootstrap "
1427} ) ;
1528
1629
17- /* API method to get paging information */
18- $ . fn . dataTableExt . oApi . fnPagingInfo = function ( oSettings )
19- {
20- return {
21- "iStart" : oSettings . _iDisplayStart ,
22- "iEnd" : oSettings . fnDisplayEnd ( ) ,
23- "iLength" : oSettings . _iDisplayLength ,
24- "iTotal" : oSettings . fnRecordsTotal ( ) ,
25- "iFilteredTotal" : oSettings . fnRecordsDisplay ( ) ,
26- "iPage" : oSettings . _iDisplayLength === - 1 ?
27- 0 : Math . ceil ( oSettings . _iDisplayStart / oSettings . _iDisplayLength ) ,
28- "iTotalPages" : oSettings . _iDisplayLength === - 1 ?
29- 0 : Math . ceil ( oSettings . fnRecordsDisplay ( ) / oSettings . _iDisplayLength )
30- } ;
31- } ;
32-
33-
34- /* Bootstrap style pagination control */
35- $ . extend ( $ . fn . dataTableExt . oPagination , {
36- "bootstrap" : {
37- "fnInit" : function ( oSettings , nPaging , fnDraw ) {
38- var oLang = oSettings . oLanguage . oPaginate ;
39- var fnClickHandler = function ( e ) {
40- e . preventDefault ( ) ;
41- if ( oSettings . oApi . _fnPageChange ( oSettings , e . data . action ) ) {
42- fnDraw ( oSettings ) ;
43- }
44- } ;
45-
46- $ ( nPaging ) . addClass ( 'pagination' ) . append (
47- '<ul>' +
48- '<li class="prev disabled"><a href="#">← ' + oLang . sPrevious + '</a></li>' +
49- '<li class="next disabled"><a href="#">' + oLang . sNext + ' → </a></li>' +
50- '</ul>'
51- ) ;
52- var els = $ ( 'a' , nPaging ) ;
53- $ ( els [ 0 ] ) . bind ( 'click.DT' , { action : "previous" } , fnClickHandler ) ;
54- $ ( els [ 1 ] ) . bind ( 'click.DT' , { action : "next" } , fnClickHandler ) ;
55- } ,
56-
57- "fnUpdate" : function ( oSettings , fnDraw ) {
58- var iListLength = 5 ;
59- var oPaging = oSettings . oInstance . fnPagingInfo ( ) ;
60- var an = oSettings . aanFeatures . p ;
61- var i , ien , j , sClass , iStart , iEnd , iHalf = Math . floor ( iListLength / 2 ) ;
30+ /* Bootstrap paging button renderer */
31+ DataTable . ext . renderer . pageButton . bootstrap = function ( settings , host , idx , buttons , page , pages ) {
32+ var api = new DataTable . Api ( settings ) ;
33+ var classes = settings . oClasses ;
34+ var lang = settings . oLanguage . oPaginate ;
35+ var btnDisplay , btnClass ;
6236
63- if ( oPaging . iTotalPages < iListLength ) {
64- iStart = 1 ;
65- iEnd = oPaging . iTotalPages ;
66- }
67- else if ( oPaging . iPage <= iHalf ) {
68- iStart = 1 ;
69- iEnd = iListLength ;
70- } else if ( oPaging . iPage >= ( oPaging . iTotalPages - iHalf ) ) {
71- iStart = oPaging . iTotalPages - iListLength + 1 ;
72- iEnd = oPaging . iTotalPages ;
73- } else {
74- iStart = oPaging . iPage - iHalf + 1 ;
75- iEnd = iStart + iListLength - 1 ;
37+ var attach = function ( container , buttons ) {
38+ var i , ien , node , button ;
39+ var clickHandler = function ( e ) {
40+ e . preventDefault ( ) ;
41+ if ( ! $ ( e . currentTarget ) . hasClass ( 'disabled' ) ) {
42+ api . page ( e . data . action ) . draw ( false ) ;
7643 }
44+ } ;
7745
78- for ( i = 0 , ien = an . length ; i < ien ; i ++ ) {
79- // Remove the middle elements
80- $ ( 'li:gt(0)' , an [ i ] ) . filter ( ':not(:last)' ) . remove ( ) ;
81-
82- // Add the new list items and their event handlers
83- for ( j = iStart ; j <= iEnd ; j ++ ) {
84- sClass = ( j == oPaging . iPage + 1 ) ? 'class="active"' : '' ;
85- $ ( '<li ' + sClass + '><a href="#">' + j + '</a></li>' )
86- . insertBefore ( $ ( 'li:last' , an [ i ] ) [ 0 ] )
87- . bind ( 'click' , function ( e ) {
88- e . preventDefault ( ) ;
89- oSettings . _iDisplayStart = ( parseInt ( $ ( 'a' , this ) . text ( ) , 10 ) - 1 ) * oPaging . iLength ;
90- fnDraw ( oSettings ) ;
91- } ) ;
92- }
46+ for ( i = 0 , ien = buttons . length ; i < ien ; i ++ ) {
47+ button = buttons [ i ] ;
9348
94- // Add / remove disabled classes from the static elements
95- if ( oPaging . iPage === 0 ) {
96- $ ( 'li:first' , an [ i ] ) . addClass ( 'disabled' ) ;
97- } else {
98- $ ( 'li:first' , an [ i ] ) . removeClass ( 'disabled' ) ;
49+ if ( $ . isArray ( button ) ) {
50+ attach ( container , button ) ;
51+ }
52+ else {
53+ btnDisplay = '' ;
54+ btnClass = '' ;
55+
56+ switch ( button ) {
57+ case 'ellipsis' :
58+ btnDisplay = '…' ;
59+ btnClass = 'disabled' ;
60+ break ;
61+
62+ case 'first' :
63+ btnDisplay = lang . sFirst ;
64+ btnClass = button + ( page > 0 ?
65+ '' : ' disabled' ) ;
66+ break ;
67+
68+ case 'previous' :
69+ btnDisplay = lang . sPrevious ;
70+ btnClass = button + ( page > 0 ?
71+ '' : ' disabled' ) ;
72+ break ;
73+
74+ case 'next' :
75+ btnDisplay = lang . sNext ;
76+ btnClass = button + ( page < pages - 1 ?
77+ '' : ' disabled' ) ;
78+ break ;
79+
80+ case 'last' :
81+ btnDisplay = lang . sLast ;
82+ btnClass = button + ( page < pages - 1 ?
83+ '' : ' disabled' ) ;
84+ break ;
85+
86+ default :
87+ btnDisplay = button + 1 ;
88+ btnClass = page === button ?
89+ 'active' : '' ;
90+ break ;
9991 }
10092
101- if ( oPaging . iPage === oPaging . iTotalPages - 1 || oPaging . iTotalPages === 0 ) {
102- $ ( 'li:last' , an [ i ] ) . addClass ( 'disabled' ) ;
103- } else {
104- $ ( 'li:last' , an [ i ] ) . removeClass ( 'disabled' ) ;
93+ if ( btnDisplay ) {
94+ node = $ ( '<li>' , {
95+ 'class' : classes . sPageButton + ' ' + btnClass ,
96+ 'aria-controls' : settings . sTableId ,
97+ 'tabindex' : settings . iTabIndex ,
98+ 'id' : idx === 0 && typeof button === 'string' ?
99+ settings . sTableId + '_' + button :
100+ null
101+ } )
102+ . append ( $ ( '<a>' , {
103+ 'href' : '#'
104+ } )
105+ . html ( btnDisplay )
106+ )
107+ . appendTo ( container ) ;
108+
109+ settings . oApi . _fnBindAction (
110+ node , { action : button } , clickHandler
111+ ) ;
105112 }
106113 }
107114 }
108- }
109- } ) ;
115+ } ;
116+
117+ attach (
118+ $ ( host ) . empty ( ) . html ( '<div class="pagination"><ul/></div>' ) . find ( 'ul' ) ,
119+ buttons
120+ ) ;
121+ } ;
110122
111123
112124/*
113125 * TableTools Bootstrap compatibility
114126 * Required TableTools 2.1+
115127 */
116- if ( $ . fn . DataTable . TableTools ) {
128+ if ( DataTable . TableTools ) {
117129 // Set the classes that TableTools uses to something suitable for Bootstrap
118- $ . extend ( true , $ . fn . DataTable . TableTools . classes , {
130+ $ . extend ( true , DataTable . TableTools . classes , {
119131 "container" : "DTTT btn-group" ,
120132 "buttons" : {
121133 "normal" : "btn" ,
@@ -137,7 +149,7 @@ if ( $.fn.DataTable.TableTools ) {
137149 } ) ;
138150
139151 // Have the collection use a bootstrap compatible dropdown
140- $ . extend ( true , $ . fn . DataTable . TableTools . DEFAULTS . oTags , {
152+ $ . extend ( true , DataTable . TableTools . DEFAULTS . oTags , {
141153 "collection" : {
142154 "container" : "ul" ,
143155 "button" : "li" ,
@@ -146,3 +158,5 @@ if ( $.fn.DataTable.TableTools ) {
146158 } ) ;
147159}
148160
161+
162+ } ) ( window , document , jQuery , jQuery . fn . dataTable ) ;
0 commit comments