Bootstrap 3 - Pagination renderer for DataTables 1.10.

- 1.10 now uses 'renderers' for the header cells and pagination buttons,
  so the core logic, of what to display (i.e. what pagination buttons)
  can remain in DataTables core, and `paginationType` can be applied to
  any buttons. How those buttons are displayed is up to the renderer -
  in the case of bootstrap we want a ul list of li elements with a tags
  and appropriate classes applied. This commit adds that for Bootstrap 3
pull/40/head
Allan Jardine 11 years ago
parent 9323ccdd67
commit 7a9d5b573f

@ -1,15 +1,12 @@
/* Set the defaults for DataTables initialisation */
$.extend( true, $.fn.dataTable.defaults, {
"sDom": "<'row'<'col-xs-6'l><'col-xs-6'f>r>t<'row'<'col-xs-6'i><'col-xs-6'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
} );
/* Default class modification */
$.extend( $.fn.dataTableExt.oStdClasses, {
"sWrapper": "dataTables_wrapper form-inline",
@ -17,6 +14,102 @@ $.extend( $.fn.dataTableExt.oStdClasses, {
"sLengthSelect": "form-control input-sm"
} );
// In 1.10 we use the pagination renderers to draw the Bootstrap paging,
// rather than custom plug-in
if ( $.fn.dataTable.Api ) {
$.fn.dataTable.defaults.renderer = 'bootstrap';
$.fn.dataTable.ext.renderer.pageButton.bootstrap = function ( settings, host, idx, buttons, page, pages ) {
var classes = settings.oClasses;
var lang = settings.oLanguage.oPaginate;
var btnDisplay, btnClass;
var attach = function( container, buttons ) {
var i, ien, node, button;
var clickHandler = function ( e ) {
settings.oApi._fnPageChange( settings, e.data.action, true );
};
for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
button = buttons[i];
if ( $.isArray( button ) ) {
// var inner = $( '<'+(button.DT_el || 'div')+'/>' )
// .appendTo( container );
attach( container, button );
}
else {
btnDisplay = '';
btnClass = '';
switch ( button ) {
case 'ellipsis':
container.append('<span>&hellip;</span>');
break;
case 'first':
btnDisplay = lang.sFirst;
btnClass = button + (page > 0 ?
'' : ' disabled');
break;
case 'previous':
btnDisplay = lang.sPrevious;
btnClass = button + (page > 0 ?
'' : ' disabled');
break;
case 'next':
btnDisplay = lang.sNext;
btnClass = button + (page < pages-1 ?
'' : ' disabled');
break;
case 'last':
btnDisplay = lang.sLast;
btnClass = button + (page < pages-1 ?
'' : ' disabled');
break;
default:
btnDisplay = button + 1;
btnClass = page === button ?
'active' : '';
break;
}
if ( btnDisplay ) {
node = $('<li>', {
'class': classes.sPageButton+' '+btnClass,
'aria-controls': settings.sTableId,
'tabindex': settings.iTabIndex,
'id': idx === 0 && typeof button === 'string' ?
settings.sTableId +'_'+ button :
null
} )
.append( $('<a>', {
'href': '#'
} )
.html( btnDisplay )
)
.appendTo( container );
settings.oApi._fnBindAction(
node, {action: button}, clickHandler
);
}
}
}
};
attach(
$(host).empty().html('<ul class="pagination"/>').children('ul'),
buttons
);
}
}
else {
// Integration for 1.9-
$.fn.dataTable.defaults.sPaginationType = 'bootstrap';
/* API method to get paging information */
$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
@ -34,7 +127,6 @@ $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
};
};
/* Bootstrap style pagination control */
$.extend( $.fn.dataTableExt.oPagination, {
"bootstrap": {
@ -111,6 +203,7 @@ $.extend( $.fn.dataTableExt.oPagination, {
}
}
} );
}
/*

Loading…
Cancel
Save