如何在Datatables中加载表并让它在加载时自动滚动到最后一条记录

时间:2012-11-21 04:22:41

标签: javascript php jquery jquery-datatables

问题:使用dataTables javascript库来完美地显示数据。

通过PHP / mysql检索数据然后绘制表格,代码循环遍历表格:

dataTables的JAVA代码 -

<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 -->

-

问题是,该表当前显示所有数据,如果用户可以向下滚动,我需要表格在表格的底部有滚动条。用户可以向上滚动,但不能向下滚动。

我尝试了jquery scrollTop,但是没有用,我找不到任何人成功的例子。

- 推荐后尝试#2 -

不幸的是没有运气

<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>

仍然没有运气..

2 个答案:

答案 0 :(得分:1)

如果您使用Scroller extra,则可以在初始化函数中调用以下内容:

"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.
}

祝你好运!

答案 1 :(得分:0)

试,

$( “dataTables_scrollBody。”)scrollTop的(9999);

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