数据表:更改表的高度不起作用 [英] Datatables: Change height of table not working

查看:18
本文介绍了数据表:更改表的高度不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 bPaginate = false 的 Jquery Datatables 表,并且 sScrollY 是一些固定高度.最终我希望表格在 window.resize 事件上调整大小.

为了让它起作用,我构建了一个较小的测试用例:在以下代码片段中,我希望在单击按钮时调整表格的大小

HTML:

<头><link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/><script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script><script type="text/javascript" language="javascript" src="http://www.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script><元字符集=utf-8/><title>JS Bin</title><身体><input id="button" type="button" value="点击我!"/><table cellpadding="0" cellspacing="0" border="0" class="display" id="example"><头><tr><th>渲染引擎</th><th>浏览器</th><th>平台</th><th>引擎版本</th><第>CSS等级</th></tr></thead><tr class="奇数X"><td>三叉戟</td><td>互联网资源管理器 4.0<td>Win 95+</td><td class="center">4/td<td class="center">X</td></tr><tr class="even gradeC"><td>三叉戟</td><td>互联网资源管理器 5.0<td>Win 95+</td><td class="center">5</td><td class="center">C</td></tr><tr class="奇数A"><td>三叉戟</td><td>互联网资源管理器 5.5</td><td>Win 95+</td><td class="center">5.5</td><td class="center">A</td></tr></tbody><脚><tr><th>渲染引擎</th><th>浏览器</th><th>平台</th><th>引擎版本</th><第>CSS等级</th></tr></tfoot>

Javascript:

$('#button').click(function() {console.log('调用 .click() 的处理程序.');table = $('#example').dataTable();设置 = table.fnSettings();console.log('old:' + settings.oScroll.sY);settings.oScroll.sY = '150px';console.log('new:' + settings.oScroll.sY);table.fnDraw(false);});$('#example').dataTable({"sScrollY": "350px",bPaginate":假,"bJQueryUI": 真});

控制台输出符合预期:

 .click() 的处理程序被调用.旧:350px新:150像素

但是表格没有更新!知道我做错了什么吗?

一个活生生的例子可以在这里找到:http://jsbin.com/anegiw/12/edit

解决方案

好吧,似乎工作得很好的是利用 datatbables 框架添加的元素:

$(window).resize(function() {console.log($(window).height());$('.dataTables_scrollBody').css('height', ($(window).height() - 200));});$('#example').dataTable({"sScrollY": ($(window).height() - 200),bPaginate":假,"bJQueryUI": 真});

这个例子让表格随着窗口平滑地调整大小.

实例:http://jsbin.com/anegiw/18/edit>

I am using a Jquery Datatables table with bPaginate = false and sScrollY is some fixed height. Ultimately I want the table to resize on the window.resize event.

To get this to work I have built a smaller testcase: In the following code snippets I want the table to resize when I click the button

HTML:

<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
<script type="text/javascript" language="javascript" src="http://www.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <input id="button" type="button" value="Click me!" />
  <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
    <thead>
        <tr>
            <th>Rendering engine</th>

            <th>Browser</th>
            <th>Platform(s)</th>
            <th>Engine version</th>
            <th>CSS grade</th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd gradeX">
            <td>Trident</td>
            <td>Internet
                 Explorer 4.0</td>
            <td>Win 95+</td>
            <td class="center"> 4</td>
            <td class="center">X</td>

        </tr>
        <tr class="even gradeC">
            <td>Trident</td>
            <td>Internet
                 Explorer 5.0</td>
            <td>Win 95+</td>
            <td class="center">5</td>
            <td class="center">C</td>

        </tr>
        <tr class="odd gradeA">
            <td>Trident</td>
            <td>Internet
                 Explorer 5.5</td>
            <td>Win 95+</td>
            <td class="center">5.5</td>
            <td class="center">A</td>

        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th>Rendering engine</th>
            <th>Browser</th>
            <th>Platform(s)</th>

            <th>Engine version</th>
            <th>CSS grade</th>
        </tr>
    </tfoot>
</table>
  </body>
</html>

Javascript:

$('#button').click(function() {
  console.log('Handler for .click() called.');
  table = $('#example').dataTable();
  settings = table.fnSettings();  
  console.log('old:' + settings.oScroll.sY);
  settings.oScroll.sY = '150px';
  console.log('new:' + settings.oScroll.sY);
  table.fnDraw(false);
});
$('#example').dataTable({
  "sScrollY": "350px",
  "bPaginate": false,
  "bJQueryUI": true
});

Console output is as expected:

Handler for .click() called.
old:350px
new:150px

but the table doesn't update! Any idea what I am doing wrong?

A live example can be found here: http://jsbin.com/anegiw/12/edit

解决方案

Ok what seems to work nicely is to do tap into the elements added by the datatbables framework:

$(window).resize(function() {
  console.log($(window).height());
  $('.dataTables_scrollBody').css('height', ($(window).height() - 200));
});

$('#example').dataTable({
  "sScrollY": ($(window).height() - 200),
  "bPaginate": false,
  "bJQueryUI": true
});

This example lets the table resize smoothly with the window.

Live example: http://jsbin.com/anegiw/18/edit

这篇关于数据表:更改表的高度不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆