繁体   English   中英

datatables选择第一行onload

[英]datatables select first row onload

我正在尝试在第一次加载数据时自动选择第一行表并且无法执行此操作。

第一次加载表格时如何自动选择第一行表格? 这个HTML不起作用:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <style type="text/css" title="currentStyle">
            @import "DataTables/css/demo_page.css";
            @import "DataTables/css/demo_table.css";
    </style>


    <script type="text/javascript" src="Datatables/js/jquery.js"></script>
    <script type="text/javascript" src="Datatables/js/jquery.dataTables.js"></script>

    <script>

        var oTable;
        var firstTime = true;

        $(document).ready(function () {

            $("#example tbody").click(function (event) {
                $(oTable.fnSettings().aoData).each(function () {
                    $(this.nTr).removeClass('row_selected');
                });
                $(event.target.parentNode).addClass('row_selected');
            });

            oTable = $('#example').dataTable({
                "sScrollX": "500px",
                "sPaginationType": "full_numbers",

                "bServerSide": true,
                "sAjaxSource": "services/data3.ashx",
                "sServerMethod": "POST",
                "fnDrawCallback": function (oSettings) {
                    if (firstTime) {
                        alert('DataTables has redrawn the table');
                        oTable.$('tr:first').click();
                        firstTime = false;
                    }

                }
            });


        });

    </script>

</head>
<body>

    <table border="1" >

        <tr><td>id</td><td><input type="text" /></td></tr>
        <tr><td>name</td><td><input type="text" /></td></tr>
        <tr><td>surname</td><td><input type="text" /></td></tr>


    </table><br />

    <table id="example" border="1" class="display">
        <thead>
            <tr>
                <td>name</td>
                <td>surname</td>
                <td>id</td>
            </tr>

        </thead>
        <tbody></tbody>


    </table>

</body>
</html>

oTable.$('tr:first')将引发错误 - $不是oTable的函数或属性。

你必须使用

oTable.find('tbody tr:first').focus()

因为tr:first会返回<thead> <tr> ,而不是<tbody> <tr>

我不认为你在默认情况下可以关注整个<tr>的HTML,所以你必须添加一些CSS的<tr>使焦点可见。 像这样

tr {
    border:1px inset white;
}
tr.focused {
   border:1px solid red; 
}

oTable.find('tbody tr:first').addClass('focused');

单击行时聚焦行的示例:

oTable.find('tbody tr').click(function() {
    oTable.find('tbody tr').removeClass('focused');
    $(this).addClass('focused');
});

以上所有这些小提琴 - > http://jsfiddle.net/vv7Sa/

检查此链接,它应该有所帮助: https//www.datatables.net/forums/discussion/18305/select-first-row-onload 或者你可以尝试这个:

 $('#table-id').on('init.dt', function() {
           $('#table-id tbody tr:eq(0)').click();
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM