如何向表头添加工具提示 [英] How to add tooltip to table header

查看:104
本文介绍了如何向表头添加工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery DataTables,并且我一直试图在过去2天内将工具提示添加到我的数据表的Header列中无济于事。

I'm using jQuery DataTables and I have been trying to add tooltips to the Header column of my datatables for the past 2 days to no avail.

我在datatables网站上使用了这个例子,其中工具提示被添加到行数据中,但是没有用。我只看到一个工具提示,它甚至没有得到列的标题。

I have used the example on the datatables website where tooltips were added to the row data, but that didn't work. I only see one tooltip, and it does not even get the title of the column.

以下是我到目前为止的代码。

Below is the code I have so far.

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
            });
        }
    });
}


推荐答案


原因

您的代码存在多个问题:

There are multiple issues with your code:


  • 你有不正确的CSS选择器,你应该定位 th 元素而不是 tr

  • initComplete 是一个合适的地方,因为你只需要做一次。

  • You have incorrect CSS selectors, you should be targeting th elements and not tr.
  • initComplete is a proper place to do this since you only need to do it once.

DEMO

以下示例适用于Bootstrap Tooltip。相应地调整到工具提示插件。

My example below is for Bootstrap Tooltip. Adjust to your tooltip plugin accordingly.

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

这篇关于如何向表头添加工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆