最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add tooltip to table header - Stack Overflow

programmeradmin3浏览0评论

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.

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

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.

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
            });
        }
    });
}
Share Improve this question edited Oct 22, 2015 at 16:49 Gyrocode. 58.9k16 gold badges156 silver badges191 bronze badges asked Aug 31, 2015 at 18:59 james Makindejames Makinde 9733 gold badges18 silver badges37 bronze badges 2
  • you might wanna look at this example - (for rows) datatables/beta/1.9/examples/advanced_init/… – Rachel Gallen Commented Aug 31, 2015 at 19:07
  • I have same code above in my question. :-). How do I do the same for my header column ONLY is my question? – james Makinde Commented Aug 31, 2015 at 19:15
Add a ment  | 

2 Answers 2

Reset to default 10

CAUSE

There are multiple issues with your code:

  • 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

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

$(document).ready(function() {  
    var table = $('#example').DataTable( {     
        "ajax": 'https://api.myjson./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/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="//cdn.datatables/1.10.7/js/jquery.dataTables.min.js"></script>

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

<!-- Latest piled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn./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>

I would try moving

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

outside of the 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
                });
            }
发布评论

评论列表(0)

  1. 暂无评论