Question

Datatable Fixed column is not working

Scrollbody width and table widths are coming as equal. So I am not getting the horizontal scroll bar for the fxed columns.

we are using " jquery.dataTables.min-1.9.4.js" and Fixedcolumns (3.0.1).js.

html and 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>

screenshot:

enter image description here

Was it helpful?

Solution

You just have the table width set to 100% and sScrollX set to 100%. You lack to define sScrollXInner :

This property can be used to force a DataTable to use more width than it might otherwise do when x-scrolling is enabled. (...)

add

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

or whatever width you want the table to have, to your options object. Your example as above in a demo -> http://jsfiddle.net/PEN7T/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top