【问题标题】:Bootstrap tooltips don't work in page different from the first of datatables引导工具提示在与第一个数据表不同的页面中不起作用
【发布时间】:2016-01-10 08:45:50
【问题描述】:

我正在开发几个数据表,其中某些列的单元格具有引导工具提示。 所以我正在使用:

数据表被组织在几个页面中。 当文档准备好并加载表格时,在数据表的第一页中,工具提示工作正常,但下一页的单元格没有工具提示!

我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery twitter-bootstrap datatables tooltip


    【解决方案1】:

    解决方案

    DataTables 每次重绘表格时都需要使用drawCallback 来初始化工具提示。这是必需的,因为在显示第一页时,DOM 中不存在除第一页以外的页面的 TR 和 TD 元素。

    演示

    $(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>

    链接

    有关更多示例和详细信息,请参阅jQuery DataTables: Custom control does not work on second page and after。

    【讨论】:

    • 感谢您的回答,它完美地解决了我的问题。
    • @Gyrocode 感谢您的解决方案,我正要编写自定义工具提示,然后发现了这个:):)
    • 像这样使用api,如何将工具提示添加到特定列?在此示例中,仅在 Position 我想要悬停文本。
    猜你喜欢
    • 2013-03-22
    • 2013-11-06
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    相关资源
    最近更新 更多