1 | /* Set the defaults for DataTables initialisation */ |
---|
2 | $.extend( true, $.fn.dataTable.defaults, { |
---|
3 | "sDom": |
---|
4 | "<'row'<'col-xs-6'l><'col-xs-6'f>r>"+ |
---|
5 | "t"+ |
---|
6 | "<'row'<'col-xs-6'i><'col-xs-6'p>>", |
---|
7 | "oLanguage": { |
---|
8 | "sLengthMenu": "_MENU_ records per page" |
---|
9 | } |
---|
10 | } ); |
---|
11 | |
---|
12 | |
---|
13 | /* Default class modification */ |
---|
14 | $.extend( $.fn.dataTableExt.oStdClasses, { |
---|
15 | "sWrapper": "dataTables_wrapper form-inline", |
---|
16 | "sFilterInput": "form-control input-sm", |
---|
17 | "sLengthSelect": "form-control input-sm" |
---|
18 | } ); |
---|
19 | |
---|
20 | // In 1.10 we use the pagination renderers to draw the Bootstrap paging, |
---|
21 | // rather than custom plug-in |
---|
22 | if ( $.fn.dataTable.Api ) { |
---|
23 | $.fn.dataTable.defaults.renderer = 'bootstrap'; |
---|
24 | $.fn.dataTable.ext.renderer.pageButton.bootstrap = function ( settings, host, idx, buttons, page, pages ) { |
---|
25 | var api = new $.fn.dataTable.Api( settings ); |
---|
26 | var classes = settings.oClasses; |
---|
27 | var lang = settings.oLanguage.oPaginate; |
---|
28 | var btnDisplay, btnClass; |
---|
29 | |
---|
30 | var attach = function( container, buttons ) { |
---|
31 | var i, ien, node, button; |
---|
32 | var clickHandler = function ( e ) { |
---|
33 | e.preventDefault(); |
---|
34 | if ( e.data.action !== 'ellipsis' ) { |
---|
35 | api.page( e.data.action ).draw( false ); |
---|
36 | } |
---|
37 | }; |
---|
38 | |
---|
39 | for ( i=0, ien=buttons.length ; i<ien ; i++ ) { |
---|
40 | button = buttons[i]; |
---|
41 | |
---|
42 | if ( $.isArray( button ) ) { |
---|
43 | attach( container, button ); |
---|
44 | } |
---|
45 | else { |
---|
46 | btnDisplay = ''; |
---|
47 | btnClass = ''; |
---|
48 | |
---|
49 | switch ( button ) { |
---|
50 | case 'ellipsis': |
---|
51 | btnDisplay = '…'; |
---|
52 | btnClass = 'disabled'; |
---|
53 | break; |
---|
54 | |
---|
55 | case 'first': |
---|
56 | btnDisplay = lang.sFirst; |
---|
57 | btnClass = button + (page > 0 ? |
---|
58 | '' : ' disabled'); |
---|
59 | break; |
---|
60 | |
---|
61 | case 'previous': |
---|
62 | btnDisplay = lang.sPrevious; |
---|
63 | btnClass = button + (page > 0 ? |
---|
64 | '' : ' disabled'); |
---|
65 | break; |
---|
66 | |
---|
67 | case 'next': |
---|
68 | btnDisplay = lang.sNext; |
---|
69 | btnClass = button + (page < pages-1 ? |
---|
70 | '' : ' disabled'); |
---|
71 | break; |
---|
72 | |
---|
73 | case 'last': |
---|
74 | btnDisplay = lang.sLast; |
---|
75 | btnClass = button + (page < pages-1 ? |
---|
76 | '' : ' disabled'); |
---|
77 | break; |
---|
78 | |
---|
79 | default: |
---|
80 | btnDisplay = button + 1; |
---|
81 | btnClass = page === button ? |
---|
82 | 'active' : ''; |
---|
83 | break; |
---|
84 | } |
---|
85 | |
---|
86 | if ( btnDisplay ) { |
---|
87 | node = $('<li>', { |
---|
88 | 'class': classes.sPageButton+' '+btnClass, |
---|
89 | 'aria-controls': settings.sTableId, |
---|
90 | 'tabindex': settings.iTabIndex, |
---|
91 | 'id': idx === 0 && typeof button === 'string' ? |
---|
92 | settings.sTableId +'_'+ button : |
---|
93 | null |
---|
94 | } ) |
---|
95 | .append( $('<a>', { |
---|
96 | 'href': '#' |
---|
97 | } ) |
---|
98 | .html( btnDisplay ) |
---|
99 | ) |
---|
100 | .appendTo( container ); |
---|
101 | |
---|
102 | settings.oApi._fnBindAction( |
---|
103 | node, {action: button}, clickHandler |
---|
104 | ); |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | }; |
---|
109 | |
---|
110 | attach( |
---|
111 | $(host).empty().html('<ul class="pagination"/>').children('ul'), |
---|
112 | buttons |
---|
113 | ); |
---|
114 | } |
---|
115 | } |
---|
116 | else { |
---|
117 | // Integration for 1.9- |
---|
118 | $.fn.dataTable.defaults.sPaginationType = 'bootstrap'; |
---|
119 | |
---|
120 | /* API method to get paging information */ |
---|
121 | $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings ) |
---|
122 | { |
---|
123 | return { |
---|
124 | "iStart": oSettings._iDisplayStart, |
---|
125 | "iEnd": oSettings.fnDisplayEnd(), |
---|
126 | "iLength": oSettings._iDisplayLength, |
---|
127 | "iTotal": oSettings.fnRecordsTotal(), |
---|
128 | "iFilteredTotal": oSettings.fnRecordsDisplay(), |
---|
129 | "iPage": oSettings._iDisplayLength === -1 ? |
---|
130 | 0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ), |
---|
131 | "iTotalPages": oSettings._iDisplayLength === -1 ? |
---|
132 | 0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength ) |
---|
133 | }; |
---|
134 | }; |
---|
135 | |
---|
136 | /* Bootstrap style pagination control */ |
---|
137 | $.extend( $.fn.dataTableExt.oPagination, { |
---|
138 | "bootstrap": { |
---|
139 | "fnInit": function( oSettings, nPaging, fnDraw ) { |
---|
140 | var oLang = oSettings.oLanguage.oPaginate; |
---|
141 | var fnClickHandler = function ( e ) { |
---|
142 | e.preventDefault(); |
---|
143 | if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) { |
---|
144 | fnDraw( oSettings ); |
---|
145 | } |
---|
146 | }; |
---|
147 | |
---|
148 | $(nPaging).append( |
---|
149 | '<ul class="pagination">'+ |
---|
150 | '<li class="prev disabled"><a href="#">← '+oLang.sPrevious+'</a></li>'+ |
---|
151 | '<li class="next disabled"><a href="#">'+oLang.sNext+' → </a></li>'+ |
---|
152 | '</ul>' |
---|
153 | ); |
---|
154 | var els = $('a', nPaging); |
---|
155 | $(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler ); |
---|
156 | $(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler ); |
---|
157 | }, |
---|
158 | |
---|
159 | "fnUpdate": function ( oSettings, fnDraw ) { |
---|
160 | var iListLength = 5; |
---|
161 | var oPaging = oSettings.oInstance.fnPagingInfo(); |
---|
162 | var an = oSettings.aanFeatures.p; |
---|
163 | var i, ien, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2); |
---|
164 | |
---|
165 | if ( oPaging.iTotalPages < iListLength) { |
---|
166 | iStart = 1; |
---|
167 | iEnd = oPaging.iTotalPages; |
---|
168 | } |
---|
169 | else if ( oPaging.iPage <= iHalf ) { |
---|
170 | iStart = 1; |
---|
171 | iEnd = iListLength; |
---|
172 | } else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) { |
---|
173 | iStart = oPaging.iTotalPages - iListLength + 1; |
---|
174 | iEnd = oPaging.iTotalPages; |
---|
175 | } else { |
---|
176 | iStart = oPaging.iPage - iHalf + 1; |
---|
177 | iEnd = iStart + iListLength - 1; |
---|
178 | } |
---|
179 | |
---|
180 | for ( i=0, ien=an.length ; i<ien ; i++ ) { |
---|
181 | // Remove the middle elements |
---|
182 | $('li:gt(0)', an[i]).filter(':not(:last)').remove(); |
---|
183 | |
---|
184 | // Add the new list items and their event handlers |
---|
185 | for ( j=iStart ; j<=iEnd ; j++ ) { |
---|
186 | sClass = (j==oPaging.iPage+1) ? 'class="active"' : ''; |
---|
187 | $('<li '+sClass+'><a href="#">'+j+'</a></li>') |
---|
188 | .insertBefore( $('li:last', an[i])[0] ) |
---|
189 | .bind('click', function (e) { |
---|
190 | e.preventDefault(); |
---|
191 | oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength; |
---|
192 | fnDraw( oSettings ); |
---|
193 | } ); |
---|
194 | } |
---|
195 | |
---|
196 | // Add / remove disabled classes from the static elements |
---|
197 | if ( oPaging.iPage === 0 ) { |
---|
198 | $('li:first', an[i]).addClass('disabled'); |
---|
199 | } else { |
---|
200 | $('li:first', an[i]).removeClass('disabled'); |
---|
201 | } |
---|
202 | |
---|
203 | if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) { |
---|
204 | $('li:last', an[i]).addClass('disabled'); |
---|
205 | } else { |
---|
206 | $('li:last', an[i]).removeClass('disabled'); |
---|
207 | } |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | } ); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /* |
---|
216 | * TableTools Bootstrap compatibility |
---|
217 | * Required TableTools 2.1+ |
---|
218 | */ |
---|
219 | if ( $.fn.DataTable.TableTools ) { |
---|
220 | // Set the classes that TableTools uses to something suitable for Bootstrap |
---|
221 | $.extend( true, $.fn.DataTable.TableTools.classes, { |
---|
222 | "container": "DTTT btn-group", |
---|
223 | "buttons": { |
---|
224 | "normal": "btn btn-default", |
---|
225 | "disabled": "disabled" |
---|
226 | }, |
---|
227 | "collection": { |
---|
228 | "container": "DTTT_dropdown dropdown-menu", |
---|
229 | "buttons": { |
---|
230 | "normal": "", |
---|
231 | "disabled": "disabled" |
---|
232 | } |
---|
233 | }, |
---|
234 | "print": { |
---|
235 | "info": "DTTT_print_info modal" |
---|
236 | }, |
---|
237 | "select": { |
---|
238 | "row": "active" |
---|
239 | } |
---|
240 | } ); |
---|
241 | |
---|
242 | // Have the collection use a bootstrap compatible dropdown |
---|
243 | $.extend( true, $.fn.DataTable.TableTools.DEFAULTS.oTags, { |
---|
244 | "collection": { |
---|
245 | "container": "ul", |
---|
246 | "button": "li", |
---|
247 | "liner": "a" |
---|
248 | } |
---|
249 | } ); |
---|
250 | } |
---|
251 | |
---|