Bootstrap tooltips don't work in page different from the first of datatables

笑着哭i 提交于 2020-07-07 09:09:05

问题


I'm developing several datatables where the cells of some columns have bootstrap tooltips. So i'm using:

  • Bootstrap framework
  • Datatables.

The Datatables is organized in several pages. When the document is ready and the table is loaded, in the first page of the datatables the tooltips are working fine, but the cells of the nexts pages have no tooltips!

How can I solve this problem?


回答1:


SOLUTION

You need to use drawCallback to initialize tooltips every time DataTables redraws the table. This is needed because TR and TD elements for pages other than first are not present in DOM at the time first page is displayed.

DEMO

$(document).ready(function() {  
    var table = $('#example').DataTable( {     
        ajax: 'https://api.myjson.com/bins/qgcu',
        drawCallback: function(settings){
            var api = this.api();
            
            /* Add some tooltips for demonstration purposes */
            $('td', api.table().container()).each(function () {
               $(this).attr('title', $(this).text());
            });

            /* Apply the tooltips */
            $('td', api.table().container()).tooltip({
               container: 'body'
            });          
        }  
    }); 
});
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 

<table id="example" class="display">
<thead>
    <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
        <th>Start Date</th>
    </tr>
</thead>

<tfoot>
    <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
        <th>Start Date</th>      
    </tr>
</tfoot>
</table>

LINKS

See jQuery DataTables: Custom control does not work on second page and after for more examples and details.



来源:https://stackoverflow.com/questions/34703628/bootstrap-tooltips-dont-work-in-page-different-from-the-first-of-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!