[英]dynamic tooltip text on every cell of table
i can set tool-tip for each cell of my table using title 我可以使用标题为表格的每个单元格设置工具提示
but is there a way to make the tool-tip text to be dynamic ? 但是有没有办法使工具提示文本具有动态性?
for example when i hover on any cell of table it show information about that cells entire row. 例如,当我将鼠标悬停在表的任何单元格上时,它将显示有关该单元格整行的信息。
<td title="something">46879</td>
example using datatables plugin : 使用datatables插件的示例:
$(document).ready(function() {
$('#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 );
} );
var oTable = $('#example').dataTable();
$( oTable.fnGetNodes() ).tooltip( {
"delay": 0,
"track": true,
"fade": 250
} );
} );
this is what i mean : 这就是我的意思:
https://jsfiddle.net/pow7651t/1/ https://jsfiddle.net/pow7651t/1/
Please have a look at following solution. 请看以下解决方案。 Note that, I have added an extra variable for columns.
请注意,我为列添加了一个额外的变量。 Let me know if this works, or if there is any issue with this solution.
让我知道这是否可行,或者此解决方案是否有问题。
$(document).ready(function() { var columns = ['Names', 'Reg Number', 'ID Code'] $('#example tbody tr').each(function() { var cells = $('td', this); var titleArr = cells.map(function(index) { return columns[index] + '=' + this.innerHTML; }); cells.each(function(index) { var finalTooltip = titleArr.filter(function(i){ return index !== i; }); $(this).attr('title', finalTooltip.toArray().join(',')) }) var name = cells[0]; var regNumber = cells[1]; var idCode = cells[2]; }); var oTable = $('#example').dataTable(); $(oTable.fnGetNodes()).tooltip({ "delay": 0, "track": true, "fade": 250 }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/> <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/> <table id="example"> <thead> <th>Names</th> <th>RegNumber</th> <th>ID Code</th> </thead> <tbody> <tr> <td>Ryan</td> <td>49765</td> <td>34</td> </tr> <tr> <td>John</td> <td>58964</td> <td>42</td> </tr> <tr> <td>Daniel</td> <td>472658</td> <td>24</td> </tr> </tbody> </table>
You have to create a title attr for each cell of your table , 您必须为表格的每个单元格创建一个标题属性,
in the below Fiddle a did loop throw each row , and inside those a get the text and the header of each cell except the current cell ( .not(this)
) and then set the title attribute to this last . 在下面的Fiddle中,一个did循环抛出了每一行,并在其中获取了除当前单元格(
.not(this)
)之外的每个单元格的文本和标题,然后将title属性设置为this。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.