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:
Name | Type | Attributes | Default | Description | |
---|---|---|---|---|---|
1 | oDT | object | DataTables settings object | ||
2 | oOpts | object | Optional | {} | Configuration object for FixedColumns. Options are defined by Scroller.oDefaults |
$(document).ready(function() { $('#example').dataTable( { "sScrollY": "200px", "sAjaxSource": "media/dataset/large.txt", "sDom": "frtiS", "bDeferRender": true } ); } );
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.
Name | Type | Attributes | Default | Description | |
---|---|---|---|---|---|
1 | bRedraw | bool | Optional | true | Redraw the table automatically after the recalculation, with the new dimentions forming the basis for the draw. |
$(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 );
Calculate the row number that will be found at the given pixel position (y-scroll)
Name | Type | Attributes | Default | Description | |
---|---|---|---|---|---|
1 | iPixels | int | Offset from top to caluclate the row number of |
Row index
$(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 ) ); } } ); } );
Calculate the pixel position from the top of the scrolling container for a given row
Name | Type | Attributes | Default | Description | |
---|---|---|---|---|---|
1 | iRow | int | Row number to calculate the position of |
Pixels
$(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 ) ); } } ); } );
Calculate the row number that will be found at the given pixel position (y-scroll)
Name | Type | Attributes | Default | Description | |
---|---|---|---|---|---|
1 | iRow | int | Row index to scroll to | ||
2 | bAnimate | bool | Optional | true | Animate the transision or not |
$(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 ); } );
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.
Initialisation for Scroller
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.
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.
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.