Adesso costruiamo un tooltip che ci dica di cliccare su una particolare cella della nostra tabella per evidenziare tutti i record dello stesso autore
Javascript code:
$(document).ready(function() {
var column = 3;
// Position the tooltip.
var positionTooltip = function(event) {
var tPosX = event.pageX - 5;
var tPosY = event.pageY + 20;
$('div.tooltip').css({top: tPosY, left: tPosX});
};
// Show (create) the tooltip.
var showTooltip = function(event) {
$('div.tooltip').remove();
var $thisAuthor = $(this).text();
$('<div class="tooltip">Click to highlight all articles written by ' + $thisAuthor + '</div>').appendTo('body')
positionTooltip(event);
};
// Hide (remove) the tooltip.
var hideTooltip = function() {
$('div.tooltip').remove();
};
$('table.striped td:nth-child(' + column + ')' )
.addClass('clickable')
.click(function(event) {
var thisClicked = $(this).text();
$('table.striped td:nth-child(' + column + ')').each(function(index) {
if (thisClicked == $(this).text()) {
$(this).parent().toggleClass('highlight');
} else {
$(this).parent().removeClass('highlight');
};
});
})
.hover(showTooltip, hideTooltip)
.mousemove(positionTooltip);
})
Le parti importanti del codice:
var positionTooltip = function(event) {
var tPosX = event.pageX - 5;
var tPosY = event.pageY + 20;
$('div.tooltip').css({top: tPosY, left: tPosX});
};
var showTooltip = function(event) {
$('div.tooltip').remove();
var $thisAuthor = $(this).text();
$('<div class="tooltip">Click to highlight all articles written by ' + $thisAuthor + '</div>').appendTo('body')
positionTooltip(event);
};
var hideTooltip = function() {
$('div.tooltip').remove();
};
.hover(showTooltip, hideTooltip) .mousemove(positionTooltip);
.tooltip {
position: absolute;
z-index: 2;
background: #efd;
border: 1px solid #ccc;
padding: 3px;
}
showTooltip.call(this, event);
Ecco la nostra tabella tooltip step 1