src/main/webapp/jquery/js/jquery.dataTables_bootstrap.js
changeset 46 3092875edd20
equal deleted inserted replaced
45:38f409912075 46:3092875edd20
       
     1 /*
       
     2  * http://datatables.net/media/blog/bootstrap_2/DT_bootstrap.js
       
     3  *
       
     4  * @copyright Copyright 2008-2012 Allan Jardine, all rights reserved.
       
     5  *
       
     6  * This source file is free software, under either the GPL v2 license or a
       
     7  * BSD style license, available at:
       
     8  *   http://datatables.net/license_gpl2
       
     9  *   http://datatables.net/license_bsd
       
    10  * 
       
    11  * This source file is distributed in the hope that it will be useful, but 
       
    12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
       
    13  * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
       
    14  * 
       
    15  * For details please refer to: http://www.datatables.net
       
    16  */
       
    17 
       
    18 /* Default class modification */
       
    19 $.extend( $.fn.dataTableExt.oStdClasses, {
       
    20 	"sWrapper": "dataTables_wrapper form-inline"
       
    21 } );
       
    22 
       
    23 /* API method to get paging information */
       
    24 $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
       
    25 {
       
    26 	return {
       
    27 		"iStart":         oSettings._iDisplayStart,
       
    28 		"iEnd":           oSettings.fnDisplayEnd(),
       
    29 		"iLength":        oSettings._iDisplayLength,
       
    30 		"iTotal":         oSettings.fnRecordsTotal(),
       
    31 		"iFilteredTotal": oSettings.fnRecordsDisplay(),
       
    32 		"iPage":          Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
       
    33 		"iTotalPages":    Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
       
    34 	};
       
    35 }
       
    36 
       
    37 /* Bootstrap style pagination control */
       
    38 $.extend( $.fn.dataTableExt.oPagination, {
       
    39 	"bootstrap": {
       
    40 		"fnInit": function( oSettings, nPaging, fnDraw ) {
       
    41 			var oLang = oSettings.oLanguage.oPaginate;
       
    42 			var fnClickHandler = function ( e ) {
       
    43 				e.preventDefault();
       
    44 				if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
       
    45 					fnDraw( oSettings );
       
    46 				}
       
    47 			};
       
    48 
       
    49 			$(nPaging).addClass('pagination').append(
       
    50 				'<ul>'+
       
    51 					'<li class="prev disabled"><a href="#">&larr; '+oLang.sPrevious+'</a></li>'+
       
    52 					'<li class="next disabled"><a href="#">'+oLang.sNext+' &rarr; </a></li>'+
       
    53 				'</ul>'
       
    54 			);
       
    55 			var els = $('a', nPaging);
       
    56 			$(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler );
       
    57 			$(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler );
       
    58 		},
       
    59 
       
    60 		"fnUpdate": function ( oSettings, fnDraw ) {
       
    61 			var iListLength = 5;
       
    62 			var oPaging = oSettings.oInstance.fnPagingInfo();
       
    63 			var an = oSettings.aanFeatures.p;
       
    64 			var i, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2);
       
    65 
       
    66 			if ( oPaging.iTotalPages < iListLength) {
       
    67 				iStart = 1;
       
    68 				iEnd = oPaging.iTotalPages;
       
    69 			}
       
    70 			else if ( oPaging.iPage <= iHalf ) {
       
    71 				iStart = 1;
       
    72 				iEnd = iListLength;
       
    73 			} else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) {
       
    74 				iStart = oPaging.iTotalPages - iListLength + 1;
       
    75 				iEnd = oPaging.iTotalPages;
       
    76 			} else {
       
    77 				iStart = oPaging.iPage - iHalf + 1;
       
    78 				iEnd = iStart + iListLength - 1;
       
    79 			}
       
    80 
       
    81 			for ( i=0, iLen=an.length ; i<iLen ; i++ ) {
       
    82 				// Remove the middle elements
       
    83 				$('li:gt(0)', an[i]).filter(':not(:last)').remove();
       
    84 
       
    85 				// Add the new list items and their event handlers
       
    86 				for ( j=iStart ; j<=iEnd ; j++ ) {
       
    87 					sClass = (j==oPaging.iPage+1) ? 'class="active"' : '';
       
    88 					$('<li '+sClass+'><a href="#">'+j+'</a></li>')
       
    89 						.insertBefore( $('li:last', an[i])[0] )
       
    90 						.bind('click', function (e) {
       
    91 							e.preventDefault();
       
    92 							oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
       
    93 							fnDraw( oSettings );
       
    94 						} );
       
    95 				}
       
    96 
       
    97 				// Add / remove disabled classes from the static elements
       
    98 				if ( oPaging.iPage === 0 ) {
       
    99 					$('li:first', an[i]).addClass('disabled');
       
   100 				} else {
       
   101 					$('li:first', an[i]).removeClass('disabled');
       
   102 				}
       
   103 
       
   104 				if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
       
   105 					$('li:last', an[i]).addClass('disabled');
       
   106 				} else {
       
   107 					$('li:last', an[i]).removeClass('disabled');
       
   108 				}
       
   109 			}
       
   110 		}
       
   111 	}
       
   112 } );
       
   113 
       
   114 /* Table initialisation */
       
   115 $(document).ready(function() {
       
   116 	$('#example').dataTable( {
       
   117 		"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
       
   118 		"sPaginationType": "bootstrap",
       
   119 		"oLanguage": {
       
   120 			"sLengthMenu": "_MENU_ records per page"
       
   121 		}
       
   122 	} );
       
   123 } );