【问题标题】:fixed column not working固定列不工作
【发布时间】:2023-03-21 14:16:02
【问题描述】:

数据表固定列不起作用

滚动体宽度和表格宽度相等。 所以我没有得到固定列的水平滚动条。

我们正在使用“jquery.dataTables.min-1.9.4.js”和 Fixedcolumns (3.0.1).js。

html 和 javascript:

<html>
<head>
<script src="datatables/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="datatables/jquery.dataTables.min-1.9.4.js" type="text/javascript"></script>
<script src="datatables/extras/FixedColumns.js" type="text/javascript"></script>

<link href="datatables/css/dataTables.fixedColumns.css" rel="stylesheet" type="text/css" />
<link href="datatables/css/jquery.dataTables-1.9.4.css" rel="stylesheet" type="text/css" />
<link href="datatables/css/dataTables.fixedHeader.min.css" rel="stylesheet" type="text/css" />
</head>


<body>

<table id="example" class="stripe row-border order-column" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th rowspan="2">Name</th>
                        <th colspan="2">HR Information</th>
                        <th colspan="3">Contact</th>
                    </tr>
                    <tr>
                        <th>Position</th>
                        <th>Salary</th>
                        <th>Office</th>
                        <th>Extn.</th>
                        <th>E-mail</th>
                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>Name</th>
                        <th>Position</th>
                        <th>Salary</th>
                        <th>Office</th>
                        <th>Extn.</th>
                        <th>E-mail</th>
                    </tr>
                </tfoot>

                <tbody>
                    <tr>
                        <td>Tiger Nixon</td>
                        <td>System Architect</td>
                        <td>$320,800</td>
                        <td>Edinburgh</td>
                        <td>5421</td>
                        <td>t.nixon@datatables.net</td>
                    </tr>
                    <tr>
                        <td>Garrett Winters</td>
                        <td>Accountant</td>
                        <td>$170,750</td>
                        <td>Tokyo</td>
                        <td>8422</td>
                        <td>g.winters@datatables.net</td>
                    </tr>
                    <tr>
                        <td>Ashton Cox</td>
                        <td>Junior Technical Author</td>
                        <td>$86,000</td>
                        <td>San Francisco</td>
                        <td>1562</td>
                        <td>a.cox@datatables.net</td>
                    </tr>
                    <tr>
                        <td>Cedric Kelly</td>
                        <td>Senior Javascript Developer</td>
                        <td>$433,060</td>
                        <td>Edinburgh</td>
                        <td>6224</td>
                        <td>c.kelly@datatables.net</td>
                    </tr>
                    <tr>
                        <td>Airi Satou</td>
                        <td>Accountant</td>
                        <td>$162,700</td>
                        <td>Tokyo</td>
                        <td>5407</td>
                        <td>a.satou@datatables.net</td>
                    </tr>                    
                </tbody>
            </table>
                                                <script type="text/javascript">
     $(document).ready(function () {
        var datatables_options =
                                {
                                    "bAutoWidth": true,
                                    "sDom": '<"top"i>rt<"bottom"flp><"clear">', //determine render order for datatables.net items, http://datatables.net/ref#sDom
                                    "bPaginate": false, // paging
                                    "sPaginationType": "full_numbers", // http://datatables.net/release-datatables/examples/basic_init/alt_pagination.html
                                    "iDisplayLength": 10, // page row size
                                    "bSort": true, //sorting
                                    "bFilter": false, // "search" box
                                    "aaSorting": [], // default sort
                                    "bInfo": false, // "Showing x to y of z entries" message
                                    "bStateSave": false, // save state into a cookie
                                    "iCookieDuration": 0, // save state cookie duration
                                    "bScrollAutoCss": true, // datatables.net auto styling of scrolling styles, http://datatables.net/forums/discussion/comment/15072
                                    "bProcessing": true, // "processing" message while sorting .. doesn't appear to be doing anything
                                    "bJQueryUI": false // css classes for jQueryUI themes?
                                    //"asStripeClasses": [], // remove odd/even row css classes (they will be assigned elsewhere)

                                };

        datatables_options["sScrollY"] = "450px";
        datatables_options["sScrollX"] = "100%";
        datatables_options["bScrollCollapse"] = true;
        var $datatable = $(".example").dataTable(datatables_options);
        new $.fn.dataTable.FixedColumns($datatable,
                                                {
                                                    "iLeftColumns": 1,

                                                    //"sLeftWidth": 'relative',
                                                    //"iLeftWidth": 20,
                                                    "sHeightMatch": "none", /* if there aren't any rows that have wrapping text this would be best because it is much faster in IE8 */
                                                    //"sHeightMatch": "semiauto",
                                                    //"sHeightMatch": "auto",
                                                });
    });
</script>
</body>
<style type="text/css">    
    th, td {
        white-space: nowrap;
        padding-left: 40px !important;
        padding-right: 40px !important;
    }
    div.dataTables_wrapper {
        width: 800px;
        margin: 0 auto;
    }
</style>
</html>

截图:

【问题讨论】:

    标签: javascript jquery jquery-datatables


    【解决方案1】:

    您只需将表格宽度设置为 100% 并且 sScrollX 设置为 100%。你缺少定义sScrollXInner

    此属性可用于强制 DataTable 使用比 当启用 x-scrolling 时,它可能会这样做。 (...)

    添加

    datatables_options["sScrollXInner"] = '150%'; 
    

    或您希望表格具有的任何宽度,到您的选项对象。您在演示中的示例 -> http://jsfiddle.net/PEN7T/

    【讨论】:

    • 他们是否提到过在数据表文档网站中添加 scrollXinner 以启用固定列。
    • 见这里datatables.net/extensions/fixedcolumns 没有添加“sScrollXInner”来启用固定列
    • 另外澄清一下您是否在 jsfiddle 中为固定列引用最新的 CSS 和脚本
    • 很明显,如果你研究我给出的 jsfiddle - 它使用 1.9.43.0.1 因为这些版本问题中提到了 :)
    • 10.04 文档中没有提到 sScrollXInnner 的原因是使用了 CSS,使所有列都变大了 +100px。您可以在这里看到完全相同的内容,使用您的表格和 10.04 版本,没有 sScrollXInnner -> jsfiddle.net/9wbPZ
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多