JQuery 更改 属性 数据 Table

JQuery Change Property of Data Table

您好,我在 JQuery 中创建了一个数据 table,如图所示。

<html>
<head>
<link rel="stylesheet" href="ui/jquery-ui.min.css">
<script src="ui/external/jquery/jquery.js"></script>
<script src="ui/jquery-ui.min.js"></script>
<script src="DataTables-1.10.7/media/js/jquery.js"></script>
<script src="DataTables-1.10.7/media/js/jquery.dataTables.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
    var table = $('#example').dataTable( {
        scrollY: 300,
        paging: false
    });
    var settings = table.fnSettings();
    console.log(settings)
});
</script>
</body>
</html>

我正在尝试将 属性 scrollY 修改为具有不同的值,例如 400。我想在初始化后执行此操作。但是,变量 settings 显然为空。

提前致谢。

您返回 NULL 值的原因是 jQuery 数据 table 无法找到您的 table.

您需要根据要显示的数据在页面上相应地定义 table HTML,在您的 javascript 上方。它需要遵循以下格式(包括 <thead><tbody> 标签。

<table id="example">
<thead>
  <tr>
    <th></th>
  </tr>
</thead>
<tbody>
  <tr>
    <td></td>
  </tr>
</tbody>
</table>

<script>
$(document).ready(function() {
    var table = $('#example').dataTable( {
        scrollY: 300,
        paging: false
    });
    var settings = table.fnSettings();
    alert(settings)
});
</script>

演示:http://jsfiddle.net/wynrj8vh/

另外,根据documentation,你需要传递一个字符串值给ScrollY:

$('#example').dataTable( {
  "scrollY": "200px"
} );

是的,您需要在 HTML 中定义您的 table:

<table id="example">

然后:

var table = $('#example').dataTable();
oSettings = table.fnSettings();

if(oSettings != null) {
   table.fnDraw();
}//if