Scroller: determine if a row is visible in the viewport

Scroller: determine if a row is visible in the viewport

beginner_beginner_ Posts: 55Questions: 2Answers: 0
edited July 2012 in General
I've enabled browser back-button support so hitting on back button will return a user to the previously selected row. However as long as that row is visible I do not want to use oScroller.fnScrollToRow, only when the row is not visible.

I have the row index of the row. How can i determine if that row is currently visible in the viewport or not?

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Interesting one! Currently there isn't an API method to do that, but I have taken a note of this as a future enhancement for Scroller. Thanks for the suggestion.

    Allan
  • beginner_beginner_ Posts: 55Questions: 2Answers: 0
    Hi Allan,

    yeah that would be nice. I've now implemented following custom workaround which more or less works. I guess adding parameter to the function could make it more generally usable instead of my hardcoded stuff.

    [code]
    function isRowVisible(rowIndex){

    var oSettings = myTable.fnSettings();
    var scrollBody = $('#myTable_wrapper .dataTables_scrollBody');
    var scrollTop = $(scrollBody[0]).scrollTop();
    var rowToPixels = oSettings.oScroller.fnRowToPixels(rowIndex);
    var scrollY = parseInt(oSettings.oScroll.sY);
    // for edge cases, as far as I can tell header height is 20 px
    // if only 1 line high and is included in scrollY
    scrollY = scrollY - 21;

    if((scrollTop + scrollY) < rowToPixels
    ||scrollTop > rowToPixels) {
    return false;
    } else {
    return true;
    }
    }
    [/code]
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Looks good - thanks for sharing your solution with us!

    Allan
  • beginner_beginner_ Posts: 55Questions: 2Answers: 0
    edited August 2012
    There is a big issue with this: i use fnFindCellRowIndexes to determine rowIndex. However this does not take into account by which column the table is sorted and hence if it is not sorted the same way the internal array is, the rowindex will be wrong.

    EDIT:

    the solution is this:

    [code]
    var rowIndexes = myTable.fnFindCellRowIndexes(id, "id" );
    var rowIndex = rowIndexes[0];
    rowIndex = oSettings.aiDisplay.indexOf(rowIndex); //aiDisplay takes sorting and filtering into account
    [/code]
This discussion has been closed.