1

I'm using jQuery's DataTables and Bootstrap. So the problem I have is that when I switch between tabs of "completed" and "dead", the "dead" tab's column headers get bunched up on the left until I click on them and then they align themselves correctly. This happens whenever I refresh. I tried using CSS with overflow:auto, but that makes my whole panel scrollable, which I don't want. I want the headers to stay fixed while i scroll through the table.

  <div class = 'row'>
  <div class='col-lg-11'>

  <div id="pane3" > 
  <ul class="nav nav-tabs">
    <li class="active">
      <a  href="#4a" data-toggle="tab">My Completed</a>
    </li>
    <li>
      <a  href="#4b" id="deadTab"  data-toggle="tab">Dead Requests</a>
    </li>

  </ul>
  <div class="tab-content">
      <div class='tab-pane active' id ="4a">
        <!-- <div id="" style=" width: 100%; position:relative; height:200px;"> -->
          <table id='completed' class="table table-striped table-hover">
          <thead>
            <tr>
              <th>Req. #</th>
              <th>Project #</th>
              <th>Client Name</th>
              <th>Program</th>
              <th>Request Type</th>
              <th>Assignee</th>
              <th>Completed Date</th>
              <th>Clone</th>

            </tr>
          </thead>
          <tbody>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
          </tbody>
        </table>
      <!--   </div> -->
        </div>
          <div class='tab-pane' id ="4b">
         <!--  <div id="" style=" overflow:auto; position:relative; "> -->
            <table id='dead' class="table table-striped table-hover">
            <thead>
              <tr>
                <th>Req. #</th>
                <th>Project #</th>
                <th>Client Name</th>
                <th>Program</th>
                <th>Request Type</th>
                <th>Assignee</th>
                <th>Dead Date</th>
                <th>Restart</th>
              </tr>
            </thead>
            <tbody>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
            <tr>
              <td>July</td>
              <td>Dooley</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>john@e</td>
              <td>[email protected]</td>
              <td>6/12/2017</td>
              <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
            </tr>
          </tbody>
          </table>
         <!--  </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script>

$(document).ready(function() {



   $('#completed').DataTable( {
    "paging":false,
    // "fixedHeader": true,
    "scrollY": "140px",
    "table-layout":"fixed",
    "columnDefs":[
    {"width":60, "targets":0},
    {"width":60, "targets":1},
    {"width":60, "targets":2},
    {"width":60, "targets":3},
    {"width":90, "targets":4},
    {"width":90, "targets":5},
    {"width":90, "targets":6},
    {"width":90, "targets":7},
    ]

  });


  $('#dead').DataTable( {
    "paging":false,
    "fixedHeader": true,
    "scrollY": "140px",
    "table-layout":"fixed",
     "columnDefs":[
    {"width":60, "targets":0},
    {"width":60, "targets":1},
    {"width":60, "targets":2},
    {"width":60, "targets":3},
    {"width":90, "targets":4},
    {"width":90, "targets":5},
    {"width":90, "targets":6},
    {"width":90, "targets":7},
    ]

    // "table-layout":"fixed"



  });





});



</script>

1 Answer 1

1

The problem is that your other tab is hidden (display: none). You can initialize your data table after dead tab is shown like this:

$('#completed').DataTable({
  "paging": false,
  // "fixedHeader": true,
  "scrollY": "140px",
  "table-layout": "fixed",
  "columnDefs": [{
      "width": 60,
      "targets": 0
    },
    {
      "width": 60,
      "targets": 1
    },
    {
      "width": 60,
      "targets": 2
    },
    {
      "width": 60,
      "targets": 3
    },
    {
      "width": 90,
      "targets": 4
    },
    {
      "width": 90,
      "targets": 5
    },
    {
      "width": 90,
      "targets": 6
    },
    {
      "width": 90,
      "targets": 7
    },
  ]

});






$('#deadTab').one('shown.bs.tab', function(e) {
  $('#dead').DataTable({
    "paging": false,
    "fixedHeader": true,
    "scrollY": "140px",
    "table-layout": "fixed",
    "columnDefs": [{
        "width": 60,
        "targets": 0
      },
      {
        "width": 60,
        "targets": 1
      },
      {
        "width": 60,
        "targets": 2
      },
      {
        "width": 60,
        "targets": 3
      },
      {
        "width": 90,
        "targets": 4
      },
      {
        "width": 90,
        "targets": 5
      },
      {
        "width": 90,
        "targets": 6
      },
      {
        "width": 90,
        "targets": 7
      },
    ]

    // "table-layout":"fixed"

  });
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>

<div class="container">
  <div class='row'>
    <div class='col-lg-11'>
      <div id="pane3">
        <ul class="nav nav-tabs">
          <li class="active">
            <a href="#4a" data-toggle="tab">My Completed</a>
          </li>
          <li>
            <a href="#4b" id="deadTab" data-toggle="tab">Dead Requests</a>
          </li>

        </ul>
        <div class="tab-content">
          <div class='tab-pane active' id="4a">
            <!-- <div id="" style=" width: 100%; position:relative; height:200px;"> -->
            <table id='completed' class="table table-striped table-hover">
              <thead>
                <tr>
                  <th>Req. #</th>
                  <th>Project #</th>
                  <th>Client Name</th>
                  <th>Program</th>
                  <th>Request Type</th>
                  <th>Assignee</th>
                  <th>Completed Date</th>
                  <th>Clone</th>

                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
              </tbody>
            </table>
            <!--   </div> -->
          </div>
          <div class='tab-pane' id="4b">
            <!--  <div id="" style=" overflow:auto; position:relative; "> -->
            <table id='dead' class="table table-striped table-hover">
              <thead>
                <tr>
                  <th>Req. #</th>
                  <th>Project #</th>
                  <th>Client Name</th>
                  <th>Program</th>
                  <th>Request Type</th>
                  <th>Assignee</th>
                  <th>Dead Date</th>
                  <th>Restart</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
                <tr>
                  <td>July</td>
                  <td>Dooley</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>john@e</td>
                  <td>[email protected]</td>
                  <td>6/12/2017</td>
                  <td><button type="button" class="btn btn-primary btn-xs">View Form</button></td>
                </tr>
              </tbody>
            </table>
            <!--  </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.