DT_bootstrap.js 4.3 KB

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