Class: Scroller

Scroller v1.0.1 documentation

Navigation

Hiding private elements (toggle)
Showing extended elements (toggle)
new Scroller( oDT:object, [oOpts:object] )

Scroller is a virtual rendering plug-in for DataTables which allows large datasets to be drawn on screen every quickly. What the virtual rendering means is that only the visible portion of the table (and a bit to either side to make the scrolling smooth) is drawn, while the scrolling container gives the visual impression that the whole table is visible. This is done by making use of the pagination abilities of DataTables and moving the table around in the scrolling container DataTables adds to the page. The scrolling container is forced to the height it would be for the full table display using an extra element.

Note that rows in the table MUST all be the same hight. Information in a cell which expands on to multiple lines will cause some odd behaviour in the scrolling.

Scroller is initialised by simply including the letter 'S' in the sDom for the table you want to have this feature enabled on. Note that the 'S' must come AFTER the 't' parameter in sDom.

Key features include:

  • Speed! The aim of Scroller for DataTables is to make rendering large data sets fast
  • Full compatibility with deferred rendering in DataTables 1.8 for maximum speed
  • Correct visual scrolling implementation, similar to "infinite scrolling" in DataTable core
  • Integration with state saving in DataTables (scrolling position is saved)
  • Easy to use

Constructor

Parameters:
Name Type Attributes Default Description
1
oDTobjectDataTables settings object
2
oOptsobjectOptional{}Configuration object for FixedColumns. Options are defined by Scroller.oDefaults
Example:
		$(document).ready(function() {
			$('#example').dataTable( {
				"sScrollY": "200px",
				"sAjaxSource": "media/dataset/large.txt",
				"sDom": "frtiS",
				"bDeferRender": true
			} );
		} );

Requires

  • jQuery 1.4+
  • DataTables 1.8.0+

Summary

Namespaces

dom
DOM elements used by the class instance
oDefaults
Scroller default settings for initialisation
s
Settings object which contains customisable information for the Scroller instance

Methods

fnMeasure( [bRedraw:bool] ):void
Calculate and store information about how many rows are to be displayed in the scrolling viewport, based on current dimensions in the browser's rendering. This can be particularly useful if the table is initially drawn in a hidden element - for example in a tab.
fnPixelsToRow( iPixels:int ):int
Calculate the row number that will be found at the given pixel position (y-scroll)
fnRowToPixels( iRow:int ):int
Calculate the pixel position from the top of the scrolling container for a given row
fnScrollToRow( iRow:int, [bAnimate:bool] ):void
Calculate the row number that will be found at the given pixel position (y-scroll)
<private> _fnCalcRowHeight( ):void
Automatic calculation of table row height. This is just a little tricky here as using initialisation DataTables has tale the table out of the document, so we need to create a new table and insert it into the document, calculate the row height and then whip the table out.
<private> _fnConstruct( ):void
Initialisation for Scroller
<private> _fnDrawCallback( ):void
Draw callback function which is fired when the DataTable is redrawn. The main function of this method is to position the drawn table correctly the scrolling container for the rows that is displays as a result of the scrolling position.
<private> _fnInfo( ):void
Update any information elements that are controlled by the DataTable based on the scrolling viewport and what rows are visible in it. This function basically acts in the same way as _fnUpdateInfo in DataTables, and effectively replaces that function.
<private> _fnScroll( ):void
Scrolling function - fired whenever the scrolling position is changed. This method needs to use the stored values to see if the table should be redrawn as we are moving towards the end of the information that is currently drawn or not. If needed, then it will redraw the table based on the new position.

Details

Methods

fnMeasure( [bRedraw:bool] ):void

Calculate and store information about how many rows are to be displayed in the scrolling viewport, based on current dimensions in the browser's rendering. This can be particularly useful if the table is initially drawn in a hidden element - for example in a tab.

Parameters:
Name Type Attributes Default Description
1
bRedrawboolOptionaltrueRedraw the table automatically after the recalculation, with the new dimentions forming the basis for the draw.
Example:
   $(document).ready(function() {
     // Make the example container hidden to throw off the browser's sizing
     document.getElementById('container').style.display = "none";
     var oTable = $('#example').dataTable( {
       "sScrollY": "200px",
       "sAjaxSource": "media/dataset/large.txt",
       "sDom": "frtiS",
       "bDeferRender": true,
       "fnInitComplete": function (o) {
         // Immediately scroll to row 1000
         o.oScroller.fnScrollToRow( 1000 );
       }
     } );
     
     setTimeout( function () {
       // Make the example container visible and recalculate the scroller sizes
       document.getElementById('container').style.display = "block";
       oTable.fnSettings().oScroller.fnMeasure();
     }, 3000 );
fnPixelsToRow( iPixels:int ):int

Calculate the row number that will be found at the given pixel position (y-scroll)

Parameters:
Name Type Attributes Default Description
1
iPixelsintOffset from top to caluclate the row number of
Returns:

Row index

Example:
   $(document).ready(function() {
     $('#example').dataTable( {
       "sScrollY": "200px",
       "sAjaxSource": "media/dataset/large.txt",
       "sDom": "frtiS",
       "bDeferRender": true,
       "fnInitComplete": function (o) {
         // Find what row number is at 500px
         alert( o.oScroller.fnPixelsToRow( 500 ) );
       }
     } );
   } );
fnRowToPixels( iRow:int ):int

Calculate the pixel position from the top of the scrolling container for a given row

Parameters:
Name Type Attributes Default Description
1
iRowintRow number to calculate the position of
Returns:

Pixels

Example:
   $(document).ready(function() {
     $('#example').dataTable( {
       "sScrollY": "200px",
       "sAjaxSource": "media/dataset/large.txt",
       "sDom": "frtiS",
       "bDeferRender": true,
       "fnInitComplete": function (o) {
         // Find where row 25 is
         alert( o.oScroller.fnRowToPixels( 25 ) );
       }
     } );
   } );
fnScrollToRow( iRow:int, [bAnimate:bool] ):void

Calculate the row number that will be found at the given pixel position (y-scroll)

Parameters:
Name Type Attributes Default Description
1
iRowintRow index to scroll to
2
bAnimateboolOptionaltrueAnimate the transision or not
Example:
   $(document).ready(function() {
     $('#example').dataTable( {
       "sScrollY": "200px",
       "sAjaxSource": "media/dataset/large.txt",
       "sDom": "frtiS",
       "bDeferRender": true,
       "fnInitComplete": function (o) {
         // Immediately scroll to row 1000
         o.oScroller.fnScrollToRow( 1000 );
       }
     } );
     
     // Sometime later on use the following to scroll to row 500...
         var oSettings = $('#example').dataTable().fnSettings();
     oSettings.oScroller.fnScrollToRow( 500 );
   } );
<private> _fnCalcRowHeight( ):void

Automatic calculation of table row height. This is just a little tricky here as using initialisation DataTables has tale the table out of the document, so we need to create a new table and insert it into the document, calculate the row height and then whip the table out.

<private> _fnConstruct( ):void

Initialisation for Scroller

<private> _fnDrawCallback( ):void

Draw callback function which is fired when the DataTable is redrawn. The main function of this method is to position the drawn table correctly the scrolling container for the rows that is displays as a result of the scrolling position.

<private> _fnInfo( ):void

Update any information elements that are controlled by the DataTable based on the scrolling viewport and what rows are visible in it. This function basically acts in the same way as _fnUpdateInfo in DataTables, and effectively replaces that function.

<private> _fnScroll( ):void

Scrolling function - fired whenever the scrolling position is changed. This method needs to use the stored values to see if the table should be redrawn as we are moving towards the end of the information that is currently drawn or not. If needed, then it will redraw the table based on the new position.