固定列不起作用

时间:2014-05-10 13:16:23

标签: javascript jquery jquery-datatables

数据表固定列无法正常工作

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

我们正在使用“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>

截图:

enter image description here

1 个答案:

答案 0 :(得分:2)

您只需将表格宽度设置为100%并将 sScrollX设置为100%。您无法定义sScrollXInner

  

此属性可用于强制DataTable使用更多宽度   否则在启用x滚动时可能会这样做。 (...)

添加

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

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