dataTables.foundation.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Set the defaults for DataTables initialisation */
  2. $.extend( true, $.fn.dataTable.defaults, {
  3. "sDom":
  4. "<'row'<'large-6 columns'l><'large-6 columns'f>r>"+
  5. "t"+
  6. "<'row'<'large-6 columns'i><'large-6 columns'p>>",
  7. "sPaginationType": "bootstrap",
  8. "oLanguage": {
  9. "sLengthMenu": "_MENU_ records per page"
  10. }
  11. } );
  12. /* API method to get paging information */
  13. $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
  14. {
  15. return {
  16. "iStart": oSettings._iDisplayStart,
  17. "iEnd": oSettings.fnDisplayEnd(),
  18. "iLength": oSettings._iDisplayLength,
  19. "iTotal": oSettings.fnRecordsTotal(),
  20. "iFilteredTotal": oSettings.fnRecordsDisplay(),
  21. "iPage": oSettings._iDisplayLength === -1 ?
  22. 0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
  23. "iTotalPages": oSettings._iDisplayLength === -1 ?
  24. 0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
  25. };
  26. };
  27. /* Bootstrap style pagination control */
  28. $.extend( $.fn.dataTableExt.oPagination, {
  29. "bootstrap": {
  30. "fnInit": function( oSettings, nPaging, fnDraw ) {
  31. var oLang = oSettings.oLanguage.oPaginate;
  32. var fnClickHandler = function ( e ) {
  33. e.preventDefault();
  34. if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
  35. fnDraw( oSettings );
  36. }
  37. };
  38. $(nPaging).append(
  39. '<ul class="pagination">'+
  40. '<li class="prev arrow unavailable"><a href="">&laquo;</a></li>'+
  41. '<li class="next arrow unavailable"><a href="">&raquo;</a></li>'+
  42. '</ul>'
  43. );
  44. var els = $('a', nPaging);
  45. $(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler );
  46. $(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler );
  47. },
  48. "fnUpdate": function ( oSettings, fnDraw ) {
  49. var iListLength = 5;
  50. var oPaging = oSettings.oInstance.fnPagingInfo();
  51. var an = oSettings.aanFeatures.p;
  52. var pages = [];
  53. var i, ien, klass, host;
  54. // This could use some improving - however, see
  55. // https://github.com/DataTables/DataTables/issues/163 - this will
  56. // be changing in the near future, so not much point in doing too
  57. // much just now
  58. if ( oPaging.iTotalPages <= 6 ) {
  59. for ( i=0 ; i<oPaging.iTotalPages ; i++ ) {
  60. pages.push( i );
  61. }
  62. }
  63. else {
  64. // Current page
  65. pages.push( oPaging.iPage );
  66. // After current page
  67. var pagesAfter = oPaging.iPage + 2 >= oPaging.iTotalPages ?
  68. oPaging.iTotalPages :
  69. oPaging.iPage + 2;
  70. for ( i=oPaging.iPage+1 ; i<pagesAfter ; i++ ) {
  71. pages.push( i );
  72. }
  73. // After gap
  74. if ( pagesAfter < oPaging.iTotalPages-2 ) {
  75. pages.push( null );
  76. }
  77. // End
  78. if ( $.inArray( oPaging.iTotalPages-2, pages ) === -1 && oPaging.iPage < oPaging.iTotalPages-2 ) {
  79. pages.push( oPaging.iTotalPages-2 );
  80. }
  81. if ( $.inArray( oPaging.iTotalPages-1, pages ) === -1 ) {
  82. pages.push( oPaging.iTotalPages-1 );
  83. }
  84. // Pages before
  85. var pagesBefore = oPaging.iPage - 2 > 0 ?
  86. oPaging.iPage - 2 :
  87. 0;
  88. for ( i=oPaging.iPage-1 ; i>pagesBefore ; i-- ) {
  89. pages.unshift( i );
  90. }
  91. // Before gap
  92. if ( pagesBefore > 1 ) {
  93. pages.unshift( null );
  94. }
  95. // Start
  96. if ( $.inArray( 1, pages ) === -1 && oPaging.iTotalPages > 1 ) {
  97. pages.unshift( 1 );
  98. }
  99. if ( $.inArray( 0, pages ) === -1 ) {
  100. pages.unshift( 0 );
  101. }
  102. }
  103. for ( i=0, ien=an.length ; i<ien ; i++ ) {
  104. // Remove the middle elements
  105. host = an[i];
  106. $('li:gt(0)', host).filter(':not(:last)').remove();
  107. // Add the new list items and their event handlers
  108. $.each( pages, function( i, page ) {
  109. klass = page === null ? 'unavailable' :
  110. page === oPaging.iPage ? 'current' : '';
  111. $('<li class="'+klass+'"><a href="">'+(page===null? '&hellip;' : page+1)+'</a></li>')
  112. .insertBefore( $('li:last', host) )
  113. .bind('click', function (e) {
  114. e.preventDefault();
  115. oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
  116. fnDraw( oSettings );
  117. } );
  118. } );
  119. // Add / remove disabled classes from the static elements
  120. if ( oPaging.iPage === 0 ) {
  121. $('li:first', host).addClass('unavailable');
  122. } else {
  123. $('li:first', host).removeClass('unavailable');
  124. }
  125. if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
  126. $('li:last', host).addClass('unavailable');
  127. } else {
  128. $('li:last', host).removeClass('unavailable');
  129. }
  130. }
  131. }
  132. }
  133. } );
  134. /*
  135. * TableTools Foundation compatibility
  136. * Required TableTools 2.1+
  137. */
  138. if ( $.fn.DataTable.TableTools ) {
  139. // Set the classes that TableTools uses to something suitable for Foundation
  140. $.extend( true, $.fn.DataTable.TableTools.classes, {
  141. "container": "DTTT button-group",
  142. "buttons": {
  143. "normal": "button",
  144. "disabled": "disabled"
  145. },
  146. "collection": {
  147. "container": "DTTT_dropdown dropdown-menu",
  148. "buttons": {
  149. "normal": "",
  150. "disabled": "disabled"
  151. }
  152. },
  153. "select": {
  154. "row": "active"
  155. }
  156. } );
  157. // Have the collection use a bootstrap compatible dropdown
  158. $.extend( true, $.fn.DataTable.TableTools.DEFAULTS.oTags, {
  159. "collection": {
  160. "container": "ul",
  161. "button": "li",
  162. "liner": "a"
  163. }
  164. } );
  165. }