How do I go-to / scroll to row without using scroller plug-in?

How do I go-to / scroll to row without using scroller plug-in?

LukeSLukeS Posts: 9Questions: 0Answers: 0
edited August 2012 in General
Is it possible to scroll to a row when vertical scrolling is enabled but NOT the scroller plug-in? I know of the fnScrollToRow method with the scroller plug-in but the scroller plug-in locks up on mobile devices when you scroll fast so I am not using that in my my application. I have also tried the fnDisplayRow API plug-in function but that only works with pages? Any ideas?

Replies

  • LukeSLukeS Posts: 9Questions: 0Answers: 0
    edited September 2012
    I got it working on desktop browsers some trial and error, here is the function I wrote to do it if anyone tries to do the same. This does not work on mobile browsers, it appears the jQuery scrollTop() function is the issue, when I try directly calling this function with a fixed pixel value in a mobile browser it does nothing. Any ideas?
    [code]
    function goToRow(iRowIndex, oTable) {
    var iScrollingDivSize, iScrollingDivHeight, iTotalNumOfRows, iRowHeigth, iNewPosition;

    /* Get total height of scrolling div, height of DIV (set in sScrollY), and total number of rows */
    iScrollingDivSize = jQuery('#DataTablesDIV .dataTables_scrollBody')[0].scrollHeight;
    iScrollingDivHeight = jQuery('#DataTablesDIV .dataTables_scrollBody')[0].offsetHeight;
    iTotalNumOfRows = oTable.fnSettings().fnRecordsTotal();

    /* Calculate height of each row */
    iRowHeigth = iScrollingDivSize / iTotalNumOfRows;

    /* Calculate pixels down in scrolling DIV to center row in scrolling DIV */
    iNewPosition = (iRowHeigth * iRowIndex) - (iScrollingDivHeight / 2);

    /* Bounds check */
    if (iNewPosition > iScrollingDivSize) {iNewPosition = iScrollingDivSize; }
    if (iNewPosition < 0) {iNewPosition = 0; }

    /* Goto row */
    jQuery('#DataTablesDIV .dataTables_scrollBody').scrollTop(iNewPosition);
    }
    [/code]

    Is the scrolling DIV height set in the "sScrollY" initialization setting present somewhere in oTable.fnSettings()? I am pulling a parameter I found in the jQuery object right now but rather get it from the datatable settings.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Yup - that looks like what would be needed to me. There is no external method for doing that at the moment, so something like that would be needed. However, can't you just use position()?

    For example: http://live.datatables.net/akepus/edit#javascript,html

    > Is the scrolling DIV height set in the "sScrollY" initialization setting present somewhere in oTable.fnSettings()?

    Yes - its in oScroll.sY : http://datatables.net/docs/DataTables/1.9.3/DataTable.models.oSettings.oScroll.html#sY

    Allan
This discussion has been closed.