Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap tooltip in table header?

I am using default bootstrap tooltip in my table header. It shows irrelevant behavior, like- it increases the width of the header.

here is the codepen link

https://codepen.io/tumulalmamun/pen/mpQzBV

$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();   
});
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h3>Tooltip Example</h3>
  <p>The data-placement attribute specifies the tooltip position.</p>
      <div class="row">
      <div class="col-lg-3">
            <table class="table table-bordered table-hover table-striped">
                <thead>
                    <tr><th colspan="2"> status (Overall)</th></tr>
                    <tr>
                        <th colspan="2" data-toggle="tooltip" data-placement="right" title="Hooray!">
                            Pending 

                        </th>

                    </tr>
                    <tr>
                        <th data-toggle="tooltip" data-placement="right" title="Hooray!">M</th>
                        <th data-toggle="tooltip" data-placement="right" title="Hooray!">O</th>
                    </tr>  
                </thead>
</table>
        
      </div>     
     
 <div class="col-lg-3"></div>
      <div class="col-lg-3"></div>

    </div>
</div>

</body>
</html>
like image 459
Abdullah Al Mamun Avatar asked Jan 18 '18 15:01

Abdullah Al Mamun


1 Answers

The fix is to add the tool-tip to an element inside of th/td instead of directly on it. (I have added divs around the text you see in your example)

<table class="table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th colspan="5">Pre-Recruitment status (Overall)</th>
        </tr>
        <tr>
            <th rowspan="2" colspan="2">Status </th>
            <th rowspan="2">Candidate</th>
            <th colspan="2">
                <div data-toggle="tooltip" data-placement="right" title="Hooray!">Panding Doc.</div>
            </th>
        </tr>
        <tr>
            <th>
               <div data-toggle="tooltip" data-placement="right" title="Hooray!">M</div>
            </th>
            <th>
                <div data-toggle="tooltip" data-placement="right" title="Hooray!">O</div>
            </th>
        </tr>  
    </thead>
</table> 

When a tool-tip is displayed it adds a new div to the markup directly after the element with the data-toggle="tooltip" attribute on it (Inspect using dev tools to see this happening). Therefore it will break your table layout because a tool-tip will be placed after the table element. If it is done inside a table element the layout will not be affected.

I have forked your codepen with the solution.

like image 164
Michael Hancock Avatar answered Oct 11 '22 17:10

Michael Hancock