How to load table in Datatables and have it scroll to last record automatically on load

ぃ、小莉子 提交于 2019-12-23 16:16:02

问题


Problem : using dataTables javascript library to display data perfectly fine.

Data is retrieved via PHP/mysql and then the table is drawn and the code cycles through the tables as such :

JAVA code for dataTables -

<script>
$(document).ready(function() {
    $("#PayMasterResults").dataTable( {
    "sPaginationType": "full_numbers",
    "bPaginate": false,
    "bJQueryUI" : true,
    "sScrollY": "215",
    "bLengthChange": true,
    "bFilter": true,
    "bSort": false,
    "bInfo": true,
    "bAutoWidth": false,
    "sDom": \'<"H"lTfr>t<"F"ip>\',
    "oTableTools": {
            "sSwfPath": "lib/copy_csv_xls_pdf.swf",
            "sRowSelect": "multi",
            "aButtons": [
                    "print",
                    {
                            "sExtends": "copy",
                            "sButtonText": "Copy to clipboard"
                    },
                    {
                            "sExtends": "csv",
                            "sButtonText": "Export CSV"
                    },
                    {
                            "sExtends": "xls",
                            "sButtonText": "Save for Excel"
                    },
                    {
                            "sExtends": "pdf",
                            "sButtonText": "Save to PDF",
                            "sPdfOrientation": "landscape",
                    }
            ]
    }
} );
});
</script>

HTML / PHP -

            <div class="table">
            <table class="display" id="PayMasterResults">
                    <thead>
                            <tr>
                             <th class="" width="600px"><b>Entry Description</b></th>
                             <th class=""><b>Entry Timestamp</b></th>
                             <th class=""><b>Debit/Credit</b></th>
                             <th class=""><b>Running Balance</b></th>
                            </tr>
                    </thead>
                    <tbody>
            ';
            for($x=0; $x<$xmldata->Ledger->count(); $x++) {
                    echo '
                            <tr class="gradeB">
                             <td><a href="#" onclick="loadPMLedgerDetail(\''.$xmldata->Ledger[$x]->attributes()->ID.'\',\''.$_POST['ID'].'\');">'.$xmldata->Ledger[$x]->Description.'</a></td>
                             <td>'.$xmldata->Ledger[$x]->Stamp.'</td>
                             <Td>'.$xmldata->Ledger[$x]->Amount.'</td>
                             <td>'.$xmldata->Ledger[$x]->Balance.'</td>
                            </tr>
                    ';
            }
            echo '
                    </tbody>
            </table>

            </div> <!-- End Table -->

--

The problem is, the table currently displays all data and the user can scroll down if they like, I need the table to have the scrollbar at the bottom of the table. so the User can scroll up, but not down.

I have attempted jquery scrollTop but that did not work, i cannot find any examples of people succesflly getting this to work.

-- Attempt #2 after recommendation --

no luck unfortunately

<script>
$(document).ready(function() {
var myTable = $("#PMLedgerDetailsTable").dataTable( {
    "sPaginationType": "full_numbers",
    "aaSorting": [[ 0, "asc" ]],
    "bPaginate": true,
    "bJQueryUI" : true,
    "sScrollY": "215",
    "bLengthChange": true,
    "bFilter": true,
    "bSort": false,
    "bInfo": true,
    "bAutoWidth": false,
    "sDom": \'<"H"lTfr>t<"F"ip>S\',
    "oTableTools": {
            "sSwfPath": "lib/copy_csv_xls_pdf.swf",
            "sRowSelect": "multi",
            "aButtons": [
                    "print",
                    {
                            "sExtends": "copy",
                            "sButtonText": "Copy to clipboard"
                    },
                    {
                            "sExtends": "csv",
                            "sButtonText": "Export CSV"
                    },
                    {
                            "sExtends": "xls",
                            "sButtonText": "Save for Excel"
                    },
                    {
                            "sExtends": "pdf",
                            "sButtonText": "Save to PDF",
                            "sPdfOrientation": "landscape",
                    }
            ]
    },
    "fnInitComplete" : function() {
            myTable.fnSettings().oScroller.fnScrollToRow(10000);
    }
} );

});

</script>

Still no luck..


回答1:


If you use the Scroller extra, you can call the following in your initialization function:

"fnInitComplete": function() { 

    // Make sure you have your table stored to a variable
    // (here, I used oTable).  Then tell the Scroller to
    // scroll to an arbitrary large number beyond the 
    // number of rows in your table.

    oTable.fnSettings().oScroller.fnScrollToRow(100000); 

    // Alternatively, if you know the number of rows in 
    // your table at runtime, you could just feed in that
    // value to the Scroller.
}

Best of luck!




回答2:


try,

$(".dataTables_scrollBody").scrollTop(9999);

http://datatables.net/forums/discussion/19084/scroller-plugin-scroll-table-with-keypress



来源:https://stackoverflow.com/questions/13486319/how-to-load-table-in-datatables-and-have-it-scroll-to-last-record-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!