2 * ©2011-2014 SpryMedia Ltd - datatables.net/license
7 * @description Virtual rendering for DataTables
9 * @file dataTables.scroller.js
10 * @author SpryMedia Ltd (www.sprymedia.co.uk)
11 * @contact www.sprymedia.co.uk/contact
12 * @copyright Copyright 2011-2014 SpryMedia Ltd.
14 * This source file is free software, available under the following license:
15 * MIT license - http://datatables.net/license/mit
17 * This source file is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
21 * For details please refer to: http://www.datatables.net
24 (function(window, document, undefined){
27 var factory = function( $, DataTable ) {
31 * Scroller is a virtual rendering plug-in for DataTables which allows large
32 * datasets to be drawn on screen every quickly. What the virtual rendering means
33 * is that only the visible portion of the table (and a bit to either side to make
34 * the scrolling smooth) is drawn, while the scrolling container gives the
35 * visual impression that the whole table is visible. This is done by making use
36 * of the pagination abilities of DataTables and moving the table around in the
37 * scrolling container DataTables adds to the page. The scrolling container is
38 * forced to the height it would be for the full table display using an extra
41 * Note that rows in the table MUST all be the same height. Information in a cell
42 * which expands on to multiple lines will cause some odd behaviour in the scrolling.
44 * Scroller is initialised by simply including the letter 'S' in the sDom for the
45 * table you want to have this feature enabled on. Note that the 'S' must come
46 * AFTER the 't' parameter in `dom`.
48 * Key features include:
49 * <ul class="limit_length">
50 * <li>Speed! The aim of Scroller for DataTables is to make rendering large data sets fast</li>
51 * <li>Full compatibility with deferred rendering in DataTables 1.9 for maximum speed</li>
52 * <li>Display millions of rows</li>
53 * <li>Integration with state saving in DataTables (scrolling position is saved)</li>
54 * <li>Easy to use</li>
60 * @param {object} oDT DataTables settings object
61 * @param {object} [oOpts={}] Configuration object for FixedColumns. Options
62 * are defined by {@link Scroller.defaults}
64 * @requires jQuery 1.7+
65 * @requires DataTables 1.9.0+
68 * $(document).ready(function() {
69 * $('#example').dataTable( {
70 * "sScrollY": "200px",
71 * "sAjaxSource": "media/dataset/large.txt",
73 * "bDeferRender": true
77 var Scroller = function ( oDTSettings, oOpts ) {
78 /* Sanity check - you just know it will happen */
79 if ( ! this instanceof Scroller )
81 alert( "Scroller warning: Scroller must be initialised with the 'new' keyword." );
85 if ( typeof oOpts == 'undefined' )
91 * Settings object which contains customisable information for the Scroller instance
94 * @extends Scroller.defaults
98 * DataTables settings object
100 * @default Passed in as first parameter to constructor
105 * Pixel location of the top of the drawn table in the viewport
112 * Pixel location of the bottom of the drawn table in the viewport
119 * Pixel location of the boundary for when the next data set should be loaded and drawn
120 * when scrolling up the way.
128 * Pixel location of the boundary for when the next data set should be loaded and drawn
129 * when scrolling down the way. Note that this is actually calculated as the offset from
138 * Auto row height or not indicator
145 * Number of rows calculated as visible in the visible viewport
152 * setTimeout reference for state saving, used when state saving is enabled in the DataTable
153 * and when the user scrolls the viewport in order to stop the cookie set taking too much
161 * setTimeout reference for the redraw, used when server-side processing is enabled in the
162 * DataTables in order to prevent DoSing the server
175 * Height of rows in the table
182 * Pixel height of the viewport
190 scrollDrawDiff: null,
194 // @todo The defaults should extend a `c` property and the internal settings
195 // only held in the `s` property. At the moment they are mixed
196 this.s = $.extend( this.s, Scroller.oDefaults, oOpts );
198 // Workaround for row height being read from height object (see above comment)
199 this.s.heights.row = this.s.rowHeight;
202 * DOM elements used by the class instance
208 "force": document.createElement('div'),
214 /* Attach the instance to the DataTables instance so it can be accessed */
215 this.s.dt.oScroller = this;
223 Scroller.prototype = /** @lends Scroller.prototype */{
224 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
226 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
229 * Calculate the pixel position from the top of the scrolling container for
231 * @param {int} iRow Row number to calculate the position of
232 * @returns {int} Pixels
234 * $(document).ready(function() {
235 * $('#example').dataTable( {
236 * "sScrollY": "200px",
237 * "sAjaxSource": "media/dataset/large.txt",
239 * "bDeferRender": true,
240 * "fnInitComplete": function (o) {
241 * // Find where row 25 is
242 * alert( o.oScroller.fnRowToPixels( 25 ) );
247 "fnRowToPixels": function ( rowIdx, intParse, virtual )
252 pixels = this._domain( 'virtualToPhysical', rowIdx * this.s.heights.row );
255 var diff = rowIdx - this.s.baseRowTop;
256 pixels = this.s.baseScrollTop + (diff * this.s.heights.row);
259 return intParse || intParse === undefined ?
260 parseInt( pixels, 10 ) :
266 * Calculate the row number that will be found at the given pixel position
269 * Please note that when the height of the full table exceeds 1 million
270 * pixels, Scroller switches into a non-linear mode for the scrollbar to fit
271 * all of the records into a finite area, but this function returns a linear
272 * value (relative to the last non-linear positioning).
273 * @param {int} iPixels Offset from top to calculate the row number of
274 * @param {int} [intParse=true] If an integer value should be returned
275 * @param {int} [virtual=false] Perform the calculations in the virtual domain
276 * @returns {int} Row index
278 * $(document).ready(function() {
279 * $('#example').dataTable( {
280 * "sScrollY": "200px",
281 * "sAjaxSource": "media/dataset/large.txt",
283 * "bDeferRender": true,
284 * "fnInitComplete": function (o) {
285 * // Find what row number is at 500px
286 * alert( o.oScroller.fnPixelsToRow( 500 ) );
291 "fnPixelsToRow": function ( pixels, intParse, virtual )
293 var diff = pixels - this.s.baseScrollTop;
295 this._domain( 'physicalToVirtual', pixels ) / this.s.heights.row :
296 ( diff / this.s.heights.row ) + this.s.baseRowTop;
298 return intParse || intParse === undefined ?
299 parseInt( row, 10 ) :
305 * Calculate the row number that will be found at the given pixel position (y-scroll)
306 * @param {int} iRow Row index to scroll to
307 * @param {bool} [bAnimate=true] Animate the transition or not
310 * $(document).ready(function() {
311 * $('#example').dataTable( {
312 * "sScrollY": "200px",
313 * "sAjaxSource": "media/dataset/large.txt",
315 * "bDeferRender": true,
316 * "fnInitComplete": function (o) {
317 * // Immediately scroll to row 1000
318 * o.oScroller.fnScrollToRow( 1000 );
322 * // Sometime later on use the following to scroll to row 500...
323 * var oSettings = $('#example').dataTable().fnSettings();
324 * oSettings.oScroller.fnScrollToRow( 500 );
327 "fnScrollToRow": function ( iRow, bAnimate )
331 var px = this.fnRowToPixels( iRow );
333 // We need to know if the table will redraw or not before doing the
334 // scroll. If it will not redraw, then we need to use the currently
335 // displayed table, and scroll with the physical pixels. Otherwise, we
336 // need to calculate the table's new position from the virtual
338 var preRows = ((this.s.displayBuffer-1)/2) * this.s.viewportRows;
339 var drawRow = iRow - preRows;
344 if ( (px > this.s.redrawBottom || px < this.s.redrawTop) && this.s.dt._iDisplayStart !== drawRow ) {
346 px = this.fnRowToPixels( iRow, false, true );
349 if ( typeof bAnimate == 'undefined' || bAnimate )
352 $(this.dom.scroller).animate( {
355 // This needs to happen after the animation has completed and
356 // the final scroll event fired
357 setTimeout( function () {
364 $(this.dom.scroller).scrollTop( px );
370 * Calculate and store information about how many rows are to be displayed
371 * in the scrolling viewport, based on current dimensions in the browser's
372 * rendering. This can be particularly useful if the table is initially
373 * drawn in a hidden element - for example in a tab.
374 * @param {bool} [bRedraw=true] Redraw the table automatically after the recalculation, with
375 * the new dimensions forming the basis for the draw.
378 * $(document).ready(function() {
379 * // Make the example container hidden to throw off the browser's sizing
380 * document.getElementById('container').style.display = "none";
381 * var oTable = $('#example').dataTable( {
382 * "sScrollY": "200px",
383 * "sAjaxSource": "media/dataset/large.txt",
385 * "bDeferRender": true,
386 * "fnInitComplete": function (o) {
387 * // Immediately scroll to row 1000
388 * o.oScroller.fnScrollToRow( 1000 );
392 * setTimeout( function () {
393 * // Make the example container visible and recalculate the scroller sizes
394 * document.getElementById('container').style.display = "block";
395 * oTable.fnSettings().oScroller.fnMeasure();
398 "fnMeasure": function ( bRedraw )
400 if ( this.s.autoHeight )
402 this._fnCalcRowHeight();
405 var heights = this.s.heights;
407 heights.viewport = $(this.dom.scroller).height();
408 this.s.viewportRows = parseInt( heights.viewport / heights.row, 10 )+1;
409 this.s.dt._iDisplayLength = this.s.viewportRows * this.s.displayBuffer;
411 if ( bRedraw === undefined || bRedraw )
413 this.s.dt.oInstance.fnDraw();
419 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
420 * Private methods (they are of course public in JS, but recommended as private)
421 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
424 * Initialisation for Scroller
428 "_fnConstruct": function ()
433 if ( !this.s.dt.oFeatures.bPaginate ) {
434 this.s.dt.oApi._fnLog( this.s.dt, 0, 'Pagination must be enabled for Scroller' );
438 /* Insert a div element that we can use to force the DT scrolling container to
439 * the height that would be required if the whole table was being displayed
441 this.dom.force.style.position = "absolute";
442 this.dom.force.style.top = "0px";
443 this.dom.force.style.left = "0px";
444 this.dom.force.style.width = "1px";
446 this.dom.scroller = $('div.'+this.s.dt.oClasses.sScrollBody, this.s.dt.nTableWrapper)[0];
447 this.dom.scroller.appendChild( this.dom.force );
448 this.dom.scroller.style.position = "relative";
450 this.dom.table = $('>table', this.dom.scroller)[0];
451 this.dom.table.style.position = "absolute";
452 this.dom.table.style.top = "0px";
453 this.dom.table.style.left = "0px";
455 // Add class to 'announce' that we are a Scroller table
456 $(this.s.dt.nTableWrapper).addClass('DTS');
458 // Add a 'loading' indicator
459 if ( this.s.loadingIndicator )
461 this.dom.loader = $('<div class="DTS_Loading">'+this.s.dt.oLanguage.sLoadingRecords+'</div>')
462 .css('display', 'none');
464 $(this.dom.scroller.parentNode)
465 .css('position', 'relative')
466 .append( this.dom.loader );
469 /* Initial size calculations */
470 if ( this.s.heights.row && this.s.heights.row != 'auto' )
472 this.s.autoHeight = false;
474 this.fnMeasure( false );
476 /* Scrolling callback to see if a page change is needed - use a throttled
477 * function for the save save callback so we aren't hitting it on every
480 this.s.ingnoreScroll = true;
481 this.s.stateSaveThrottle = this.s.dt.oApi._fnThrottle( function () {
482 that.s.dt.oApi._fnSaveState( that.s.dt );
484 $(this.dom.scroller).on( 'scroll.DTS', function (e) {
485 that._fnScroll.call( that );
488 /* In iOS we catch the touchstart event in case the user tries to scroll
489 * while the display is already scrolling
491 $(this.dom.scroller).on('touchstart.DTS', function () {
492 that._fnScroll.call( that );
495 /* Update the scroller when the DataTable is redrawn */
496 this.s.dt.aoDrawCallback.push( {
498 if ( that.s.dt.bInitialised ) {
499 that._fnDrawCallback.call( that );
505 /* On resize, update the information element, since the number of rows shown might change */
506 $(window).on( 'resize.DTS', function () {
507 that.fnMeasure( false );
511 /* Add a state saving parameter to the DT state saving so we can restore the exact
512 * position of the scrolling
514 var initialStateSave = true;
515 this.s.dt.oApi._fnCallbackReg( this.s.dt, 'aoStateSaveParams', function (oS, oData) {
516 /* Set iScroller to saved scroll position on initialization.
518 if(initialStateSave && that.s.dt.oLoadedState){
519 oData.iScroller = that.s.dt.oLoadedState.iScroller;
520 oData.iScrollerTopRow = that.s.dt.oLoadedState.iScrollerTopRow;
521 initialStateSave = false;
523 oData.iScroller = that.dom.scroller.scrollTop;
524 oData.iScrollerTopRow = that.s.topRowFloat;
526 }, "Scroller_State" );
528 if ( this.s.dt.oLoadedState ) {
529 this.s.topRowFloat = this.s.dt.oLoadedState.iScrollerTopRow || 0;
533 this.s.dt.aoDestroyCallback.push( {
536 $(window).off( 'resize.DTS' );
537 $(that.dom.scroller).off('touchstart.DTS scroll.DTS');
538 $(that.s.dt.nTableWrapper).removeClass('DTS');
539 $('div.DTS_Loading', that.dom.scroller.parentNode).remove();
541 that.dom.table.style.position = "";
542 that.dom.table.style.top = "";
543 that.dom.table.style.left = "";
550 * Scrolling function - fired whenever the scrolling position is changed.
551 * This method needs to use the stored values to see if the table should be
552 * redrawn as we are moving towards the end of the information that is
553 * currently drawn or not. If needed, then it will redraw the table based on
558 "_fnScroll": function ()
562 heights = this.s.heights,
563 iScrollTop = this.dom.scroller.scrollTop,
570 if ( this.s.ingnoreScroll ) {
574 /* If the table has been sorted or filtered, then we use the redraw that
575 * DataTables as done, rather than performing our own
577 if ( this.s.dt.bFiltered || this.s.dt.bSorted ) {
578 this.s.lastScrollTop = 0;
582 /* Update the table's information display for what is now in the viewport */
585 /* We don't want to state save on every scroll event - that's heavy
586 * handed, so use a timeout to update the state saving only when the
587 * scrolling has finished
589 clearTimeout( this.s.stateTO );
590 this.s.stateTO = setTimeout( function () {
591 that.s.dt.oApi._fnSaveState( that.s.dt );
594 /* Check if the scroll point is outside the trigger boundary which would required
595 * a DataTables redraw
597 if ( iScrollTop < this.s.redrawTop || iScrollTop > this.s.redrawBottom ) {
598 var preRows = Math.ceil( ((this.s.displayBuffer-1)/2) * this.s.viewportRows );
600 if ( Math.abs( iScrollTop - this.s.lastScrollTop ) > heights.viewport || this.s.ani ) {
601 iTopRow = parseInt(this._domain( 'physicalToVirtual', iScrollTop ) / heights.row, 10) - preRows;
602 this.s.topRowFloat = (this._domain( 'physicalToVirtual', iScrollTop ) / heights.row);
605 iTopRow = this.fnPixelsToRow( iScrollTop ) - preRows;
606 this.s.topRowFloat = this.fnPixelsToRow( iScrollTop, false );
609 if ( iTopRow <= 0 ) {
610 /* At the start of the table */
613 else if ( iTopRow + this.s.dt._iDisplayLength > this.s.dt.fnRecordsDisplay() ) {
614 /* At the end of the table */
615 iTopRow = this.s.dt.fnRecordsDisplay() - this.s.dt._iDisplayLength;
620 else if ( iTopRow % 2 !== 0 ) {
621 // For the row-striping classes (odd/even) we want only to start
622 // on evens otherwise the stripes will change between draws and
627 if ( iTopRow != this.s.dt._iDisplayStart ) {
628 /* Cache the new table position for quick lookups */
629 this.s.tableTop = $(this.s.dt.nTable).offset().top;
630 this.s.tableBottom = $(this.s.dt.nTable).height() + this.s.tableTop;
632 var draw = function () {
633 if ( that.s.scrollDrawReq === null ) {
634 that.s.scrollDrawReq = iScrollTop;
637 that.s.dt._iDisplayStart = iTopRow;
638 if ( that.s.dt.oApi._fnCalculateEnd ) { // Removed in 1.10
639 that.s.dt.oApi._fnCalculateEnd( that.s.dt );
641 that.s.dt.oApi._fnDraw( that.s.dt );
644 /* Do the DataTables redraw based on the calculated start point - note that when
645 * using server-side processing we introduce a small delay to not DoS the server...
647 if ( this.s.dt.oFeatures.bServerSide ) {
648 clearTimeout( this.s.drawTO );
649 this.s.drawTO = setTimeout( draw, this.s.serverWait );
655 if ( this.dom.loader && ! this.s.loaderVisible ) {
656 this.dom.loader.css( 'display', 'block' );
657 this.s.loaderVisible = true;
662 this.s.lastScrollTop = iScrollTop;
663 this.s.stateSaveThrottle();
668 * Convert from one domain to another. The physical domain is the actual
669 * pixel count on the screen, while the virtual is if we had browsers which
670 * had scrolling containers of infinite height (i.e. the absolute value)
672 * @param {string} dir Domain transform direction, `virtualToPhysical` or
673 * `physicalToVirtual`
674 * @returns {number} Calculated transform
677 _domain: function ( dir, val )
679 var heights = this.s.heights;
682 // If the virtual and physical height match, then we use a linear
683 // transform between the two, allowing the scrollbar to be linear
684 if ( heights.virtual === heights.scroll ) {
685 coeff = (heights.virtual-heights.viewport) / (heights.scroll-heights.viewport);
687 if ( dir === 'virtualToPhysical' ) {
690 else if ( dir === 'physicalToVirtual' ) {
695 // Otherwise, we want a non-linear scrollbar to take account of the
696 // redrawing regions at the start and end of the table, otherwise these
697 // can stutter badly - on large tables 30px (for example) scroll might
698 // be hundreds of rows, so the table would be redrawing every few px at
699 // the start and end. Use a simple quadratic to stop this. It does mean
700 // the scrollbar is non-linear, but with such massive data sets, the
701 // scrollbar is going to be a best guess anyway
702 var xMax = (heights.scroll - heights.viewport) / 2;
703 var yMax = (heights.virtual - heights.viewport) / 2;
705 coeff = yMax / ( xMax * xMax );
707 if ( dir === 'virtualToPhysical' ) {
709 return Math.pow(val / coeff, 0.5);
712 val = (yMax*2) - val;
715 (xMax*2) - Math.pow(val / coeff, 0.5);
718 else if ( dir === 'physicalToVirtual' ) {
720 return val * val * coeff;
723 val = (xMax*2) - val;
726 (yMax*2) - (val * val * coeff);
733 * Draw callback function which is fired when the DataTable is redrawn. The main function of
734 * this method is to position the drawn table correctly the scrolling container for the rows
735 * that is displays as a result of the scrolling position.
739 "_fnDrawCallback": function ()
743 heights = this.s.heights,
744 iScrollTop = this.dom.scroller.scrollTop,
745 iActualScrollTop = iScrollTop,
746 iScrollBottom = iScrollTop + heights.viewport,
747 iTableHeight = $(this.s.dt.nTable).height(),
748 displayStart = this.s.dt._iDisplayStart,
749 displayLen = this.s.dt._iDisplayLength,
750 displayEnd = this.s.dt.fnRecordsDisplay();
752 // Disable the scroll event listener while we are updating the DOM
755 // Resize the scroll forcing element
756 this._fnScrollForce();
758 // Reposition the scrolling for the updated virtual position if needed
759 if ( displayStart === 0 ) {
760 // Linear calculation at the top of the table
761 iScrollTop = this.s.topRowFloat * heights.row;
763 else if ( displayStart + displayLen >= displayEnd ) {
764 // Linear calculation that the bottom as well
765 iScrollTop = heights.scroll - ((displayEnd - this.s.topRowFloat) * heights.row);
768 // Domain scaled in the middle
769 iScrollTop = this._domain( 'virtualToPhysical', this.s.topRowFloat * heights.row );
772 this.dom.scroller.scrollTop = iScrollTop;
774 // Store positional information so positional calculations can be based
775 // upon the current table draw position
776 this.s.baseScrollTop = iScrollTop;
777 this.s.baseRowTop = this.s.topRowFloat;
779 // Position the table in the virtual scroller
780 var tableTop = iScrollTop - ((this.s.topRowFloat - displayStart) * heights.row);
781 if ( displayStart === 0 ) {
784 else if ( displayStart + displayLen >= displayEnd ) {
785 tableTop = heights.scroll - iTableHeight;
788 this.dom.table.style.top = tableTop+'px';
790 /* Cache some information for the scroller */
791 this.s.tableTop = tableTop;
792 this.s.tableBottom = iTableHeight + this.s.tableTop;
794 // Calculate the boundaries for where a redraw will be triggered by the
795 // scroll event listener
796 var boundaryPx = (iScrollTop - this.s.tableTop) * this.s.boundaryScale;
797 this.s.redrawTop = iScrollTop - boundaryPx;
798 this.s.redrawBottom = iScrollTop + boundaryPx;
802 // Restore the scrolling position that was saved by DataTable's state
803 // saving Note that this is done on the second draw when data is Ajax
804 // sourced, and the first draw when DOM soured
805 if ( this.s.dt.oFeatures.bStateSave && this.s.dt.oLoadedState !== null &&
806 typeof this.s.dt.oLoadedState.iScroller != 'undefined' )
808 // A quirk of DataTables is that the draw callback will occur on an
809 // empty set if Ajax sourced, but not if server-side processing.
810 var ajaxSourced = (this.s.dt.sAjaxSource || that.s.dt.ajax) && ! this.s.dt.oFeatures.bServerSide ?
814 if ( ( ajaxSourced && this.s.dt.iDraw == 2) ||
815 (!ajaxSourced && this.s.dt.iDraw == 1) )
817 setTimeout( function () {
818 $(that.dom.scroller).scrollTop( that.s.dt.oLoadedState.iScroller );
819 that.s.redrawTop = that.s.dt.oLoadedState.iScroller - (heights.viewport/2);
821 // In order to prevent layout thrashing we need another
823 setTimeout( function () {
824 that.s.ingnoreScroll = false;
830 that.s.ingnoreScroll = false;
833 // Because of the order of the DT callbacks, the info update will
834 // take precedence over the one we want here. So a 'thread' break is
836 setTimeout( function () {
837 that._fnInfo.call( that );
840 // Hide the loading indicator
841 if ( this.dom.loader && this.s.loaderVisible ) {
842 this.dom.loader.css( 'display', 'none' );
843 this.s.loaderVisible = false;
849 * Force the scrolling container to have height beyond that of just the
850 * table that has been drawn so the user can scroll the whole data set.
852 * Note that if the calculated required scrolling height exceeds a maximum
853 * value (1 million pixels - hard-coded) the forcing element will be set
854 * only to that maximum value and virtual / physical domain transforms will
855 * be used to allow Scroller to display tables of any number of records.
859 _fnScrollForce: function ()
861 var heights = this.s.heights;
864 heights.virtual = heights.row * this.s.dt.fnRecordsDisplay();
865 heights.scroll = heights.virtual;
867 if ( heights.scroll > max ) {
868 heights.scroll = max;
871 this.dom.force.style.height = heights.scroll+"px";
876 * Automatic calculation of table row height. This is just a little tricky here as using
877 * initialisation DataTables has tale the table out of the document, so we need to create
878 * a new table and insert it into the document, calculate the row height and then whip the
883 "_fnCalcRowHeight": function ()
886 var origTable = dt.nTable;
887 var nTable = origTable.cloneNode( false );
888 var tbody = $('<tbody/>').appendTo( nTable );
890 '<div class="'+dt.oClasses.sWrapper+' DTS">'+
891 '<div class="'+dt.oClasses.sScrollWrapper+'">'+
892 '<div class="'+dt.oClasses.sScrollBody+'"></div>'+
897 // Want 3 rows in the sizing table so :first-child and :last-child
898 // CSS styles don't come into play - take the size of the middle row
899 $('tbody tr:lt(4)', origTable).clone().appendTo( tbody );
900 while( $('tr', tbody).length < 3 ) {
901 tbody.append( '<tr><td> </td></tr>' );
904 $('div.'+dt.oClasses.sScrollBody, container).append( nTable );
907 if (dt._bInitComplete) {
908 appendTo = origTable.parentNode;
910 if (!this.s.dt.nHolding) {
911 this.s.dt.nHolding = $( '<div></div>' ).insertBefore( this.s.dt.nTable );
913 appendTo = this.s.dt.nHolding;
916 container.appendTo( appendTo );
917 this.s.heights.row = $('tr', tbody).eq(1).outerHeight();
923 * Update any information elements that are controlled by the DataTable based on the scrolling
924 * viewport and what rows are visible in it. This function basically acts in the same way as
925 * _fnUpdateInfo in DataTables, and effectively replaces that function.
929 "_fnInfo": function ()
931 if ( !this.s.dt.oFeatures.bInfo )
938 language = dt.oLanguage,
939 iScrollTop = this.dom.scroller.scrollTop,
940 iStart = Math.floor( this.fnPixelsToRow(iScrollTop, false, this.s.ani)+1 ),
941 iMax = dt.fnRecordsTotal(),
942 iTotal = dt.fnRecordsDisplay(),
943 iPossibleEnd = Math.ceil( this.fnPixelsToRow(iScrollTop+this.s.heights.viewport, false, this.s.ani) ),
944 iEnd = iTotal < iPossibleEnd ? iTotal : iPossibleEnd,
945 sStart = dt.fnFormatNumber( iStart ),
946 sEnd = dt.fnFormatNumber( iEnd ),
947 sMax = dt.fnFormatNumber( iMax ),
948 sTotal = dt.fnFormatNumber( iTotal ),
951 if ( dt.fnRecordsDisplay() === 0 &&
952 dt.fnRecordsDisplay() == dt.fnRecordsTotal() )
954 /* Empty record set */
955 sOut = language.sInfoEmpty+ language.sInfoPostFix;
957 else if ( dt.fnRecordsDisplay() === 0 )
959 /* Empty record set after filtering */
960 sOut = language.sInfoEmpty +' '+
961 language.sInfoFiltered.replace('_MAX_', sMax)+
962 language.sInfoPostFix;
964 else if ( dt.fnRecordsDisplay() == dt.fnRecordsTotal() )
966 /* Normal record set */
967 sOut = language.sInfo.
968 replace('_START_', sStart).
969 replace('_END_', sEnd).
970 replace('_MAX_', sMax).
971 replace('_TOTAL_', sTotal)+
972 language.sInfoPostFix;
976 /* Record set after filtering */
977 sOut = language.sInfo.
978 replace('_START_', sStart).
979 replace('_END_', sEnd).
980 replace('_MAX_', sMax).
981 replace('_TOTAL_', sTotal) +' '+
982 language.sInfoFiltered.replace(
984 dt.fnFormatNumber(dt.fnRecordsTotal())
986 language.sInfoPostFix;
989 var callback = language.fnInfoCallback;
991 sOut = callback.call( dt.oInstance,
992 dt, iStart, iEnd, iMax, iTotal, sOut
996 var n = dt.aanFeatures.i;
997 if ( typeof n != 'undefined' )
999 for ( var i=0, iLen=n.length ; i<iLen ; i++ )
1001 $(n[i]).html( sOut );
1009 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1011 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1015 * Scroller default settings for initialisation
1017 * @name Scroller.defaults
1020 Scroller.defaults = /** @lends Scroller.defaults */{
1022 * Indicate if Scroller show show trace information on the console or not. This can be
1023 * useful when debugging Scroller or if just curious as to what it is doing, but should
1024 * be turned off for production.
1029 * var oTable = $('#example').dataTable( {
1030 * "sScrollY": "200px",
1032 * "bDeferRender": true,
1041 * Scroller will attempt to automatically calculate the height of rows for it's internal
1042 * calculations. However the height that is used can be overridden using this parameter.
1047 * var oTable = $('#example').dataTable( {
1048 * "sScrollY": "200px",
1050 * "bDeferRender": true,
1056 "rowHeight": "auto",
1059 * When using server-side processing, Scroller will wait a small amount of time to allow
1060 * the scrolling to finish before requesting more data from the server. This prevents
1061 * you from DoSing your own server! The wait time can be configured by this parameter.
1066 * var oTable = $('#example').dataTable( {
1067 * "sScrollY": "200px",
1069 * "bDeferRender": true,
1078 * The display buffer is what Scroller uses to calculate how many rows it should pre-fetch
1079 * for scrolling. Scroller automatically adjusts DataTables' display length to pre-fetch
1080 * rows that will be shown in "near scrolling" (i.e. just beyond the current display area).
1081 * The value is based upon the number of rows that can be displayed in the viewport (i.e.
1082 * a value of 1), and will apply the display range to records before before and after the
1083 * current viewport - i.e. a factor of 3 will allow Scroller to pre-fetch 1 viewport's worth
1084 * of rows before the current viewport, the current viewport's rows and 1 viewport's worth
1085 * of rows after the current viewport. Adjusting this value can be useful for ensuring
1086 * smooth scrolling based on your data set.
1091 * var oTable = $('#example').dataTable( {
1092 * "sScrollY": "200px",
1094 * "bDeferRender": true,
1096 * "displayBuffer": 10
1103 * Scroller uses the boundary scaling factor to decide when to redraw the table - which it
1104 * typically does before you reach the end of the currently loaded data set (in order to
1105 * allow the data to look continuous to a user scrolling through the data). If given as 0
1106 * then the table will be redrawn whenever the viewport is scrolled, while 1 would not
1107 * redraw the table until the currently loaded data has all been shown. You will want
1108 * something in the middle - the default factor of 0.5 is usually suitable.
1113 * var oTable = $('#example').dataTable( {
1114 * "sScrollY": "200px",
1116 * "bDeferRender": true,
1118 * "boundaryScale": 0.75
1122 "boundaryScale": 0.5,
1125 * Show (or not) the loading element in the background of the table. Note that you should
1126 * include the dataTables.scroller.css file for this to be displayed correctly.
1131 * var oTable = $('#example').dataTable( {
1132 * "sScrollY": "200px",
1134 * "bDeferRender": true,
1136 * "loadingIndicator": true
1140 "loadingIndicator": false
1143 Scroller.oDefaults = Scroller.defaults;
1147 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1149 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1155 * @name Scroller.version
1158 Scroller.version = "1.2.2";
1162 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1164 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1167 * Register a new feature with DataTables
1169 if ( typeof $.fn.dataTable == "function" &&
1170 typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
1171 $.fn.dataTableExt.fnVersionCheck('1.9.0') )
1173 $.fn.dataTableExt.aoFeatures.push( {
1174 "fnInit": function( oDTSettings ) {
1175 var init = oDTSettings.oInit;
1176 var opts = init.scroller || init.oScroller || {};
1177 var oScroller = new Scroller( oDTSettings, opts );
1178 return oScroller.dom.wrapper;
1181 "sFeature": "Scroller"
1186 alert( "Warning: Scroller requires DataTables 1.9.0 or greater - www.datatables.net/download");
1190 // Attach Scroller to DataTables so it can be accessed as an 'extra'
1191 $.fn.dataTable.Scroller = Scroller;
1192 $.fn.DataTable.Scroller = Scroller;
1195 // DataTables 1.10 API method aliases
1196 if ( $.fn.dataTable.Api ) {
1197 var Api = $.fn.dataTable.Api;
1199 Api.register( 'scroller()', function () {
1203 Api.register( 'scroller().rowToPixels()', function ( rowIdx, intParse, virtual ) {
1204 var ctx = this.context;
1206 if ( ctx.length && ctx[0].oScroller ) {
1207 return ctx[0].oScroller.fnRowToPixels( rowIdx, intParse, virtual );
1212 Api.register( 'scroller().pixelsToRow()', function ( pixels, intParse, virtual ) {
1213 var ctx = this.context;
1215 if ( ctx.length && ctx[0].oScroller ) {
1216 return ctx[0].oScroller.fnPixelsToRow( pixels, intParse, virtual );
1221 Api.register( 'scroller().scrollToRow()', function ( row, ani ) {
1222 this.iterator( 'table', function ( ctx ) {
1223 if ( ctx.oScroller ) {
1224 ctx.oScroller.fnScrollToRow( row, ani );
1231 Api.register( 'scroller().measure()', function ( redraw ) {
1232 this.iterator( 'table', function ( ctx ) {
1233 if ( ctx.oScroller ) {
1234 ctx.oScroller.fnMeasure( redraw );
1247 // Define as an AMD module if possible
1248 if ( typeof define === 'function' && define.amd ) {
1249 define( ['jquery', 'datatables'], factory );
1251 else if ( typeof exports === 'object' ) {
1253 factory( require('jquery'), require('datatables') );
1255 else if ( jQuery && !jQuery.fn.dataTable.Scroller ) {
1256 // Otherwise simply initialise as normal, stopping multiple evaluation
1257 factory( jQuery, jQuery.fn.dataTable );
1261 })(window, document);