如何给表头添加工具提示

5

我正在使用jQuery DataTables,并且过去两天一直在尝试为我的datatables添加标题列的工具提示,但是一直没有成功。

我使用了datatables网站上的示例,在行数据中添加了工具提示,但是那并没有起作用。我只看到一个工具提示,甚至没有获得列的标题。

以下是我目前的代码。

if (oTable != null) {
    oTable.fnClearTable();
    oTable.fnAddData(columnData);
} else {

    oTable = $('#caseDataTable').dataTable({
        "bDestroy": true,
        "aaData": columnData,
        "aoColumnDefs": columnNames,
        bFilter: true,
        bAutoWidth: true,
        autoWidth: true,
        "responsive": true,
        dom: 'Bfltip',
        buttons: [
            {
                extend: 'colvis',
                postfixButtons: ['colvisRestore'],
                collectionLayout: 'fixed two-column'
            }
        ],
        "fnDrawCallback": function() {
            if (typeof oTable != 'undefined') {
                $('.toggleCheckBox').bootstrapToggle({});
            }

            $('#caseDataTable thead tr').each(function () {
                var sTitle;
                var nTds = $('td', this);
                var columnTitle= $(nTds[0]).text();

                 this.setAttribute('title', columnTitle);
            });

            /* Apply the tooltips */
            $('#caseDataTable thead tr[title]').tooltip({
                "delay": 0,
                "track": true,
                "fade": 250
            });
        }
    });
}

你可能想看一下这个例子 - (针对行) https://datatables.net/beta/1.9/examples/advanced_init/events_post_init.html - Rachel Gallen
我在我的问题中有相同的代码。 :-). 我怎样才能仅针对我的标题列做同样的操作? - james Makinde
2个回答

10

问题原因

您的代码存在多个问题:

  • 您的 CSS 选择器不正确,应该针对 th 元素而不是 tr元素。
  • initComplete 是一个适当的位置,因为您只需要执行一次。

演示

我的下面的示例是针对 Bootstrap Tooltip 的。请根据您的工具提示插件进行调整。

$(document).ready(function() {  
    var table = $('#example').DataTable( {     
        "ajax": 'https://api.myjson.com/bins/qgcu',
        "initComplete": function(settings){
            $('#example thead th').each(function () {
               var $td = $(this);
               $td.attr('title', $td.text());
            });

            /* Apply the tooltips */
            $('#example thead th[title]').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>


0

我会尝试移动

            /* Apply the tooltips */
            $('#caseDataTable thead tr[title]').tooltip({
                 "delay": 0,
                 "track": true,
                 "fade": 250
            });

在fnDrawCallback之外

if (oTable != null) {
                oTable.fnClearTable();
                oTable.fnAddData(columnData);
            } else {

                oTable = $('#caseDataTable').dataTable({
                    "bDestroy": true,
                    "aaData": columnData,
                    "aoColumnDefs": columnNames,
                    bFilter: true,
                    bAutoWidth: true,
                    autoWidth: true,
                    "responsive": true,
                    dom: 'Bfltip',
                    buttons: [
                        {
                            extend: 'colvis',
                            postfixButtons: ['colvisRestore'],
                            collectionLayout: 'fixed two-column'
                        }
                    ],
                    "fnDrawCallback": function() {
                        if (typeof oTable != 'undefined') {
                            $('.toggleCheckBox').bootstrapToggle({});
                        }

                        $('#caseDataTable thead tr').each(function () {
                            var sTitle;
                            var nTds = $('td', this);
                            var columnTitle= $(nTds[0]).text();

                             this.setAttribute('title', columnTitle);
                        });

                    }
                });

                /* Apply the tooltips */
                $('#caseDataTable thead tr[title]').tooltip({
                     "delay": 0,
                     "track": true,
                     "fade": 250
                });
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接