【问题标题】:JQuery datatable headers not expandingJQuery 数据表标题不扩展
【发布时间】:2015-11-17 13:08:22
【问题描述】:

我知道这可能很简单,但我是 jQuery 新手,我发现的示例修复与我们使用的语法不符。

这是我的代码:

<div class="row">
    <div class="col-lg-12">
        <div class="table-header">Commission Detail&nbsp; &nbsp;</div>
                <table class="table table-striped table-bordered table-hover dataTable" id="commissionDetailTable">
                    <thead>
                    <tr>
                        <th>Coverage</th>
                        <th>Transaction Date</th>
                        <th>Premium</th>
                        <th>Commission Rate</th>
                        <th>Commission Amount</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach (var item in Model.CommissionDetails)
                    {
                        <!--Important - In order for dataTable to work body cannot have any colspan items, and must match the header exactly-->
                        <tr>
                            <td>@item.Coverage</td>
                            <td>@item.TransactionDate.ToShortDate()</td>
                            <td>@item.Premium</td>
                            <td>@item.CommissionRate</td>
                            <td>@item.CommissionAmount</td>
                        </tr>
                    }
                    </tbody>

                    <tfoot>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    </tfoot>
                </table>
    </div>
</div>

<script>
$(document).ready(function () {
    var table = $('table#commissionDetailTable').dataTable({
        dom: 'frtB',
        scrollY: '50vh',
        bAutoWidth: false,
        scrollX: false,
        paging: false,
        buttons: [
            {
                title: 'Commission Detail Export',
                extend: 'csv',
                className: 'btn btn-success',
                text: 'Export to Excel'
            }
        ]
    });
    $(window).on('resize', function () {
        table.fnAdjustColumnSizing();
    });
});
</script>

这是在模式窗口中调用的。如果我与 UI 中的表格进行交互,它的大小是正确的。我很确定这是使用“fnAdjustColumnSizing”属性的问题,但我无法让它工作。任何帮助表示赞赏。

【问题讨论】:

  • 尝试使用:bAutoWidth: true,
  • 问题是标题单元格不会在容器调整大小时调整大小?
  • 检查控制台是否有错误
  • 控制台没有错误

标签: jquery datatables


【解决方案1】:

尝试像这样使用fnAdjustColumnSizing

var table = $('table#commissionDetailTable').dataTable(/*...your options...*/);

$(window).on('resize', function () {
    table.fnAdjustColumnSizing();
});

http://jsfiddle.net/Lgf2cmLh/

【讨论】:

  • 我不知道为什么该评论不会格式化。代码运行,但标题仍然关闭。
  • 也许你有不同的表ID?
  • 另外,为什么要调整大小?该表是在模态窗口中生成的,所以我认为它会自动展开。
  • 它似乎与scrollY有关。如果我删除它,它可以工作,但我确实需要滚动。
  • @TrevorGoodchild 请用你的问题打开另一个帖子,因为这个帖子被标记为已解决
猜你喜欢
  • 2017-09-02
  • 1970-01-01
  • 2021-10-20
  • 2011-02-27
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2010-12-25
相关资源
最近更新 更多