DT_bootstrap.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. }
  8. } );
  9. /* Default class modification */
  10. $.extend( $.fn.dataTableExt.oStdClasses, {
  11. "sWrapper": "dataTables_wrapper form-inline"
  12. } );
  13. /* API method to get paging information */
  14. $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
  15. {
  16. return {
  17. "iStart": oSettings._iDisplayStart,
  18. "iEnd": oSettings.fnDisplayEnd(),
  19. "iLength": oSettings._iDisplayLength,
  20. "iTotal": oSettings.fnRecordsTotal(),
  21. "iFilteredTotal": oSettings.fnRecordsDisplay(),
  22. "iPage": oSettings._iDisplayLength === -1 ?
  23. 0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
  24. "iTotalPages": oSettings._iDisplayLength === -1 ?
  25. 0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
  26. };
  27. };
  28. /* Bootstrap style pagination control */
  29. $.extend( $.fn.dataTableExt.oPagination, {
  30. "bootstrap": {
  31. "fnInit": function( oSettings, nPaging, fnDraw ) {
  32. var oLang = oSettings.oLanguage.oPaginate;
  33. var fnClickHandler = function ( e ) {
  34. e.preventDefault();
  35. if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
  36. fnDraw( oSettings );
  37. }
  38. };
  39. $(nPaging).addClass('pagination').append(
  40. '<ul>'+
  41. '<li class="prev disabled"><a href="#">&larr; '+oLang.sPrevious+'</a></li>'+
  42. '<li class="next disabled"><a href="#">'+oLang.sNext+' &rarr; </a></li>'+
  43. '</ul>'
  44. );
  45. var els = $('a', nPaging);
  46. $(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler );
  47. $(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler );
  48. },
  49. "fnUpdate": function ( oSettings, fnDraw ) {
  50. var iListLength = 5;
  51. var oPaging = oSettings.oInstance.fnPagingInfo();
  52. var an = oSettings.aanFeatures.p;
  53. var i, ien, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2);
  54. if ( oPaging.iTotalPages < iListLength) {
  55. iStart = 1;
  56. iEnd = oPaging.iTotalPages;
  57. }
  58. else if ( oPaging.iPage <= iHalf ) {
  59. iStart = 1;
  60. iEnd = iListLength;
  61. } else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) {
  62. iStart = oPaging.iTotalPages - iListLength + 1;
  63. iEnd = oPaging.iTotalPages;
  64. } else {
  65. iStart = oPaging.iPage - iHalf + 1;
  66. iEnd = iStart + iListLength - 1;
  67. }
  68. for ( i=0, ien=an.length ; i<ien ; i++ ) {
  69. // Remove the middle elements
  70. $('li:gt(0)', an[i]).filter(':not(:last)').remove();
  71. // Add the new list items and their event handlers
  72. for ( j=iStart ; j<=iEnd ; j++ ) {
  73. sClass = (j==oPaging.iPage+1) ? 'class="active"' : '';
  74. $('<li '+sClass+'><a href="#">'+j+'</a></li>')
  75. .insertBefore( $('li:last', an[i])[0] )
  76. .bind('click', function (e) {
  77. e.preventDefault();
  78. oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
  79. fnDraw( oSettings );
  80. } );
  81. }
  82. // Add / remove disabled classes from the static elements
  83. if ( oPaging.iPage === 0 ) {
  84. $('li:first', an[i]).addClass('disabled');
  85. } else {
  86. $('li:first', an[i]).removeClass('disabled');
  87. }
  88. if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
  89. $('li:last', an[i]).addClass('disabled');
  90. } else {
  91. $('li:last', an[i]).removeClass('disabled');
  92. }
  93. }
  94. }
  95. }
  96. } );
  97. /*
  98. * TableTools Bootstrap compatibility
  99. * Required TableTools 2.1+
  100. */
  101. if ( $.fn.DataTable.TableTools ) {
  102. // Set the classes that TableTools uses to something suitable for Bootstrap
  103. $.extend( true, $.fn.DataTable.TableTools.classes, {
  104. "container": "DTTT btn-group",
  105. "buttons": {
  106. "normal": "btn",
  107. "disabled": "disabled"
  108. },
  109. "collection": {
  110. "container": "DTTT_dropdown dropdown-menu",
  111. "buttons": {
  112. "normal": "",
  113. "disabled": "disabled"
  114. }
  115. },
  116. "print": {
  117. "info": "DTTT_print_info modal"
  118. },
  119. "select": {
  120. "row": "active"
  121. }
  122. } );
  123. // Have the collection use a bootstrap compatible dropdown
  124. $.extend( true, $.fn.DataTable.TableTools.DEFAULTS.oTags, {
  125. "collection": {
  126. "container": "ul",
  127. "button": "li",
  128. "liner": "a"
  129. }
  130. } );
  131. }
  132. /* Table initialisation */
  133. $(document).ready(function() {
  134. $('#example').dataTable( {
  135. "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
  136. "sPaginationType": "bootstrap",
  137. "oLanguage": {
  138. "sLengthMenu": "_MENU_ records per page"
  139. }
  140. } );
  141. } );