javascript - jQuery 数据表默认折叠

标签 javascript jquery html css datatables

我的表格默认加载如下: enter image description here

我必须调整浏览器的大小才能使页眉展开: enter image description here

然后单击其中一个标题(排序)以展开所有行: enter image description here

是否缺少某些内容,但我无法找出默认折叠的原因。

js + handlebars 文件:

(function() {
    "use strict";

    var MyCollection = Backbone.Collection.extend({

        url: /some/path
    });

    var MyRowView = Reporting.RowView.extend({
        template: _.template($("#row-template").html())
    });

    var MyTableView = Reporting.TableView.extend({
        el: $(".report"),
        header: _.template($("#header-template").html()),
        table: _.template($( "#table-template").html()),
        RowView: MyRowView
    });

    var mycollection = new MyCollection();
    
    var tableView = new MyTableView({
        collection: mycollection
    });
    
    mycollection.fetch();
    
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/template" id="header-template">
    <tr>
        <th>{{i18n "Assignment"}}</th>
        <th>{{i18n "Status"}}</th>
        <th>{{i18n "Success"}}</th>
        <th>{{i18n "Score"}}</th>
        <th>{{i18n "Comments"}}</th>
        <th>{{i18n "Rating"}}</th>
    </tr>
</script>

<script type="text/template" id="row-template">
    <td><%= resourceName %></td>
    <td><%= completionStatus %></td>
    <td><%= successStatus %></td>
    <td><%= score %></td>
    <td><%= noComments %></td>
    <td><%= noRatings %></td>
</script>

<script type="text/template" id="table-template">
    <table id="table-id" width="100%" class="table table-striped table-hover table-bordered">
        <thead>
        </thead>
        <tbody>
        </tbody>
    </table>
</script>

<div class="report">
    {{include this resourceType="/path/to/bootstraptable"}}
</div>
{{includeClientLib categories="category.of.js.file" }}

最佳答案

原因

很可能发生这种情况是因为在 DataTables 初始化发生时您的表是隐藏的。但是,我在您的 JavaScript 代码中没有看到 DataTables 初始化代码。

解决方案

一旦您的表格可见,您需要运行以下函数。

$('#table-id').DataTable().columns.adjust();

来自columns.adjust()手册中的页面:

Call it when the table becomes visible if hidden when initialised (for example in a tab) or when the data changes significantly.

链接

参见 jQuery DataTables – Column width issues with Bootstrap tabs用于解决表最初隐藏时 jQuery DataTables 最常见的问题。

关于javascript - jQuery 数据表默认折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31351382/

相关文章:

html - 使用 CSS vh 单位和百分比时不显示内容

php - 如何在 JS 或 PHP 中突出显示不同类型的代码?

javascript - 如何将随机类名添加到博客模板中的元素

javascript - 嵌套for循环只执行一次

php - 检查用户是否存在于数据库(表单)中

jquery - 如何删除带有空格的元素?

javascript - 如何使用 javascript API 从 Parse 获取上传的图像

javascript - jQuery 日期范围选择器在 Firefox 和 Edge 中不起作用

html - 如何使用 schema.org 设置 Review-aggregate?

css - 移除响应式网站 HTML