如何显示每个单元格的工具提示?

时间:2012-07-03 11:14:19

标签: jquery jquery-datatables

如何修改代码以将每个单元格信息显示为工具提示?

http://datatables.net/release-datatables/examples/advanced_init/events_post_init.html

$(document).ready(function() {
/*
* First step is to create title attributes for the rows in the table
* This isn't needed if the required 'title' attribute is already set in the HTML in the
* DOM
*/
$('#example tbody tr').each( function() {
var sTitle;
var nTds = $('td', this);
var sBrowser = $(nTds[1]).text();
var sGrade = $(nTds[4]).text();

if ( sGrade == "A" )
sTitle = sBrowser+' will provide a first class (A) level of CSS support.';
else if ( sGrade == "C" )
sTitle = sBrowser+' will provide a core (C) level of CSS support.';
else if ( sGrade == "X" )
sTitle = sBrowser+' does not provide CSS support or has a broken implementation. Block CSS.';
else
sTitle = sBrowser+' will provide an undefined level of CSS support.';

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

/* Init DataTables */
var oTable = $('#example').dataTable();

/* Apply the tooltips */
oTable.$('tr').tooltip( {
"delay": 0,
"track": true,
"fade": 250
} );
} ); 

2 个答案:

答案 0 :(得分:9)

你可以做到

{ "sTitle": "...", ...
   'fnCreatedCell': function(nTd, sData, oData, iRow, iCol) {
        nTd.title = 'Some more information';
   }
}
列配置中的

。您可以像这样轻松使用所有行数据。 当然,这绝不能错过:

oTable.$('td').tooltip( {
    "delay": 0,
    "track": true,
    "fade": 100
} );

答案 1 :(得分:2)

您可以通过简单地为每个td

设置setAttribute来设置标题
 $('#example tbody tr td').each( function() {
    this.setAttribute( 'title', $(this).text());
});

并在td上调用工具提示

oTable.$('td').tooltip( {
"delay": 0,
"track": true,
"fade": 250
} );